2005-10-17

keyboard key in

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/*課本原例題為import java.util.Scanner
但JBuilder不接受Scanner指令,故改成上面三項import*/
public class display
{
public static void main (String[] args) throws IOException {
//發生"例外"時,丟到IOException作例外處理(為必須輸入指令)
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
//設定keyboard為一個bufferedreader
/*原程式為Scanner keyboard = new Scanner(System.in);
但是需配合import java.util.Scanner使用,故更改為現狀*/
System.out.println("Enter the number of pods follow by");
System.out.println("the number of peas in a pod:");
int numberOfPods = Integer.parseInt(keyboard.readLine());
int peasPerPod = Integer.parseInt(keyboard.readLine());
//因為readLine()一次只能讀一行,故分成兩行寫
//keyboard.readLine()處理後為字串,呼叫轉換的指令使其改變型態為integer
/*原程式為
intput numberOfPods = keyboard.nextInt();
intput peasPerPod = keyboard.nextInt();
因"nextInt" 不能使用,故更改為現狀*/
int totalNumberOfPeas = numberOfPods*peasPerPod;
//算數運算
System.out.print(numberOfPods+" pods and ");
System.out.println(peasPerPod + " peas per pod. ");
System.out.println("The total number of peas = " + totalNumberOfPeas);
//print結果
}
}

0 Comments:

張貼留言

<< Home