如果输入无效而不是继续无效输入,如何让程序重复?

时间:2013-10-21 22:01:11

标签: java validation

我正在尝试验证用户输入,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或更大的输入。然后继续该计划。

5 个答案:

答案 0 :(得分:1)

添加此项以继续获取输入,直到满足您的条件:

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

循环检查输入条件并继续输入直到满意为止。

修改: - 初始化您的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();
    }

答案 1 :(得分:1)

您应该使用while循环,以便继续循环,直到cuPerTerm至少为12.请记住在cuPerTerm = in.nextInt();循环内再次使用while进行用户输入

答案 2 :(得分: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. ");
    }
}

答案 3 :(得分:1)

存在一些缺陷:简单地执行scanner.nextInt()将为您提供当前行的下一个整数。

如果用户输入testnextInt()会抛出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);

答案 4 :(得分:0)

我认为do-while循环最适合您的需求:

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