2014-10-06 44 views
-1

我的教授被派去寫一個素數「finder」。如果輸入的數字是素數或偶數,則顯示該數字,然後顯示下一個素數。他希望我們在輸入錯誤的輸入時給出錯誤信息。我認爲負整數部分很簡單,但我無法弄清楚字符輸入。或者如果角色不是數字。我將如何阻止非數字輸入?如何拒絕非數字字符的輸入?

此外,系統應該在三個連續的錯誤輸入中退出。我將如何重置櫃檯?我編寫程序的方式,如果用戶犯了兩個錯誤,但接下來的錯誤是可以接受的,那麼就犯另一個錯誤。 (因此不是連續的)。程序關閉。 這是我第一次編程課程,所以我不會理解它。任何幫助將不勝感激。

此外,我必須使用掃描儀和兩種方法。

/** 
* 
* @param n 
* @return 
*/ 
public static boolean isPrime(int n) { 

    for (int i = 2; i < n; i++) { 
     if (n % i == 0) { 
      return false; 
     } 
    } 

    return true; 
} 



public static int nextPrime(int n) { 
    n++; 
    isPrime(n); 
    while (isPrime(n) == false) { 
     n++; 
     isPrime(n); 
    } 
    int prime = n; 
    return prime; 
} 


/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    // TODO code application logic here 

    int answer = 2; 
    int counter = 1; 

    boolean playAgain = false; 

    Scanner input = new Scanner(System.in); 

    Scanner reader = new Scanner(System.in); 

    Scanner in = new Scanner(System.in); 

    do { 
     //ask for input 
     System.out.print("\nEnter the integer value-> "); 

     //input answer 
     int n = input.nextInt(); 

     { 
      //decide is negative 
      while (n < 0){ 
       //count counter 
       counter++; 
       //give error message 
       System.out.println("\nInvalid Input! Stike!"); 

       //ask for input 
       System.out.print("\nEnter the integer value-> "); 

       //input answer 
       n = input.nextInt(); 


      //decide is character 
      // if (n != '.'){ 
       //count counter 
       // counter++; 

       //give error message 
       // System.out.println("\nInvalid Input! Strike!"); 
      // } 

      //decide if count three errors 
      if (counter == 3){ 

      //display three errors message 
      System.out.println("Three Strikes! You're Out!"); 
      //close program 
      System.exit(0); 
     } 
      } 

     //decide if prime 
     if (isPrime(n)) { 

      //display prime answer 
      System.out.println(n + " Is Prime"); 

      //decide if even 
     } else { 

      //display even answer 
      System.out.println(n + " Is Even"); 

     } 

     //counter input 
     n++; 

     //while input is false 
     while (isPrime(n) == false) { 
      n++; 
     } 


     //display next prime 
     System.out.println(n + " Next prime"); 

     { 

      //ask if you want to continue 
      System.out.println("\nPlay Again?\n\nEnter 1)Yes or 2)No "); 
      //input answer 
      answer = in.nextInt(); 


      //if answer is 1)yes 
      if (answer == 1) { 
       playAgain = true; 

       //display play again and next input 
       System.out.println("\nPlay Again!"); 
      } 
      //if answer is no 
      if (answer == 2) { 
       playAgain = false; 
       System.out.println("\nGoodbye!"); 
       //close program 
       System.exit(0); 
      } 


     } 

     } 

    } while (playAgain != false); 
} 

}

+1

我同意,簡單的研究應該給你這個問題的答案。 – DreadHeadedDeveloper 2014-10-06 01:56:43

回答

0
import java.util.Scanner; 

public class SOQ5B 
{ 

    public static boolean isPrime(int n) { 

     for (int i = 2; i < n; i++) { 
     if (n % i == 0) { 
      return false; 
     } 
     } 

     return true; 
    } 


