好的,這是我被告知要做的事 1.有用戶輸入兩個整數用於以下任務。 2.寫一個簡單的結構體,以檢查用戶輸入的數字的值,並顯示它是大於0,大於10還是負值。 3.編寫一個使用布爾表達式確定的決策結構,並且 顯示第一個和第二個是否爲正數。 4.使用switch語句檢查從1到5的數字2的值,並且 顯示適當的值。當值爲 時,結構應該退出。如果找不到找到的值,請添加一個操作以顯示未找到的值。Beginning Java Decision Structures and The Math Object Switch Statement
我已經得到了4自己,但無法弄清楚如何使switch語句退出,如果第二個數字是沒有1-5。我知道如何顯示「未找到的值」,但知道如何告訴它顯示它是1-5未輸入。讓我知道一個簡單的做法,謝謝!這是我到目前爲止:
package ICS4UIReviewTest;
import java.util.Scanner;
import java.lang.Math;
public class PartOneDecisionStructuresAndMathObject {
public static void main (String [] args) {
int int1, int2;
Scanner input = new Scanner (System.in);
System.out.print("Enter the first integer: ");
int1 = input.nextInt();
System.out.print("Enter the second integer: ");
int2 = input.nextInt();
if (int1 > 0 && int1 < 10){
System.out.println("The first number is > 0.");
} else {
if (int1 > 10){
System.out.println("The first number is > 10.");
} else {
System.out.println("The first number is negative.");
}
}
if (int1 > 0 && int2 > 0) {
System.out.println("The 1st & 2nd number are postiive.");
}
switch (int2) {
case 1: System.out.println("The second number entered is 1."); break;
case 2: System.out.println("The second number entered is 2."); break;
case 3: System.out.println("The second number entered is 3."); break;
case 4: System.out.println("The second number entered is 4."); break;
case 5: System.out.println("The second number entered is 5."); break;
}
}
}
正確的,但是你並不需要''中的情況下default' break'。 – jlordo 2013-02-19 16:19:40