2010年6月3日 星期四

Java 程式設計心得

學習程式設計簡單來說就是要把握幾個原則,
邏輯上要正確,接著要符合電腦做事的方法,
這學期在學習Java程式語言遇到了不少問題,
首先就是程式的邏輯上並沒有問題,
但電腦卻無法執行,
這其中發現了許多問題,
其中一個問題是字型上的問題,
直接從網路上複製下來的資料貼到Wordpad中,
可能會在編譯時會發生問題,
或者是在讀檔時發生問題,
其中一個解決方式就是把資料先存到記事本存檔,
接著就是資料型態上轉換,
資料轉換有時會無法直接轉換,
必須先行轉成其他形式,
再轉成要呈現的格式,
真的是從錯誤中學習JAVA。

2010年5月28日 星期五

99/5/28 Java-BMI值計算


















import java.io.*;
import java.util.*;

public class fivete
{
public static void main(String args[])
{
String s1,s2,s3,bmis;
int i,num;
double h,w,bmi;
num = Integer.parseInt(args[0]);
try
{
FileReader ip=new FileReader("940304.SM");
BufferedReader bip=new BufferedReader(ip);

FileWriter opp=new FileWriter("940304w.SM");
BufferedWriter bopp=new BufferedWriter(opp);

for ( i = 1 ; i < num + 1; i++ )
{
s1=bip.readLine();
StringTokenizer cs =new StringTokenizer(s1,",");
s2=cs.nextToken();
s3=cs.nextToken();
h= Double.parseDouble(s2);
w= Double.parseDouble(s3);

bopp.write("身高:" + s2 +"CM" +" "+ "體重:" + s3 + "KG" + " " );


bmi=w/Math.pow(h/100,2);
bmis=Double.toString(bmi);

bopp.write("BMI:" + bmis + "\n");
if(bmi>25.0)
{
bopp.write("體重過重" );
}
else if(bmi<20.0)
{
bopp.write("體重過輕" );
}
else
{
bopp.write("正常範圍" );
}

bopp.write("\n");


bopp.flush();



}
bopp.close();
System.out.println("計算完成");


}
catch(Exception e)
{
System.out.println("Exception");
}


}
}



注意事項:
1.檔案的值先放入記事本再存入wordpad,避免因字型影響讀取。
2.範例:名稱.close();必須放在迴圈之外,避免迴圈發生錯誤。
3.args[0]為存取人數的變數,務必在執行時輸入。
4.同一行的值必須放入同樣多的變數才能將值全部讀取。
5.型態轉換:String to Double EX:w= Double.parseDouble(s3);
Double to String EX:bmis=Double.toString(bmi);
      String to Integer EX:num = Integer.parseInt(args[0]);
6.同一行斷句取值方式:StringTokenizer cs =new StringTokenizer(s1,",");
         s2=cs.nextToken();
7.要用StringTokenize,必須先宣告import java.util.*; 。

99/5/21 Java-迴文判斷






















import java.io.*;
import java.lang.Long;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.util.*;

public class fiveto
{
public static void main(String args[])
{
String op ="";
try
{
FileReader ip=new FileReader("940301.SM");
BufferedReader bip=new BufferedReader(ip);
String s1=bip.readLine();
String s2="";
for (int i=s1.length();i>0;i--)
{
char ch =s1.charAt(i-1);
s2=s2+ch;
}
if (s1.equals(s2))
{
op=s1+" is a palindrome.";
}
else
{
op=s1+" is not a palindrome.";
}

FileWriter opp=new FileWriter("op.txt");
BufferedWriter bopp=new BufferedWriter(opp);
bopp.write(op);
bopp.flush();

bopp.close();

}
catch(Exception e)
{
System.out.println("Exception");
}


}
}

2010年5月13日 星期四

99/5/14 Java 長整數運算


















import java.io.*;
import java.lang.Long;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.util.*;

class ffourteen
{

public static void main(String[] args)
{
String line;
long bi1,bi2,total;



try
{
FileReader inputjerry=new FileReader("input.txt");
BufferedReader binputjerry=new BufferedReader(inputjerry);
line=binputjerry.readLine();
StringTokenizer stline=new StringTokenizer(line, " ");

bi1=Long.parseLong(stline.nextToken());
System.out.println(bi1);

bi2=Long.parseLong(stline.nextToken());
System.out.println(bi2);

BigInteger bn1 = new BigInteger(stline.nextToken());
System.out.println(bn1);

BigInteger bn2 = new BigInteger(stline.nextToken());
System.out.println(bn2);

bn1=bn1.remainder(bn2);
System.out.println(bn1);

total =bi1+ bi2;
System.out.println(total);
FileWriter outputjerry=new FileWriter("outputtext.txt");
BufferedWriter boutputjerry=new BufferedWriter(outputjerry);
boutputjerry.write(String.valueOf(total));
boutputjerry.flush();

boutputjerry.close();

}
catch(Exception e)
{
System.out.println("Exception");
}
}

}

