2015/4/10

第四週 製作按鈕及對話視窗

import java.awt.*; import java.awt.event.*; import javax.swing.*; class Test { public static void main(String[] args) {
JFrame jtfMainFrame = new JFrame("Which Button Demo"); jtfMainFrame.setSize(450, 150); JTextField jtfInput = new JTextField(20); JButton jbnButton1=new JButton("Button 1"); JButton jbnButton2=new JButton("Button 2"); JPanel jplPanel = new JPanel(); JPanel jplPane2 = new JPanel(); jplPanel.setLayout(new FlowLayout()); jplPanel.add(jtfInput); jplPanel.add(jbnButton1); jplPanel.add(jbnButton2); jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER); jtfMainFrame.pack(); jtfMainFrame.setVisible(true); System.out.print("yan-ya,liou"); } }



2015/3/12

第二週

I (javascript)

複製以下程式到wordpad
存檔並將檔案類型存成.txt檔
到檔案改檔名為.htm
點開網頁即可

參考網址:http://taiwantc.com/js/js_tut_a1.htm

<html>
<head>
<title> The First Example: Hello, World </title>
</head>
<body>
<h2> This line is HTML </h2>

<script language="JavaScript">
<!--
document.write("Hello, 邱莠惠.This sentence is written using JavaScript.");
// Text on the right of 2 slashes is comment
/* This is comment
that occur more than 1 line
*/
//-->
</script>
<noscript>
Sorry, but your browser doesn't run JavaScript.
</noscript>

<h2> This line is HTML </h2>
</body>
</html>


II (java)

複製以下程式到wordpad
將檔名存為HelloWorldApp.java
點開命名提示
進入檔案連結cd C:\Users\student\Desktop\myjava
然後javac HelloWorldApp.java
         java HelloWorldApp


/**
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}


III (java)

第一張圖:
複製以下程式到wordpad
改檔名為TestInet1.java
到命名提示字元輸入 javac TestInet1.java
java TestInet1

第二張圖:
輸入 ipconfig


import java.net.*;

public class TestInet1 {
  public static void main(String argv[]) 
  {
    try {
      InetAddress myip = InetAddress.getLocalHost();

      System.out.println(myip.getHostName());
      System.out.println(myip.getHostAddress());
    } catch (UnknownHostException e) {
      System.out.println("Error: unable to resolve localhost");
    }
  }
}



IV (javascipt)

複製以下程式碼到wordpad
將檔案類型改為.htm
點開網頁即可


<script language="JavaScript">
VIH_BackColor = "palegreen";
VIH_ForeColor = "navy";
VIH_FontPix = "16";
VIH_DisplayFormat = "You are visiting from:<br>IP Address: %%IP%%<br>Host: %%HOST%%";
VIH_DisplayOnPage = "yes";
</script>
<script language="JavaScript" src="http://scripts.hashemian.com/js/visitorIPHOST.js.php"></script>





2015/3/5

第一週 簡介&聖誕樹

1.為何why要選修這門課?(動機)
  因為想要多學會一種程式語言
2.希望從這門課獲得哪些知識?(目標)
   至少學會基礎的java程式,可以寫出一些簡易的成品
3.我要如何修習這一門課
   每週認真上課,跟上進度



class Test
{
public static void main(String[] args)
{
System.out.println("_____*_____");
System.out.println("____***____");
System.out.println("___*****___");
System.out.println("__*******__");
System.out.println("_____*_____");
System.out.println("_____*_____");
}
}


2014/11/28

11/28 第十週 使用陣列產生隨機變數

11/28  第十週  使用陣列產生隨機變數

一開始,先宣告一4*4的二維陣列以及隨機變數函數
再來進入到表單,設定每個按鈕的位置以及長寬
最後利用變數函數將設定值定在0~16
再把所有設定的內容ADD進去

EX:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
        Button[,] Button = new Button[4, 4];
        Random irand = new Random();
        int rd = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int x;
            int width, height;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    width = this.Size.Width;
                    height = this.Size.Height;
                    Button[i, j] = new Button();
                    Button[i, j].Location = new Point(i * 50, j * 50);
                    x = (i + 1) * (j + 1);
                    width = 50;
                    height = 50;
                    Button[i, j].Size = new Size(width, height);
                    Button[i, j].Text = x.ToString();
                    rd = irand.Next(0, 16);
                    Button[i, j].Text = Convert.ToString(rd);
                    this.Controls.Add(Button[i, j]);
                }

            }
        }
    }
}




2014/11/22

期中報告作業

期中報告作業

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication14
{
    public partial class Form1 : Form
    {
        Operator operator1 = new Operator();

        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue >= 96 && e.KeyValue <= 105)
            {
                operator1.choose(textBox1, (char)(e.KeyValue - 48));
            }
            else if (e.KeyValue >= 48 && e.KeyValue <= 57)
            {
                operator1.choose(textBox1, (char)e.KeyValue);
            }
            else
            {
                switch (e.KeyValue)
                {
                    case 107:
                        operator1.choose(textBox1, '+');
                        break;
                    case 109:
                        operator1.choose(textBox1, '-');
                        break;
                    case 106:
                        operator1.choose(textBox1, '*');
                        break;
                    case 111:
                        operator1.choose(textBox1, '/');
                        break;
                    case 8:
                        operator1.choose(textBox1, 'B');
                        break;
                    case 13:
                        operator1.choose(textBox1, '=');
                        break;
                    case 27:
                        operator1.choose(textBox1, 'C');
                        break;
                    case 110:
                        operator1.choose(textBox1, '.');
                        break;
                    case 46:
                        operator1.choose(textBox1, 'D');
                        break;
                    default:
                        Debug.Print("keycode = " + (e.KeyValue).ToString());
                        break;
                }
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            char ch = ((Button)sender).Text[0];
            if (((Button)sender).Text == "CE")
                ch = 'D';
            else if (((Button)sender).Text == "sqrt")
                ch = '@'; 
            
            operator1.choose(textBox1, ch);
            button5.Focus();
        }
    }

    public class Operator
    {
        private double num = 0.0, ans = 0.0;
        private char preOp;
        private int opConter = 0;
        private bool preIsOperator = true;
        private bool preIskEquals = true;
        private bool preIsDot = false;

        private string Run(char ch)
        {
            switch (ch)
            {
                case '+':
                    ans = ans + num;
                    break;
                case '-':
                    ans = ans - num;
                    break;
                case '*':
                    ans = ans * num;
                    break;
                case '/':
                    ans = ans / num;
                    break;
            }

            opConter = 0;
            return ans.ToString();
        }

        public void choose(TextBox textBox1, char ch)
        {
            switch (ch)
            {
                case '+':
                case '-':
                case '*':
                case '/':
                    if (!preIsOperator)
                    {
                        if (preOp == '\0') preOp = ch;
                        if (opConter > 0)
                            textBox1.Text = Run(preOp);
                        opConter++;
                        preIsOperator = true;
                    }

                    preIskEquals = false;
                    preIsDot = false;
                    preOp = ch;
                    break;
                case '=':
                    textBox1.Text = Run(preOp);
                    preIskEquals = true;
                    preIsOperator = true;
                    preIsDot = false;
                    break;
                case '@':
                    if (preIskEquals)
                    {
                        ans = Math.Sqrt(ans);
                        textBox1.Text = ans.ToString();
                    }
                    else
                    {
                        num = Math.Sqrt(num);
                        textBox1.Text = num.ToString();
                    }

                    break;
                case 'D': // CE
                    textBox1.Text = "0";
                    num = double.Parse(textBox1.Text);
                    break;
                case 'B': // BackSpace
                    if (textBox1.Text[textBox1.Text.Length - 1] == '.') preIsDot = false;

                    if (textBox1.Text.Length > 1)
                        textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
                    else
                        textBox1.Text = "0";

                    num = double.Parse(textBox1.Text);
                    break;
                case 'C':
                    textBox1.Text = "0";
                    num = ans = 0.0;
                    preIskEquals = true;
                    preIsOperator = true;
                    preIsDot = false;
                    break;
                case '.':
                    if (!preIsDot)
                    {
                        textBox1.Text += ".";
                        preIsOperator = false;
                        preIsDot = true;
                    }

                    break;
                default:
                    if (preIsOperator) textBox1.Text = "";
                    textBox1.Text += ch.ToString();
                    
                    if (preIskEquals)
                    {
                        ans = double.Parse(textBox1.Text);
                        num = 0.0;
                    }
                    else
                        num = double.Parse(textBox1.Text);

                    preIsOperator = false;
                    break;
            }

            Debug.Print("ans = " + ans.ToString() + ", num = " + num.ToString() + ", preOp = " + preOp.ToString() + ", ch = '" + ch.ToString() + "'\r\n");
        }
    }
}




2014/11/14

11/14 第八週 九九乘法表(VB)&圈叉遊戲(C#)

11/14 第八週  九九乘法表(VB)&圈叉遊戲(C#)

九九乘法表

首先,99乘法表部分
同樣打開Excel,利用巨集部分編輯程式
為了以防工作單內容有東西
所以我們可輸入一程式碼將內容清空
再來,宣告變數S(行數),i(被乘數),j(乘數)
做兩階迴圈產生i*j的數值並傳到各行列,即完成99乘法表

EX:
Private Sub CommandButton1_Click()
Range("a1:h9").Clear
S = 1
For i = 1 To 9
For j = 1 To 9
result = i & "*" & j & " = " & i * j
Cells(j, S).Value = result
Next j
S = S + 1
Next i
End Sub


圈叉遊戲

EX:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication16
{
    public partial class Form1 : Form
    {
        Button[,] Button = new Button[4, 4];
        int c = 0;
        int i, j, r;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (i = 1; i < 4; i++)
            {
                for (j = 1; j < 4; j++)
                {
                    Button[i, j] = new Button();
                    Button[i, j].Location = new Point(80 + i * 80, 80 + j * 80);
                    Button[i, j].Size = new Size(80, 80);
                    this.Controls.Add(Button[i, j]);
                    Button[i, j].Click += new EventHandler(Button1_Click);

                }
            }

        }

        private void Button1_Click(object sender, EventArgs e)
        {
            Button Buttons= sender as Button;
            c = c + 1;
            r = c % 2;
            textBox1.Text = Convert.ToString(r);
              if (r == 0)
               {
                   Button[i, j].Text = "O".ToString();
               }
              else
               {
                   Button[i, j].Text = "X".ToString();
               }
                  
            
        }

    }
}