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");
}
}
}