    public static int nextPrime(int n) { 
     n++; 
     isPrime(n); 
     while (isPrime(n) == false) { 
     n++; 
     isPrime(n); 
     } 
     int prime = n; 
     return prime; 
    } 


/** 
* @param args the command line arguments 
*/ 
    public static void main(String[] args) { 
    // TODO code application logic here 

     int answer; 
     int counter = 0; 
     int n; 

     boolean playAgain = true; 
     boolean isNum; 
     boolean isNum2; 
     boolean continuePermitted = true; 

     Scanner input = new Scanner(System.in); 

     String s; 

     do { 
     //ask for input 
     System.out.print("\nEnter the integer value-> "); 

     s = input.nextLine(); 


     isNum = true; 

     for(int i = 0; i < s.length(); i++) 
     { 

      if(!(s.charAt(i) >= '0' && s.charAt(i) <= '9')) 
      { 

       isNum = false; 

      } 

     } 

     if(isNum) 
     { 

      counter = 0; 

      n = Integer.parseInt(s); 

     //decide if prime 
      if (isPrime(n)) { 

      //display prime answer 
       System.out.println(n + " Is Prime"); 

      //decide if even 
      } 
      else { 

      //display even answer 
       System.out.println(n + " Is Even"); 

      } 

     //counter input 
      n++; 

     //while input is false 
      while (isPrime(n) == false) { 
       n++; 
      } 


     //display next prime 
      System.out.println(n + " Next prime"); 


      do 
      { 

       continuePermitted = true; 

      //ask if you want to continue 
       System.out.println("\nPlay Again?\n\nEnter 1)Yes or 2)No "); 
      //input answer 

       s = input.nextLine(); 

       isNum2 = true; 

       for(int i = 0; i < s.length(); i++) 
       { 

        if(!(s.charAt(i) >= '0' && s.charAt(i) <= '9')) 
        { 

        isNum2 = false; 

        } 

       } 


       if(isNum2) 
       { 

        answer = Integer.parseInt(s); 

       //if answer is 1)yes 
        if (answer == 1) { 
        playAgain = true; 

        //display play again and next input 
        System.out.println("\nPlay Again!"); 
        } 
       //if answer is no 
        if (answer == 2) { 
        playAgain = false; 
        System.out.println("\nGoodbye!"); 
        //close program 
        System.exit(0); 
        } 

       } 

       else 
       { 

        System.out.println("Incorrect response."); 
        continuePermitted = false; 

        //if answering the yes or no question incorrectly is part of the 3 strikes 
        //then uncomment the following lines of code 

        /* 
        counter++; 
        } 

        if(counter >= 3) 
        { 

        System.out.println("3 strikes you out"); 
        System.exit(0); 
        */ 

       } 
      }while(!continuePermitted); 


     } 

     else 
     { 

      System.out.print("\nIncorrect input. Number must be a positive integer.\n"); 
      counter++; 

     } 

     if(counter>=3) 
     { 

      System.out.println("3 strikes and you're out!"); 
      System.exit(0); 

     } 




     } while (playAgain != false); 
    } 
} 

未來,我建議您在將問題帶到這裏之前先在互聯網上研究您的問題。還有其他幾個地方可以很容易地回答你的問題。

現在至於你的實際問題,請注意我是如何在s = input.nextLine()那行更改你的代碼的?我在那裏做的是檢查字符串中的每個數字是否在0-9之間的任何數字。我不僅能夠檢查它們是否都是數字,但是我也能夠看到它們是否都是積極的,因爲你必須爲此付出負面的代價。除此之外,還有一個布爾值,它只在輸入是正數時才起作用。如果沒有,它會檢查3次以確保您的程序不會混亂。此外,我甚至提出了另一部分,允許3次罷工,如果回答是沒有問題算作罷工。如果還有其他問題,請問我會編輯我的答案。

+1

非常感謝您的耐心和協助。我花了幾個小時在網上尋找答案,我確信我偶然發現了一個答案。儘管你已經解釋了這些步驟,但對於像我這樣的初學者來說已經足夠了。 – Nikron69 2014-10-06 03:41:38

0

您正在試圖如果你在的地方數的輸入字符在這裏你會得到java.util.InputMismatchException

採取輸入使用掃描儀類

int n = input.nextInt(); 

你能做什麼就像

try { 
    int n = input.nextInt();  
} catch (InputMismatchException e) { 
    //handle the error scenario where the input is a character 
    System.out.println("Enter Valid Input"); 
}