Upload
felipe-brenchley
View
261
Download
0
Tags:
Embed Size (px)
Citation preview
JAVA2012.10.19
OUTLINE Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice
INPUT(1)import java.io.* ;
public class InputStr{
public static void main(String[] args) throws java.io.IOException {
BufferedReader keyin =
new BufferedReader(new InputStreamReader(System.in));
// 定義 keyin 物件,並配置物件記憶體空間 String str = keyin.readline(); // 讀取輸入資料,並存入 keyin
System.out.println(str);
}
}
INPUT(2)import java.util.Scanner;
public class scan {
public static void main(String[] args) throws Exception{
Scanner keyin = new Scanner(System.in);
String str = keyin.nextLine();
System.out.print(str);
}
}
IF STATEMENT if(num%2==0){
System.out.println(“Even number”);
} else{
System.out.println(“Old number”); };
IF STATEMENT if(num%2==0){
System.out.println(“Even number!!”); System.out.println(“num =” +num);} else { System.out.println(“Old number!!”); System.out.println(“num =” +num);}
COMMON ERRORS IN SELECTION STATEMENT
Error 1: if ( radius >= 0 )
area = radius * radius * 3.14;System.out.println(“The area is” +
area);
COMMON ERRORS IN SELECTION STATEMENT
if ( radius >= 0 ){
area = radius * radius * 3.14;System.out.println(“The area is” +
area);}
COMMON ERRORS IN SELECTION STATEMENT
Error 2:if ( radius >= 0 );{
area = radius * radius * 3.14;System.out.println(“The area is” +
area);}
COMMON ERRORS IN SELECTION STATEMENT
if ( radius >= 0 );{
area = radius * radius * 3.14;System.out.println(“The area is” +
area);}
COMMON ERRORS IN SELECTION STATEMENT
Error 3: if ( a = true )
{System.out.println(“a is true !!” );
}
COMMON ERRORS IN SELECTION STATEMENT
if ( a == true ){
System.out.println(“a is true !!” );}
LOGICAL OPERATORSOperator Name Description
! not logical negation
&& and logical conjunction
|| or logical disjunction
^ exclusive or logical exclusive
真值表
SWITCH STATEMENTS
num = 1
num = 2
else
Statement 1
Statement else
Statement 2
true
true
true
false
false
false
SWITCH STATEMENTS switch (num){
case 1: statement 1;break;
case 2: statement 2;break;
default: statement else;System.exit(0);
}
SWITCH STATEMENTS import java.io.*;public class Season2Wear {public static void main(String[] argv) throws IOException {
System.out.println("請選擇季節:1.春 2.夏 3.秋 4.冬");System.out.print("→");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String str = br.readLine();int season = Integer.parseInt(str);
switch (season) {case 1: // 當 season 的數值為 1 System.out.println("請穿著長袖出門"); break; // 結束此 casecase 2: // 當 season 的數值為 2 System.out.println("請穿著短袖出門"); break; // 結束此 casecase 3: // 當 season 的數值為 3 System.out.println("請加件長袖輕薄外套出門"); break; // 結束此 casecase 4: // 當 season 的數值為 4 System.out.println("請穿著毛衣或大衣出門"); break; // 結束此 case } }}
RANDOM NUMBERimport java.util.Scanner;
public class SubtractionQuiz{
public static void main(String[] args){
int number1=(int)(Math.random()*10);
int number2=(int)(Math.random()*10);
if(number1<number2){
int temp =number1;
number1=number2;
number2=temp;
System.out.print("What is "+number1+" - "+number2+" ? ");
Scanner input new Scanner(System.in);
int answer =input.nextInt();
if(number1-number2==answer){
System.out.println("You are correct!!");
}else{
System.out.println("Your answer is wrong\n"+number1+"-"+number2+"is"+(number1-number2));
}
}
}
}
PRACTICE 1. 寫一程式比較五個任意輸入的數字 , 輸出
最大跟最小的數字
2. 請寫一個程式,輸入學生的成績,成績在 90~100 分為 A; 成績在 80~89 分為B; 範圍在 70~79 分為 C; 而範圍落在60~69 為 D;0~60 為 E( 分數不在範圍內請輸出”輸入錯誤” )( 使用 switch)
3.Write a program that prompts the user to enter a,b,c,d,e and f and displays the result. If ad-bc is 0,report that “The equation has no solution”.
4.Write a program that simulates picking a card from a deck of 52 cards. Your program should display the rank(Ace,2,3,4,5,6,7,8,9,10,Jack,Queen,King) and suit(Clubs,Diamonds,Hearts,Spades) of the card.
Ppt 下載: http://oss.csie.fju.edu.tw/~jastine01/
ppt.html