简体   繁体   English

为什么我在 java 中的代码不会在 output 中显示消息“你违反了规则”,即使我输入了超出给定范围的数字?

[英]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.

相关问题 为什么我的java代码输出错误的数字? - Why does my java Code output a wrong number? 为什么这个 Java 代码会给出“字符串索引超出范围”错误? - Why does this Java code give an " String index out of range " error? 我的输出跳过了我输入的一些短语 - My output is skipping a few phrases that I inputed (Java) 为什么我的代码没有打印出完整的数字? - (Java) Why is my code not printing out the full number? 为什么我的代码输出与Java期望的输出不同? - Why does my code output differently from what I expected in Java? 为什么我的 Java 代码 output 10 而不是 -1 在这种情况下,我该如何解决? - Why does my Java code output 10 instead of -1 in this situation, and how do I fix it? 为什么即使不满足循环内设置的条件,我的循环错误消息仍会打印出来 - Why does my looped error message print out even though the condition set inside the loop is not satisfied 如果数字超出范围,如果语句提示用户出现错误消息,我如何获取Java - how can i get java if statment promopt a user with error message when a number out of the range 我是java的新手,无法弄清楚为什么我的代码不打印出文本文件和字母出现 - I am new to java and can not figure out why my code does not print out both the text file and the letter occurrence 我不明白为什么我的 Java 代码出错 - I cannot figure out why my Java code is erroring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM