星期一, 10月 31, 2005

"Lab 10-31 (1) product of N positive numbers"

import javax.swing.JOptionPane;
public class q1031
{
public static void main(String[] args)
{
double ans=1,one, count,i=1;
while(i>0)
{
String myString2=JOptionPane.showInputDialog("Enter a number: ");
int sum1 = Integer.parseInt(myString2);
one=sum1;
while(one>0)
{
ans=ans*one;
System.out.print( sum1 + " , ");

i++;
}

System.out.print( one + " , ");
System.out.println("Ans= "+ans);
System.exit(0);
}
}
}

Fibonacci numbers

Fibonacci numbers :

看了參考網頁中:兔子的繁殖
第一個月1對交配:(1)+0=1
第二個月產下一對: (1)+1=2
第三個月1對交配1對又產下1對:(1)+2=3
第四個月1對交配2對又各生了1對:(1+2)+2=5
第五個月2對交配3對又各生了1對:(1+2+2)+3=8

我們漸漸發現:Fibonacci numbers 的公式:
原來的兔子交配第一個月不會生之後持續生;
之後的兔子也是第一個月不會生之後持續生;

兔子繁殖

星期一, 10月 24, 2005

Lab exponential

import javax.swing.JOptionPane;
public class p7
{
public static void main(String[] args)
{
double n1=1,i,n2=1,count=1,count2=1,all=0,summ=0;
String sum=JOptionPane.showInputDialog("Enter a n!:");
double n = Integer.parseInt(sum);
System.out.println("N= " +n);

n2=n;
for (i=0;i<10;i++)
{

count=count*n1;


summ=n2/count;
System.out.print(summ+ " + ");
all=all+summ;
n2=n2*n;
n1++;
}
all=all+1;

System.out.println("");
System.out.println("exp= " +all );
}
}

PROJECT2.07

PROJECT2.07

import javax.swing.JOptionPane;
public class q3
{
public static void main(String[] args)
{
String mycount=JOptionPane.showInputDialog("Enter the price of item (between 25 cents and a dollar): ");
int itemPrice = Integer.parseInt(mycount);
int quarter=(100-itemPrice)/25;
int change=(100-itemPrice)-quarter*25;
int dime,nickel;

dime=change/10;
nickel=(change-dime*10)/5;

JOptionPane.showMessageDialog(null,"You bought an item for "+itemPrice+" cents and gave me a dollar.So your change is");
JOptionPane.showMessageDialog(null,+quarter+" quarters. "+dime+" dimes. "+nickel+" nickels. ");

System.exit(0);

}
}

  • 程式結果連結
  • PROJECT2.05

    PROJECT2.05

    import javax.swing.JOptionPane;
    public class q3 {
    public static void main(String[] args) {

    String PP=JOptionPane.showInputDialog("Enter a purchase price");
    double P=Integer.parseInt(PP);
    String SS=JOptionPane.showInputDialog("Enter a expected number");
    double S=Integer.parseInt(SS);
    String YY=JOptionPane.showInputDialog("Enter a expected salvage value ");
    double Y=Integer.parseInt(YY);
    double D=(P-S)/Y;
    System.out.println("the purchase price of an item: "+P);
    System.out.println("the expected salvage value: "+S);
    System.out.println("the expected number of years of service:"+Y);
    JOptionPane.showMessageDialog(null,"the yearly depreciation for the item: "+D);
    System.exit(0);
    }
    }


  • 程式結果連結
  • Lab Fibonacci numbers

    public class q3 {

    public static void main(String[] args){
    double down=0,up=1,count=0,ratio=0;
    for (int i=0;i<100;i++)
    {
    count=up+down;
    ratio =count / up;
    down = up;
    up = count;
    System.out.println(count + " ratio is " + ratio+ " . ");
    }
    }
    }

  • 程式結果連結
  • 星期四, 10月 20, 2005

    Lab If-Else (2)

    已完成


    import javax.swing.JOptionPane;
    public class q1
    {
    public static void main(String[] args)
    {


    String myString=JOptionPane.showInputDialog("Enter a number: ");
    int n = Integer.parseInt(myString);


    if(n<0){
    System.out.println("n<0,n= " +n+ " , ");
    }
    else if(n>=0 & n<100){
    System.out.println("0<=n<100,n= " +n+ " , ");
    }
    else if( 100<=n )
    {
    System.out.println("n>=100,n= " +n+ " , ");
    }
    System.exit(0);
    }
    }

    Lab Max-Min

    我的寫法是輸入數字到表裡面,再找出其最大與最小值.

  • Lab Max-Min程式碼



  • 備註:這是第三次的更新文章了希望程式更簡單明瞭
  • 程式執行結果
  • (自行輸入有幾個數,跟數字的值)

    星期一, 10月 17, 2005

    DISPLAY3.1+輸入數字

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    public class q1
    {
    public static void main(String[] args) throws IOException
    {
    BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));

    double netIncome, tax, fivePercentTax, tenPercentTax;

    System.out.println("Enter net income.\n"
    + "Do not include a dollar sign or any commas.");
    netIncome =Integer.parseInt(keyboard.readLine());


    if (netIncome <= 15000)
    tax = 0;
    else if ((netIncome > 15000) && (netIncome <= 30000))
    //tax = 5% of amount over $15,000
    tax = (0.05*(netIncome - 15000));
    else //netIncome > $30,000
    {
    //fivePercentTax = 5% of income from $15,000 to $30,000.
    fivePercentTax = 0.05*15000;
    //tenPercentTax = 10% of income over $30,000.
    tenPercentTax = 0.10*(netIncome - 30000);
    tax = (fivePercentTax + tenPercentTax);
    }

    System.out.print("Tax due = " +tax);
    }
    }

    display2.6

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    public class q1
    {
    public static void main(String[] args) throws IOException
    {
    BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the number of pods followed by ");
    System.out.println("the number of peas in a pod:");
    int numberOfPods =Integer.parseInt(keyboard.readLine());
    int peasPerPod =Integer.parseInt(keyboard.readLine());
    int totalNumberOfPeas = numberOfPods*peasPerPod;
    System.out.println(numberOfPods +" ponds and " + peasPerPod +" peas per pod .");
    System.out.println("The total number of peas = " +totalNumberOfPeas+ "." );
    }
    }

    10/17 輸入數字到視窗

    import javax.swing.JOptionPane;
    public class q1
    {
    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);

    }
    }

    星期一, 10月 03, 2005

    DISPLAY1.7&PROJECT5

    DISPLAY1.7已完成

    public class Untitled1 {
    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\" starts at index "
    + position);

    sentence = sentence.substring(0, position) + "love"
    + ending;
    System.out.println("The changed string is:");
    System.out.println(sentence);
    }
    }


    PROJECT5已完成

    public class Untitled1 {
    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 text 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);
    }
    }