好的,这就是我被告知要做的
1.让用户输入两个整数以用于以下任务。
2.写一个简单的if结构检查输入的数字
用户并显示它是大于0,大于10还是负数。
3编写一个决策结构,该决策结构使用布尔表达式来确定和
显示第一和第二是否为正。
4,使用switch语句检查2的值从1到5以及
显示适当的值。当值为时,结构应退出
找到了。如果未找到值,请添加一个操作以显示未找到的值。
我本人已打到4,但无法弄清楚如果第二个数字不是1-5,如何退出switch语句。我知道如何显示“未找到值”,但知道如何显示它的值是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;
}
}
}
最佳答案
利用switch
中的掉线(并添加default
大小写):
switch (int2) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("The second number entered is " + int2 + ".");
break;
default: System.out.println("Number entered not between 1 and 5");
}