[英]Why my code in java does not output the message "you have broken the rule" even if I inputed the number out of the given range?
I am using the if else statement to output the message Exelent or you have broken the rule based on the user input.我正在使用 if else 语句对 output 消息 Exelent 或者您违反了基于用户输入的规则。 However, why my code always output the Excelent?但是,为什么我的代码总是 output 优秀? although I have written the number out of the given range?虽然我写的数字超出了给定范围?
The code:编码:
import java.util.Scanner;
public class numberOfChoice{
public static void main(String[] args) {
int choosenNumber;
Scanner input=new Scanner (System.in);
System.out.println("Write your number in between 1 and 100");
choosenNumber=input.nextInt();
if (choosenNumber>0 && choosenNumber<100) {
System.out.println("Excelent!");
} else{
System.out.println("You have broken the rule");
}
}
}
I would like to add the output as a picture but I could not upload it.我想将 output 添加为图片,但无法上传。
Honestly I'm unable to replicate this problem.老实说,我无法复制这个问题。 When I copy paste this code and run it, it runs as expected.当我复制粘贴此代码并运行它时,它会按预期运行。 Try running it in another compiler and see what happens尝试在另一个编译器中运行它,看看会发生什么
I'm not sure why your code doesn't work, but I would suggest running the code in another IDE (you can find a few online, try this one: https://www.onlinegdb.com/online_java_compiler , it's pretty good).我不确定你的代码为什么不起作用,但我建议在另一个 IDE 中运行代码(你可以在网上找到一些,试试这个: https://www.onlinegdb.com/online_java_compiler ,非常好)。
Maybe double-check to see if you are running the wrong program (although very unlikely), and make sure what you input is correct.也许仔细检查你是否运行了错误的程序(虽然不太可能),并确保你输入的内容是正确的。 Also check to see if the code is the same - especially in the if-else statement, make sure it's &&
(and) and not ||
还要检查代码是否相同——尤其是在 if-else 语句中,确保它是&&
(and) 而不是||
(or). (或者)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.