简体   繁体   中英

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

I am new with JAVA (computer programming by the way). 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. you can run your program and check your logic by removing the Scanner class like this.

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

        }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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