Java 如何显示无效用户输入的错误消息?

Java 如何显示无效用户输入的错误消息?,java,Java,我的程序的目的是接收一个二进制数(1和0)作为输入,验证它是一个二进制数,如果它不是二进制数,则拒绝输入,并继续提示用户输入二进制数,然后输出该二进制数中有多少个1和0 这里是我遇到的问题:虽然我的程序确实输出数字中有多少个1和0,但即使我输入了正确的二进制数,我的输出仍然显示“错误:不是二进制数”。例如,如果我的输入是10001,则输出将是这样- 请输入一个二进制数。 10001 二进制数中有两个1。 二进制数中有3个零。 错误:不是二进制数。 请输入一个二进制数 我在代码中做错了什么 imp

我的程序的目的是接收一个二进制数(1和0)作为输入,验证它是一个二进制数,如果它不是二进制数,则拒绝输入,并继续提示用户输入二进制数,然后输出该二进制数中有多少个1和0

这里是我遇到的问题:虽然我的程序确实输出数字中有多少个1和0,但即使我输入了正确的二进制数,我的输出仍然显示“错误:不是二进制数”。例如,如果我的输入是10001,则输出将是这样-

请输入一个二进制数。 10001 二进制数中有两个1。 二进制数中有3个零。 错误:不是二进制数。 请输入一个二进制数

我在代码中做错了什么

import java.util.Scanner;

public class NewClass
{
public static void main( String [] args )   
{
Scanner scan = new Scanner( System.in);

 int i = 0, count1 = 0, count0 = 0;
 String number;

 System.out.println("Please enter a binary number.");
 number = scan.next();

 String number1 = "1";

while ((i = number.indexOf(number1, i++)) != -1) {
     count1++;
     i += number1.length();
 }

    System.out.println("There are "+ count1 + " ones in the binary number.");


    String number2 = "0";

 while ((i = number.indexOf(number2, i++)) != -1) {
     count0++;
     i += number2.length();
   }
    System.out.println("There are "+ count0 + " zeros in the binary number.");


    int total = (count1 + count0);
    int length = number.length();

 if (length != total);
 {
     System.out.println("ERROR: Not a binary number.");
     System.out.println("Please enter a binary number.");
     number = scan.next();


 }


 }

}

我将使用正则表达式检查字符串是否只有0和1:

String number = "10001";
if (number.matches(".*[^01].*")) {
    System.out.println("ERROR: Not a binary number.");
}

我将使用正则表达式检查字符串是否只有0和1:

String number = "10001";
if (number.matches(".*[^01].*")) {
    System.out.println("ERROR: Not a binary number.");
}

如果您注意到原始代码中有
If(length!=total)****{
这个
破坏了你的if语句,所以它总是被触发

    int total = (count1 + count0);
    int length = number.length();

    if (length != total)
    {
        System.out.println("ERROR: Not a binary number.");
        System.out.println("Please enter a binary number.");
        number = scan.next();
    }
我的建议是使用一个布尔函数来检查它是否是二进制的,如下所示

public static boolean isBinary(int number) {
    int copyOfInput = number;

    while (copyOfInput != 0) {
        if (copyOfInput % 10 > 1) {
            return false;
        }
        copyOfInput = copyOfInput / 10;
    }
    return true;
}

如果您注意到原始代码中有
If(length!=total)**;***{
破坏了您的If语句,因此它总是被触发

    int total = (count1 + count0);
    int length = number.length();

    if (length != total)
    {
        System.out.println("ERROR: Not a binary number.");
        System.out.println("Please enter a binary number.");
        number = scan.next();
    }
我的建议是使用一个布尔函数来检查它是否是二进制的,如下所示

public static boolean isBinary(int number) {
    int copyOfInput = number;

    while (copyOfInput != 0) {
        if (copyOfInput % 10 > 1) {
            return false;
        }
        copyOfInput = copyOfInput / 10;
    }
    return true;
}

尝试以下代码。首先,可以使用scan.nextLine()方法获取用户的输入。 第二,对于0和1,不需要分别使用两个while循环。 最后,如果有错误的输入,如2,3或其他。它应该无限循环

import java.util.Scanner;


public class NewClass {

public static void main(String [] args) {

    Scanner scan = new Scanner(System.in);

    int i = 0, count1 = 0, count0 = 0;
    String number;

    char number1 = '0';
    char number2 = '1';

    int total;

    while(true){
        System.out.println("Please enter a binary number.");
        number = scan.nextLine();

        char [] charArray = number.toCharArray();

        while(i < charArray.length){

            if(charArray[i] == number1){
                count0++;
            } else if(charArray[i] == number2) {
                count1++;
            }
            i++;
        }

        total = count0 + count1;

        if(charArray.length == total){

            System.out.println("There are " + count0 + " zeros in the binary number.");
            System.out.println("There are " + count1 + " ones in the binary number.");
            break;

        } else {
            System.out.println("ERROR: Not a binary number.");
        }


    }
  }
}
import java.util.Scanner;
公共类新类{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int i=0,count1=0,count0=0;
字符串编号;
字符编号1='0';
字符编号2='1';
整数合计;
while(true){
System.out.println(“请输入一个二进制数”);
number=scan.nextLine();
char[]charArray=number.toCharArray();
而(i
试试这段代码。首先,可以使用scan.nextLine()方法获取用户的输入。 第二,对于0和1,不需要分别使用两个while循环。 最后,如果有错误的输入,如2,3或其他。它应该无限循环

import java.util.Scanner;


public class NewClass {

public static void main(String [] args) {

    Scanner scan = new Scanner(System.in);

    int i = 0, count1 = 0, count0 = 0;
    String number;

    char number1 = '0';
    char number2 = '1';

    int total;

    while(true){
        System.out.println("Please enter a binary number.");
        number = scan.nextLine();

        char [] charArray = number.toCharArray();

        while(i < charArray.length){

            if(charArray[i] == number1){
                count0++;
            } else if(charArray[i] == number2) {
                count1++;
            }
            i++;
        }

        total = count0 + count1;

        if(charArray.length == total){

            System.out.println("There are " + count0 + " zeros in the binary number.");
            System.out.println("There are " + count1 + " ones in the binary number.");
            break;

        } else {
            System.out.println("ERROR: Not a binary number.");
        }


    }
  }
}
import java.util.Scanner;
公共类新类{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int i=0,count1=0,count0=0;
字符串编号;
字符编号1='0';
字符编号2='1';
整数合计;
while(true){
System.out.println(“请输入一个二进制数”);
number=scan.nextLine();
char[]charArray=number.toCharArray();
而(i
您能正确缩进并格式化此代码吗?为什么再次问相同的问题?
如果(长度!=total);
根据@jordanGS,不应该有分号您能正确缩进并格式化此代码吗?为什么再次问相同的问题?
如果(长度!=total);
根据@jordanGS的规定,不应该有分号。我提供了一个解决方案,不管怎样,他在
if(length!=total)的末尾有一个额外的
不起作用。这是一个非常糟糕的学校作业tho耸耸肩:/see Well我提供了一个解决方案不管怎样,他在
的末尾有一个额外的
如果(长度!=total)
不起作用。这是一个非常糟糕的学校作业tho耸耸肩:/