2017-11-11 54 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"); 

    } 

} 

} 


} 

你只需要直到你得到的二進制數使用循環。 希望它有幫助。

相關問題