2010年5月7日 星期五

99/5/7 Java 檔案取值求和更改副檔名輸出檔案














import java.io.*;
import java.lang.Byte;
import java.util.StringTokenizer;
class HelloJava
{
public static void main(String[] args)
{
String InFile;
String OutFile;
String num;
int i;
double line;
double total = 0.0;
double avg ;
String end;
File mingfile = new File("mf.txt");


try
{
InFile=args[0];
System.out.println(InFile);
FileReader rFile = new FileReader(InFile);
BufferedReader brFile = new BufferedReader(rFile);
for( i = 1 ; i < 12 ; i++)
{
num = brFile.readLine();
System.out.println(num);
line = Double.parseDouble(num);
total = total + line;
}
avg = total / 11;
end = Double.toString(avg);
StringTokenizer tokens = new StringTokenizer( InFile, ".");
OutFile=tokens.nextToken()+ ".w" + tokens.nextToken();
System.out.println(OutFile);
File myfile=new File(OutFile);
FileWriter fos= new FileWriter(OutFile);
BufferedWriter bfos=new BufferedWriter(fos);

bfos.write(end);
bfos.flush();
bfos.close();





}
catch (Exception e)
{
System.out.println("Ming");
}
}
}

2010年4月30日 星期五

99/4/30 Java-型態轉換加總











import java.io.*;
import java.lang.Byte;
class Javaftirty
{
public static void main(String[] args)
{
int a,b,total;
System.out.println("Ming");
File mingfile = new File("mf.txt");
//Byte b = new Byte("1");
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
total=a+b;

try
{

FileOutputStream lym = new FileOutputStream(mingfile);
mingfile.createNewFile();
PrintStream lop = new PrintStream(lym);
lop.print(a +"+" + b + "=" + total);
//lym.write(b);
/*BufferedOutputStream yml = new BufferedOutputStream(lym);
PrintStream ps = new PrintStream(yml);
ps.println("12345");
ps.close();
lym.close();
yml.close();*/
}
catch (Exception e)
{
System.out.println("Ming");
}
}
}


資料來源
http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html
http://www.tcrc.edu.tw/question/30.pdf
http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileOutputStream.html
http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedOutputStream.html
http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintStream.html
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Byte.html

2010年4月16日 星期五

99/04/16 Java 期中考



















import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TriangleMethod extends JFrame
implements ActionListener {

private JButton button;
private JPanel panel;

public static int b1,b2;
public static void main(String[] args) {

int a1,a2;

a1 = Integer.parseInt(args[0]);
a2 = Integer.parseInt(args[1]);
b1 = a1;
b2 = a2;

TriangleMethod frame = new TriangleMethod();
frame.setSize(350, 400);
frame.createGUI();

frame.setVisible(true);

}



private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout() );

panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 300));
panel.setBackground(Color.white);
window.add(panel);

button = new JButton("Press me");
window.add(button);
button.addActionListener(this);
}

public void actionPerformed(ActionEvent event) {
Graphics paper = panel.getGraphics();
drawLogo(paper, 10, 20);
//drawLogo(paper, 100, 100);
//drawTriangle(paper, 100, 10, 40, 40);
drawTriangle(paper, 10, 100, 20, 60);
}

private void drawLogo(Graphics drawingArea,
int xPos, int yPos) {
drawingArea.drawRect(xPos, yPos, b1, b2);//drawingArea.drawRect(xPos, yPos, 40, 40);
//drawingArea.drawRect(xPos, yPos, 20, 20);
}

private void drawTriangle(Graphics drawingArea,
int xPlace,
int yPlace,
int width,
int height) {
drawingArea.drawLine(xPlace, yPlace,
xPlace, yPlace + b1);
drawingArea.drawLine(xPlace, yPlace + b1,
xPlace + b2, yPlace + b1);
drawingArea.drawLine(xPlace, yPlace,
xPlace + b2, yPlace + b1);
}
}



注意事項
1.首先必須先把值改程可以用輸入的。
2.最直接的方法就是放外層共用。
3.並不是直接將值傳入,改程讓程式自動取值。