2013-10-21 33 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);