horsemanship世界

星期一, 3月 27, 2006

Lab6

單迴圈版

public class Untitled7 {
public Untitled7() { }
public static void main(String[] args) throws IOException {
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
System.out.println("輸入一個數字 X");
String w = keyboard.readLine();
double x = Integer.parseInt(w);
double y=1,z=1,q=1;
for(int i=1;i<=10;i++) { y=y*i; //階乘 z=x*z; //次方 q=q+z/y; } System.out.println("exp(
X)="+q);}}

結果:
輸入一個數字 X
10
exp(
X)=12842.305114638448

================================================================
雙迴圈版

public class Untitled7 {
public Untitled7() {
}
public static void main(String[] args) throws IOException {
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
System.out.println("輸入一個數字X");
String x = keyboard.readLine();

int y = Integer.parseInt(x);
double m=1,n,w=1,q=1;
double z=1;

for(int i=1;i<=10;i++) { w=w*y; //x的次方迴圈 m=1; for(int j=1;j<=i;j++) { m=j*m; //階乘 } z=z+w/m; } System.out.println("
exp(X)="+z);}}

結果:

輸入一個數字X
3
exp(X)=20.079665178571425

星期六, 3月 25, 2006

Homework5

public class Untitled2
{
public static void main(String[] args)
{
double m=0,n=1,a=0,x;
int i;
System.out.println("累加數列:");
for(i=1;i<=100;i++)
{
a=m+n; m=n; n=a; x=a/m;
System.out.println(a+"比例:"+x );
} }}

結果(僅列出一部份):
累加數列:
1.0比例:1.0
2.0比例:2.0
3.0比例:1.5
5.0比例:1.6666666666666667
8.0比例:1.6
13.0比例:1.625
21.0比例:1.6153846153846154
34.0比例:1.619047619047619
55.0比例:1.6176470588235294
89.0比例:1.6181818181818182
144.0比例:1.6179775280898876
233.0比例:1.6180555555555556
377.0比例:1.6180257510729614
610.0比例:1.6180371352785146
987.0比例:1.618032786885246
1597.0比例:1.618034447821682
2584.0比例:1.6180338134001253
4181.0比例:1.618034055727554
6765.0比例:1.6180339631667064
10946.0比例:1.6180339985218033
17711.0比例:1.618033985017358
28657.0比例:1.6180339901755971
46368.0比例:1.618033988205325
75025.0比例:1.618033988957902
121393.0比例:1.6180339886704431
196418.0比例:1.6180339887802426
317811.0比例:1.618033988738303
514229.0比例:1.6180339887543225
832040.0比例:1.6180339887482036
1346269.0比例:1.6180339887505408
2178309.0比例:1.6180339887496482
3524578.0比例:1.618033988749989
5702887.0比例:1.618033988749859
9227465.0比例:1.6180339887499087

星期一, 3月 20, 2006

Lab5

import java.io.*;
public class work{ public static void main(String[] args) throws IOException{BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
int x;
double r,z,w,max,min;
String y;System.out.println("請輸入任意數字(十次):");
y = keyboard.readLine();
w = Double.parseDouble(y);
max=w;
min=w;
for( x=1 ; x<=9; x++) { y = keyboard.readLine();
w = Double.parseDouble(y);
if (w >= max) max = w;
if (w < min) min = w;
}
System.out.println("最大的數字是:"+max);}}

結果:
請輸入任意數字(十次):
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
最大的數字是:-1.0

星期一, 3月 13, 2006

Homework4

作業1:
import java.io.*;
public class work {
public static void main(String[] args) throws IOException
{
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入你的年收入:");
String number1 = keyboard.readLine();
int x = Integer.parseInt(number1);
double y,z=0;

if(x<370000)
{
z = x*0.06;
}
else if( x < 990000)
{
y = x * 0.13;
z = y - 25900;
}
else if( x < 1980000)
{
y = x*0.21;
z = y-105100;
}
else if( x < 3720000)
{
y = x*0.3;
z =y-283300;
}
else
{
y=x*0.4;
z=y-655300;
}
System.out.println("你應繳交" + z + "元的所得稅.");
}
}

結果:
請輸入你的年收入:
3720001
你應繳交832700.4000000001元的所得稅.
=====================================================================
作業2:
import java.io.*;
public class work {public static void main(String[] args) throws IOException{BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a nonnegative number per line.");
System.out.println("請輸入任意數字,並且只能輸給正數,且最後一個數為負數。");
double max=-1,min=-1,num=0;int count=-1;
String input;
while(num >= 0){input = keyboard.readLine();
num = Double.parseDouble(input);
if ((max==-1num>=max)&&(num>=0))max = num;
if ((min==-1num<=min)&&(num>=0))min = num;
count++;
}if (count==0)System.out.println("No numbers entered.");
else{System.out.println("There are "+count+" numbers read.");
System.out.println("最大數 "+max);System.out.println("最小數"+min);
}}}

結果:
請輸入任意數字,並且只能輸給正數,且最後一個數為負數。
266
999
5
64
31
-2
There are 5 numbers read.
最大數 999.0
最小數5.0

Lab4



import java.io.*;
public class work {
public static void main(String[] args) throws IOException
{
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number1:");
String number1 = keyboard.readLine();
int x = Integer.parseInt(number1);

System.out.println("Enter the number2:");
String number2 = keyboard.readLine();
int y = Integer.parseInt(number2);

int z = x*y;
System.out.println("number1 is " +x);
System.out.println("number2 is "+ y);
System.out.println("The total number of number1*number2 = " + z);
}
}
結果:
Enter the number1:
999
Enter the number2:
888
number1 is 999
number2 is 888
The total number of number1*number2 = 887112


星期日, 3月 12, 2006

Homework3


import javax.swing.JOptionPane;
public class work
{
public static void main (String[] args)
{
String myString=JOptionPane.showInputDialog("Enter a number: ");

int myNumber = Integer.parseInt(myString);

System.out.println("The number is "+ myNumber);
}
}

結果:
1234567890

星期一, 3月 06, 2006

Lab3

display 1.7

public class work
{
public static void main(String[] args)
{
String sentence = "I hate text processing!";
int position = sentence.indexOf("hate");
String ending =
sentence.substring(position + "hate".length());
System.out.println("01234567890123456789012");
System.out.println(sentence);
System.out.println("The word \"hate\"start at index "+position );
sentence =sentence.substring(0, position)+"adore"+ending;
System.out.println("The changed staring is:");
System.out.println(sentence);
}
}

結果:
01234567890123456789012
I hate text processing!
The word "hate"start at index 2
The changed staring is:
I adore text processing!
---------------------------------------------------------------------------------------------

Project 5

public class work
{
public static void main(String[] args)
{
String sentence = "I hate you!";
int position = sentence.indexOf("hate");
String ending =
sentence.substring(position + "hate".length());
System.out.println("The line of \"hate\"to be changed is:");
System.out.println(sentence);
sentence = sentence.substring(0, position) + "love"+ ending;
System.out.println("I have rephrased that line to read :");
System.out.println(sentence);
}
}

結果:
The line of "hate"to be changed is:
I hate you!
I have rephrased that line to read :
I love you!

星期四, 3月 02, 2006

Homework2

public class work
{
public static void main (String[] args)
{
int weight1;
double weight2=6.181818181818;
double c=0.0175;
double Rmet=10;
double Bmet=8;
double Smet=1;
double calories;
double w,x,y,z;
w=c*weight2*10*30; x=c*weight2*8*30;
y=c*weight2*6*60; z=w+x+y;
System.out.println("Person's weight=150 pound.");
System.out.println("SO,change into kg= 150/2.2 ="+weight2);
System.out.println("The person run 30 minutes,so he was burned "+w+" calories.");
System.out.println("He play basketball 30 minutes,so he was burned "+x+" calories.");
System.out.println("He sleep 6 hours,so he was burned "+y+" calories.");
System.out.println("The total he burned was "+z+" calories.");
}
}
結果:
Person's weight=150 pound.
SO,change into kg= 150/2.2 =6.181818181818
The person run 30 minutes,so he was burned 32.4545454545445 calories.
He play basketball 30 minutes,so he was burned 25.9636363636356 calories.
He sleep 6 hours,so he was burned 38.9454545454534 calories.
The total he burned was 97.3636363636335 calories.