好的,这就是我被告知要做的 1.让用户输入两个整数以用于以下任务。 2.编写一个简单的if then结构来检查输入的数字的值 用户并显示它是否大于 0、大于 10 还是负数。 3.编写一个决策结构,使用 boolean 表达式来确定和 显示一号和二号是否为正。 4.使用 switch 语句检查 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");
}
关于java - 开始 Java 决策结构和数学对象 Switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14962054/