需要提示用户输入整数,直到输入二进制数

时间:2017-11-11 05:21:25

标签: java while-loop

这是检查输入整数是否为二进制的程序,现在我需要创建一个循环,提示用户输入整数,直到输入二进制数。我一直在努力,但不能,所以需要一些帮助。

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

        }

    }
}

4 个答案:

答案 0 :(得分:1)

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

    }
}

}
}

简单的返回可以结束程序然后在那里:)

答案 1 :(得分:0)

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


}
}
}

答案 2 :(得分:0)

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

    }

}

}


}

你需要使用一个循环,直到你得到一个二进制数。 希望它有所帮助。

答案 3 :(得分:0)

为什么不使用正则表达式?

通过将用户输入视为数字(通过调用Scanner#nextIntScanner#nextFloat或...)来检查用户输入的所有人都非常容易破解。如何确保用户不会输入任何错误?

最好将用户输入保存在String变量中,并将其作为二进制整数(最多必须是32位,如许多语言中定义的,如java)进行检查,使用Regex更安全:< / p>

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

希望这有帮助。