繁体   English   中英

提示用户重新输入整数直到输入二进制数

[英]Prompting user to re-enter integers until binary number is entered

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

    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.");

        }

    }
}

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

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;
    }
}

更改您的代码

由此

  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;
    }

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

由于 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.");

        }
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM