Java-如果满足条件,则继续
抱歉,如果代码从这里看起来有点混乱。这是我第一次发帖,我不知道如何为网站设置格式,因为我的时间有限 我在这里很为难。我已经花了好几个小时试图解决这个问题,但似乎没有时间去解决它。我是Java新手,所以如果代码有点无序、凌乱,或者对于它的功能来说有点太长,请不要介意。我的问题在于前几行,在下面: “扫描仪输入=新扫描仪(System.in);” 我能够让if语句在输入了高于或低于0/99的数字的情况下正常工作,但是如果输入了介于这两个数字之间的数字,则不会发生任何事情。我在我的教科书(Java编程综合入门第10版)中搜索了一下,似乎没有找到问题所在。有没有办法让程序继续通过“if”语句并进入涉及“input2”的代码行 提前谢谢Java-如果满足条件,则继续,java,Java,抱歉,如果代码从这里看起来有点混乱。这是我第一次发帖,我不知道如何为网站设置格式,因为我的时间有限 我在这里很为难。我已经花了好几个小时试图解决这个问题,但似乎没有时间去解决它。我是Java新手,所以如果代码有点无序、凌乱,或者对于它的功能来说有点太长,请不要介意。我的问题在于前几行,在下面: “扫描仪输入=新扫描仪(System.in);” 我能够让if语句在输入了高于或低于0/99的数字的情况下正常工作,但是如果输入了介于这两个数字之间的数字,则不会发生任何事情。我在我的教科书(Java编程
package lab03;
import java.util.Scanner;
public class Lab03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter an integer between 0-99: ");
int input1 = input.nextInt();
if (input1 < 0 || input1 > 99)
System.out.println(" Outside range. Please enter an integer between 0-99: ");
input1 = input.nextInt();
if (input1 < 0 || input1 > 99) {
System.out.println("Outside range. Program ending.");
System.exit(0);
System.out.println("Next, please enter another integer between 0-99: ");
int input2 = input.nextInt();
if (input2 < 0 || input2 > 99)
System.out.println(" Outside range. Please enter an integer between 0-99: `enter code here`");
input2 = input.nextInt();
if (input2 < 0 || input2 > 99) {
System.out.println("Outside range. Program ending.");
System.exit(0);
lab03包装;
导入java.util.Scanner;
公共类Lab03{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入一个介于0-99之间的整数:”);
int input1=input.nextInt();
如果(输入1<0 | |输入1>99)
System.out.println(“超出范围。请输入一个介于0-99之间的整数:”;
input1=input.nextInt();
如果(输入1<0 | |输入1>99){
System.out.println(“超出范围。程序结束”);
系统出口(0);
System.out.println(“下一步,请输入另一个介于0-99之间的整数:”;
int input2=input.nextInt();
如果(输入2<0 | |输入2>99)
System.out.println(“超出范围。请输入一个介于0-99之间的整数:`在此处输入代码'”;
input2=input.nextInt();
如果(输入2<0 | |输入2>99){
System.out.println(“超出范围。程序结束”);
系统出口(0);
我会将它们放在while(scanner.hasnetint())
循环中,然后如果满足条件,调用continue
,它将返回while循环的开头。问题是,由于调用System.exit()
if
语句执行它们下面的下一行或大括号内的所有内容。正如您所看到的,您有一个打开的大括号,但实际上并没有将代码张贴在大括号关闭的位置。这意味着将执行下一行下的所有内容,包括System.exit()
if(input1<0 | | input1>99){
您当前的程序可以简化为
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter an integer between 0-99: ");
int input1 = input.nextInt();
if (input1 < 0 || input1 > 99) {
System.out.println(" Outside range. Please enter an integer between 0-99: ");
}
input1 = input.nextInt();
if (input1 < 0 || input1 > 99) {
System.out.println("Outside range. Program ending.");
System.exit(0);
// everything below this will never execute
}
}
publicstaticvoidmain(字符串参数[]){
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入一个介于0-99之间的整数:”);
int input1=input.nextInt();
如果(输入1<0 | |输入1>99){
System.out.println(“超出范围。请输入一个介于0-99之间的整数:”;
}
input1=input.nextInt();
如果(输入1<0 | |输入1>99){
System.out.println(“超出范围。程序结束”);
系统出口(0);
//低于这一点的一切都将永远无法执行
}
}
我认为您缺少了大括号,它定义了第一个if语句的结果范围。您可能是指这样的内容
if (input1 < 0 || input1 > 99) {
System.out.println(" Outside range. Please enter an integer between 0-99: ");
input1 = input.nextInt();
if (input1 < 0 || input1 > 99) {
System.out.println("Outside range. Program ending.");
System.exit(0);
}
}
if(input1<0 | | input1>99){
System.out.println(“超出范围。请输入一个介于0-99之间的整数:”;
input1=input.nextInt();
如果(输入1<0 | |输入1>99){
System.out.println(“超出范围。程序结束”);
系统出口(0);
}
}
但实际发生的情况是,程序正在等待第二次尝试输入
input1
基本上,您已将“继续”条件包装在“失败”条件if
块中
if (input1 < 0 || input1 > 99) {
System.out.println("Outside range. Program ending.");
System.exit(0);
// This is included within the failed block
// but will never execute because of the previous statement
System.out.println("Next, please enter another integer between 0-99: ");
int input2 = input.nextInt();
if (input2 < 0 || input2 > 99) {
System.out.println(" Outside range. Please enter an integer between 0-99: `enter code here`");
}
input2 = input.nextInt();
if (input2 < 0 || input2 > 99) {
System.out.println("Outside range. Program ending.");
System.exit(0);
}
}
记住{…}
将上下文添加到它所包围的代码中
根据您想要实现的目标,您还可以使用do while
循环在输入循环中“陷阱”用户,直到他们输入正确的值
int input1 = -1;
do {
System.out.println("Please enter an integer between 0-99: ");
String text = input.nextLine();
try {
input1 = Integer.parseInt(text);
if (input1 < 0 || input1 > 99) {
System.out.print("Outside range. ");
}
} catch (NumberFormatException exp) {
System.out.println(text + " is not a valid numerical value");
}
} while (input1 < 0 || input1 > 99);
intinput1=-1;
做{
System.out.println(“请输入一个介于0-99之间的整数:”);
String text=input.nextLine();
试一试{
input1=Integer.parseInt(文本);
如果(输入1<0 | |输入1>99){
系统输出打印(“超出范围”);
}
}捕获(NumberFormatException exp){
System.out.println(text+“不是有效的数值”);
}
}而(input1<0 | | input1>99);
现在,记住,这会使它们陷入陷阱,直到它们包含0到99之间的值,所以您可能需要考虑添加一个“逃逸”条件(IE)(类型E)退出,然后要么退出循环,要么退出程序;
当前代码仅适用于大于99且小于0的数字。对于介于0和99之间的数字,您需要一条else语句,程序才能继续 if (input1 < 0 || input1 > 99) {
System.out.println(" Outside range. Please enter an integer between 0-99: ");
input1 = input.nextInt();
}else{
//Continue on with program
}
if (input1 < 0 || input1 > 99) {
System.out.println("Outside range. Program ending.");
System.exit(0);
if(input1<0 | | input1>99){
System.out.println(“超出范围。请输入一个介于0-99之间的整数:”;
input1=input.nextInt();
}否则{
//继续节目
}
如果(输入1<0 | |输入1>99){
System.out.println(“超出范围。程序结束”);
系统出口(0);
您的所有if检查都超出范围。当数字在范围内时,您在做什么?您已将“良好”条件包装在失败条件块中
int input1 = -1;
do {
System.out.println("Please enter an integer between 0-99: ");
String text = input.nextLine();
try {
input1 = Integer.parseInt(text);
if (input1 < 0 || input1 > 99) {
System.out.print("Outside range. ");
}
} catch (NumberFormatException exp) {
System.out.println(text + " is not a valid numerical value");
}
} while (input1 < 0 || input1 > 99);
if (input1 < 0 || input1 > 99) {
System.out.println(" Outside range. Please enter an integer between 0-99: ");
input1 = input.nextInt();
}else{
//Continue on with program
}
if (input1 < 0 || input1 > 99) {
System.out.println("Outside range. Program ending.");
System.exit(0);