2010年6月3日 星期四
BMI計算
import java.util.*;
import java.lang.Math;
public class myjava
{
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動產生方法 Stub
String inputs="";
try
{
Double a2;
FileReader myinpufile=new FileReader("940304.SM");
BufferedReader isr=new BufferedReader(myinpufile);
//System.out.println(isr.readLine());
for (int i=1;i<=3;i++)
{
double a,a1,a3;
inputs=isr.readLine();
StringTokenizer mytoken=new StringTokenizer(inputs,",");
//System.out.println(mytoken.nextToken());
//System.out.println(mytoken.nextToken());
String n=new String(mytoken.nextToken());
String m=new String(mytoken.nextToken());
a=Double.parseDouble(n);
a3=a/100;
a1=Double.parseDouble(m);
a2=a1/(a3*a3);
System.out.println(a);
System.out.println(a1);
System.out.println(a2);
FileWriter myoutfile=new FileWriter("940304w.SM");
BufferedWriter isrw=new BufferedWriter(myoutfile);
isrw.write("體重"+a3+" 身高"+a1+" BIM:"+Math.round(a2));
if(a2<20)
{
isrw.write("過輕");
}
else if(a2>25)
{
isrw.write("過重");
}
else
{
isrw.write("正常");
}
isrw.flush();
myoutfile.close();
}
}
catch(Exception e)
{
}
}
}
迴文palindrome判斷
public class around
{
public static void main(String args[]) throws IOException
{
String bi1;
FileReader inputjerry=new FileReader("940301.SM");
//BufferedReader buf =new BufferedReader(inputjerry);
BufferedReader binputjerry=new BufferedReader(inputjerry);
String s1=binputjerry.readLine();
String s2="";
for (int i=s1.length();i>0;i--){
char ch =s1.charAt(i-1);
s2=s2+ch;
}
if (s1.equals(s2))
bi1=s1+" is palindrome";
else
bi1=s1+" is not palindrome";
FileWriter outputjerry=new FileWriter("outputtext.txt");
BufferedWriter boutputjerry=new BufferedWriter(outputjerry);
boutputjerry.write(String.valueOf(bi1));
boutputjerry.flush();
}
}
nextToken的運用
import java.io.*;
import java.util.*;
// inputtext.txt 檔案內容 COFFEE 50 4
// 從檔案讀入一列字串,讀入50 和 4 相乘再寫入另一檔案
class TestReadWrite
{
public static void main(String[] args)
{
String line;
//int price,total;
try
{
FileReader inputjerry=new FileReader("inputtext.txt");
BufferedReader binputjerry=new BufferedReader(inputjerry);
line=binputjerry.readLine();
StringTokenizer stline=new StringTokenizer(line, " ");
BigInteger bi1 = new BigInteger(stline.nextToken());
System.out.println(bi1);
BigInteger bi2 = new BigInteger(stline.nextToken());
System.out.println(bi2);
bi1 = bi1.add(bi2);
System.out.println(bi1);
FileWriter outputjerry=new FileWriter("outputtext.txt");
BufferedWriter boutputjerry=new BufferedWriter(outputjerry);
boutputjerry.write(String.valueOf(bi1));
boutputjerry.flush();
boutputjerry.close();
}
catch(Exception e)
{
System.out.println("Exception");
}
}
}
輸入檔與輸出檔運用
期中考
2010年4月8日 星期四
建構子程式
class d9539186
{
public static void main(String[] args)
{
int w,u;
int a,b;
a=6;
b=8;
Triangel Alan1 = new Triangel();
Ractabgle Alan2 = new Ractabgle();
u=Alan1.add(a,b);
w=Alan2.add(a,b);
System.out.println(u);
System.out.println(w);
}
}
class Triangel
{
static int add(int a,int b)
{
int x,y,z;
x=a;
y=b ;
z=(x*y)/2;
return z ;
}
}
class Ractabgle
{
static int add(int a,int b)
{
int x,y,z;
x=a;
y=b ;
z=x*y;
return z ;
}
}
2010年3月19日 星期五
cos,sin
public class HelloWorld {
public static void main(String[] args) {
int x,y,z;
double pi;
x=1;
y=1;
z=x+y;
pi=Math.PI;
System.out.println("x="+x);
System.out.println("x="+y);
System.out.println("x="+z);
System.out.println("cos(0)="+Math.cos(0));
System.out.println("cos(pi/4)="+Math.cos(pi/4));
System.out.println("cos(pi/2)="+Math.cos(pi/2));
System.out.println("cos(3pi/2)="+Math.cos(3*pi/2));
System.out.println("cos(2pi)="+Math.cos(2*pi));
System.out.println("sin(0))="+Math.sin(0));
System.out.println("sin(pi/4))="+Math.sin(pi/4));
System.out.println("sin(pi/2))="+Math.sin(pi/2));
System.out.println("sin(3*pi/2))="+Math.sin(3*pi/2));
System.out.println("sin(2*pi))="+Math.sin(2*pi));
}
}