简体   繁体   English

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

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

I am new with JAVA (computer programming by the way).我是 JAVA 新手(顺便说一下,计算机编程)。 The following program checks if the input is binary or not.以下程序检查输入是否为二进制。 And it should prompt user to re-enter integers until binary number is entered.它应该提示用户重新输入整数,直到输入二进制数。 But this program is doing exactly opposite.但是这个程序做的恰恰相反。 It is asking me to re-enter integers if input is binary, and the program terminates when non-binary is entered.如果输入是二进制,它要求我重新输入整数,并且程序在输入非二进制时终止。 I need a serious help, please.我需要一个认真的帮助,请。 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.");

        }

    }
}

It might be too late for the answer.答案可能为时已晚。 But the code can be simplified much if you make use of the method.但是如果使用该方法,代码可以大大简化。

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

Change your code更改您的代码

from this由此

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

    }

to this对此

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

and it will ask for values and tell if it is binary or not and it terminates if it is binary它会询问值并判断它是否是二进制的,如果它是二进制的,它会终止

Error coming due to Scanner class.由于 Scanner 类而出现错误。 you can run your program and check your logic by removing the Scanner class like this.您可以通过像这样删除 Scanner 类来运行您的程序并检查您的逻辑。

public class Test{公共课测试{

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.

相关问题 需要提示用户输入整数直到输入二进制数 - Need to prompt user to enter integers until binary number is entered 如果用户输入参数之外的数字,如何循环提示用户重新输入数字 - How to loop a prompt for a user to re-enter a number if they input a number outside of the parameters 我如何要求用户重新输入他们的选择? - How can I ask the user to re-enter their choice? 如果值不正确,使用户重新输入值 - make a user re-enter values if value not correct notifyItemRemoved(); 在我退出并重新进入活动之前不会从列表中删除评论 - notifyItemRemoved(); isn't removing the comment from the list until I exit and re-enter the activity 我有办法重新输入错误的输入吗? - Is there a way for me to re-enter a wrong input? 在第二个for循环中,如何使用户重新输入考试分数(如果为负)? - How to do I make the user re-enter the exam scores if its negative, in the second for loop? JDBC 登录,如果在数据库中找不到,则要求用户重新输入用户名和密码 - JDBC login that asks user to re-enter username and password if not found in database 试图提示用户重新进入catch块,但是catch块终止了吗? - Trying to prompt the user to re-enter from the catch block, but the catch block terminates? 当要求用户在Java中循环输入一些字符串时,如何仅保留文本打印一次 - How to keep Text print only once when asking a user to re-enter some string in loop in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM