2016-11-08 1 views
0

내 프로그램의 목적은 이진수 (1과 0)를 입력으로 받고, 이진수인지 확인하고, 이진수가 아닌 경우 입력을 거부하고 이진수를 입력 할 때까지 사용자에게 계속 묻습니다. 그런 다음 이진수에 얼마나 많은 1과 0이 출력되는지 확인하십시오.잘못된 사용자 입력에 대한 오류 메시지를 표시하는 방법은 무엇입니까?

여기에 내가 겪고있는 문제가있다. 적절한 숫자의 이진수를 입력 할 때도 내 프로그램에서 숫자와 숫자가 몇 개인 지 출력하지만 내 출력에는 여전히 "이진수가 아닙니다."라는 메시지가 표시됩니다. 예를 들어, 입력 한 내용이 10001이면 출력은 다음과 같습니다.

이진수를 입력하십시오. 이진수에는 2가 있습니다. 2 진수에는 3 개의 0이 있습니다. 오류 : 이진수가 아닙니다. 이진수를 입력하십시오.

코드에서 내가 뭘 잘못 했습니까?

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

@jordanGS에 따라'if (length! = total);'는 세미콜론이 없어야합니다. –

답변

0

이 코드를 사용해보십시오. 먼저, scan.nextLine() 메서드를 사용하여 사용자 입력을 얻을 수 있습니다. 둘째, 0과 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이 있는지 확인합니다 : 당신은 당신이 if (length != total)**;**{했다 원래 코드에서 알 경우 ;이 if 문을 아프게

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- a-binary-number –

2

을 그래서 항상 시작되었습니다.

내 제안은 부울 함수를 사용하여 다음과 같은 이진인지 확인하는 것입니다.

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 (길이! = 합계) '끝에 여분의';'가 있습니다. 그건 정말 나쁜 학교 임무입니다 * 으s : */ – JordanGS

+0

@ SkaryWombat 업데이트, 감사합니다 :) – JordanGS

관련 문제