2016-11-08 62 views
0

我的程序的目的是接收一个二进制数(1和0)作为输入,验证它是一个二进制数,否认输入,如果它不是二进制数并继续提示用户,直到他们输入一个二进制数,并且然后输出该二进制数中有多少个零和零。如何显示无效用户输入的错误消息?

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

请输入一个二进制数。 二进制数中有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; 

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

你能正确缩进和格式化这段代码吗? –

+0

你为什么又问同样的问题? –

+0

'if(length!= total);'根据@jordanGS应该没有分号 –

回答

0

试试看看这个代码。首先,您可以使用scan.nextLine()方法获取用户的输入。 第二,你不需要使用两个while循环分别为零和1。 最后,如果有错误的输入,如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."); 
     } 


    } 
    } 
} 
0

我会检查该字符串只有使用正则表达式的0和1:

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

参见http://stackoverflow.com/questions/40476765/how-to-clarify-whether-the-users-input-was-一个二进制数 –

2

如果在你有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; 
} 
+0

请参阅http://stackoverflow.com/questions/40476765/how-to-clarify-whether-the-users-input-was-a-binary-number –

+0

嗯,我提供了一个解决方案,无论他有在'if(length!= total)'末尾加了一个额外的';',这是不起作用的。这是一个非常糟糕的学校任务*耸耸肩*:/ – JordanGS

+0

@ScaryWombat更新,谢谢:) – JordanGS

相关问题