【问题标题】:Prompting user to re-enter integers until binary number is entered提示用户重新输入整数,直到输入二进制数
【发布时间】:2017-11-12 03:47:55
【问题描述】:

我是 JAVA 新手(顺便说一下计算机编程)。以下程序检查输入是否为二进制。它应该提示用户重新输入整数,直到输入二进制数。但是这个程序做的恰恰相反。 如果输入是二进制的,它要求我重新输入整数,并且当输入非二进制时程序终止。 我需要一个认真的帮助,拜托。Here is my output

    public static void main(String[] args) {

    int value, userValue;
    int binaryDigit = 0, notBinaryDigit = 0;

    Scanner scan = new Scanner(System.in);

    while (true) {
        System.out.println("Please enter positive integers: ");

        userValue = scan.nextInt();
        value = userValue;

        while (userValue > 0) {
            if ((userValue % 10 == 0) || (userValue % 10 == 1)) {
                binaryDigit++;
            } else {
                notBinaryDigit++;
            }

            userValue = userValue / 10;

        }

        if (notBinaryDigit > 0) {
            System.out.println(value + " is a not a Binary Number.");
            break;
        } else {
            System.out.println(value + " is  a  Binary Number.");

        }

    }
}

【问题讨论】:

  • if (!((userValue % 10 == 0) || (userValue % 10 == 1)) -- 否定声明。
  • 您是否在 IDE 调试器中单步执行了代码?那是开始的地方。如果您不知道如何使用调试器,请立即开始学习。如果您要进行任何编码,这是一项必备技能。
  • 我不知道您打算将二进制值用于什么,但您应该考虑二进制数可以以一个或多个零开头。如果您将值存储在 int 中,则会删除前导零。还要考虑当用户输入一个非常大的二进制数时会发生什么,当它被解析为int 时超过了int 可以表示的最大值。还要考虑您没有处理用户输入文本而不是数字的情况。最后,一般而言,您应该通过创建易于理解的小方法将逻辑分解为单元,这将简化代码。

标签: java loops


【解决方案1】:

答案可能为时已晚。但是如果使用该方法,代码可以大大简化。

import java.util.Scanner;

public class MainClass {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println("Please enter positive integers: ");
            int userValue = scan.nextInt();
            if (isBinary(userValue)) {
                System.out.println(userValue + " is a Binary Number.");
                break;
            } else {
                System.out.println(userValue + " is  a not Binary Number.");
            }
        }
    }

    public static boolean isBinary(int input) {
        while (input > 0) {
            if ((input % 10 != 0) && (input % 10 != 1)) {
                return false;
            }
            input = input / 10;
        }
        return true;
    }
}

【讨论】:

    【解决方案2】:

    更改您的代码

    从这里

      if (notBinaryDigit > 0) {
            System.out.println(value + " is a not a Binary Number.");
            break;
        } else {
            System.out.println(value + " is  a  Binary Number.");
    
        }
    

    到这里

     if (notBinaryDigit > 0) {
            System.out.println(value + " is a not a Binary Number.");
            notBinaryDigit--;
        } if(binaryDigit >0 {
            System.out.println(value + " is  a  Binary Number.");
     binaryDigit--;
      break;
        }
    

    它会询问值并判断它是否是二进制的,如果是二进制则终止

    【讨论】:

    • 感谢您的帮助!
    【解决方案3】:

    由于 Scanner 类而出现错误。 你可以运行你的程序并通过像这样删除 Scanner 类来检查你的逻辑。

    公共类测试{

    public static void main(String []args){
    
        int value, userValue=34;
        int binaryDigit = 0, notBinaryDigit = 0;
        value = userValue;
    
        while (userValue > 0) {
            if ((userValue % 10 == 0) || (userValue % 10 == 1)) {
                    binaryDigit++;
                } else {
                    notBinaryDigit++;
                }
    
                userValue = userValue / 10;
    
            }
    
            if (notBinaryDigit > 0) {
                System.out.println(value + " is a not a Binary Number.");
    
            } else {
                System.out.println(value + " is  a  Binary Number.");
    
            }
    }
    

    }

    【讨论】:

    • Scanner类没有报错,需要先导入
    • 大哥,我已经导入了,但是由于ide配置的原因,有一段时间就来了。首先,检查真正的问题是什么,然后做任何你想做的事情。
    • ide config 导致您的计算机出现问题,与上述问题无关
    猜你喜欢
    • 1970-01-01
    • 2020-08-15
    • 2021-07-07
    • 2015-12-19
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    相关资源
    最近更新 更多