2017-11-11 56 views
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); 
     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."); 

     } 

    } 
} 
+1

使用一个单独的函数,调用它来接受输入并检查数字是否为二进制,并返回布尔值 – frunkad

+1

顺便说一下,Java不是JavaScript。 JavaScript是完全不同的东西。 –

回答

0

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

所有那些通过假设它们为数字来检查用户输入(通过调用Scanner#nextIntScanner#nextFloat或...)是非常易碎的。如何确保用户不会输入任何错误?

这是更好地容纳用户输入在String变量,并使用正则表达式是更安全检查针对正在二进制整数(其具有为至多32位如在许多语言如Java定义):

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

希望这会有所帮助。

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


} 
} 
} 
+0

我不认为你需要使用“一”和东西,简单的休息会工作 – frunkad

+0

是的,这就是对的。 – Karan

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

    } 
} 

} 
} 

一个简单的回归可以结束程序,然后有:)

+0

谢谢。我不知道这很简单。但仍然有一个问题。当整数是二进制时它正在循环,当程序不是二进制时程序结束。所以,我需要弄清楚它的反面。 – TheBlues

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

    } 

} 

} 


} 

你只需要直到你得到的二进制数使用循环。 希望它有帮助。

相关问题