2013-10-21 34 views
0

我想驗證用戶輸入,cuPerTerm> 12如果輸入無效而不是繼續輸入無效,您如何讓程序重複?

我得到的錯誤消息,但該程序繼續並使用無效輸入運行

package gradplanner; 

import java.util.Scanner; 

public class GradPlanner { 

int cuToComp; 
int cuPerTerm; 

public static void main(String[] args) { 

    final double COST = 2890.00; //flat-rate tuition rate charged per term 
    final int MONPERTERM = 6; //number of months per term 
    int cuToCompTotal = 0; 
    int numTerm; 
    int numMonToComp; 
    double tuition; 



     //prompt for user to input the number of CUs for each individual course remaining. 
    Scanner in = new Scanner(System.in); 
    System.out.print("Please enter the number of CUs for each individual course you have remaining, Entering a - number when finished. ");  
    int cuToComp = in.nextInt(); 



     //add all CUs from individual courses to find the Total number of CUs left to complete. 
    while (cuToComp > 0) 
    { 
     cuToCompTotal += cuToComp; 

     System.out.print("Please enter the number of CUs for each individual course you have remaining, Entering a - number when finished. "); 
     cuToComp = in.nextInt(); 
    } 

    System.out.println("The total number of CUs left is " + cuToCompTotal); 



     //prompt for user to input how many CUs they plan to take per term. 
    System.out.print("How many credit units do you intend to take per term? "); 
    int cuPerTerm = in.nextInt(); 

     if (cuPerTerm <12) //validate input - Undergraduate Students Must enroll in a minimum of 12 CUs per term 
     { 
      System.out.print("Undergraduate Students must enroll in a Minimum of 12 CUs per Term. "); 

     } 


     //Calculate the number of terms remaining, if a remain is present increase number of terms by 1. 
    numTerm = cuToCompTotal/cuPerTerm; 
     if (cuToCompTotal%cuPerTerm > 0) 
     { 
      numTerm = numTerm + 1; 
     } 
    System.out.println("The Number of Terms you have left is " + numTerm + " Terms. "); 



     //Calculate the number of Months left to complete 
    numMonToComp = numTerm * MONPERTERM; 
    System.out.println("Which is " + numMonToComp + " Months. "); 



     //calculate the tuition cost based on the number of terms left to complete. 
    tuition = numTerm * COST; 
    System.out.println("Your Total Tuition Cost is: " + "$" + tuition +" . "); 

} 
} 

我需要它不斷地重新問直到輸入12或更大的值。然後繼續該程序。

回答

1

你應該再次使用while循環,讓你繼續循環,直到cuPerTerm至少12記得拿用戶輸入cuPerTerm = in.nextInt();內部的while循環。

+0

我包含一個while循環,它詢問輸入,但知道它只是不斷詢問,即使它是有效的。我需要打破打破;或者其他的東西? –

+0

在'while'循環的迭代結束時,如果條件不再是'true',那麼循環將結束;沒有「休息」;必要。 – rgettman

+0

沒關係,它需要cuPerTerm <12包括12作爲一個有效的輸入我改變了它,所以有些理由是<=所以它不包括12它是說它是無效的 –

0

我認爲一個do-while循環將最適合您的需要:

int val; 
    do { 
     val = in.nextInt(); 
    } while (val < 12); 
1

添加此繼續獲取輸入,直到它滿足您的條件:

while(cuPerTerm <= 12){ 
//Ask use to provide input 
} 

它是簡單的while循環,檢查你的輸入條件並繼續輸入直到滿足。

編輯: - 初始化您cuPerTerm = 0

while(cuPerTerm <= 12) 
    { 
     System.out.print("Please enter the number of CUs for each individual course you have remaining, Entering a - number when finished. ");  
     int cuToComp = in.nextInt(); 
    } 
+0

我包括while循環,其要求輸入,但知道它只是不停地問即使它是有效的。我需要打破打破;或者其他的東西? –

+0

不要緊,它需要cuPerTerm <12以包括12作爲有效的輸入 –

+0

檢查我的編輯。它應該工作 – Prateek

1

這裏有一個簡單的解決方案:

int cuPerTerm = -1; // intialize to an invalid value 
while (cuPerTerm < 12) { 
    System.out.print("How many credit units do you intend to take per term? "); 
    int cuPerTerm = in.nextInt(); 

    if (cuPerTerm <12) { //validate input - Undergraduate Students Must enroll in a minimum of 12 CUs per term 

     System.out.print("Undergraduate Students must enroll in a Minimum of 12 CUs per Term. "); 
    } 
} 
1

也有陷阱:簡單做scanner.nextInt()會給你當前行的下一個整數。

如果用戶輸入test,nextInt()會拋出InputMismatchException,則必須處理。 int也不會被消耗

因此您必須在中間調用scanner.nextLine()以清除當前(不匹配)的結果。

總之是這樣的:

do{ 
    try 
     { 
     System.out.print("Enter number > 12: "); 
     System.out.flush(); 
     number = scanner.nextInt(); 
     if (number > 12) 
     done = true; 
    } 
    catch(InputMismatchException e) { 
     System.out.println("This is not a number"); 
     scanner.nextLine() //!Important! 
    } 
    }while(!done);