简体   繁体   中英

Need to prompt user to enter integers until binary number is entered

This is the program that checks if the input integer is binary or not, now I need to create a loop that will prompt the user to renter integers until binary number is entered.

import java.util.Scanner;

public class BinaryNumbers {

    public static void main(String[] args) {
        int value, userValue;
        int binaryDigit = 0, notBinaryDigit = 0;

        Scanner scan = new Scanner(System.in);
        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 Binary Number.");
        } else {
            System.out.println(value + " is not a Binary Number.");

        }

    }
}
import java.util.Scanner;

public class BinaryNumbers {

public static void main(String[] args) {
    int value, userValue;
    Scanner scan = new Scanner(System.in);
    while(true){

    int binaryDigit = 0, notBinaryDigit = 0;

    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 Binary Number.");
        return; //does the trick ;)
    } else {
        System.out.println(value + " is not a Binary Number.");

    }
}

}
}

A simple return can end the program then and there :)

Why not using Regular Expressions?

All those checking on user inputs by assuming them as numbers (by calling Scanner#nextInt or Scanner#nextFloat or ...) are very breakable. How can be so sure user won't enter anything wrong?

It's better to hold the user input in a String variable and check it against being binary integer (which has to be 32 bit at most as defined in many languages such as java) using Regex is more safe:

import java.util.Scanner;

public class CheckBinaryInteger {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean isValid = false;
        String userInput = "";
        do {
            System.out.print("Please Enter a binary integer: ");
            userInput = sc.next();
            isValid = userInput != null && !userInput.trim().isEmpty() 
                      && userInput.matches("[01]{1,32}");//assume every digit as one bit 
            if(!isValid)
                System.out.println("invalid binary integer entered! ");
        }while(!isValid);

        System.out.println("Valid input: "+userInput);
    }
}

Hope this helps.

import java.util.Scanner;

public class BinaryNumbers {

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 Binary Number.");
break;
} 
else 
System.out.println(value + " is not a Binary Number.");


}
}
}
import java.util.Scanner;
class calculatePremium
{
public static void main(String[] args) {
    int value, userValue;
    int binaryDigit = 0, notBinaryDigit = 0;

    Scanner scan = new Scanner(System.in);

    while(true)   /*use a while loop to iterate till you get the binary number*/
{
binaryDigit = 0; notBinaryDigit = 0;
    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 Binary Number.");
    break; /* breaks out of loop when gets the correct input */
    } else {
        System.out.println(value + " is not a Binary Number.\n");

    }

}

}


}

You just need to use a loop till you get a binary number. Hope it helps.

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