0

I am trying to validate a user input, cuPerTerm > 12

I get the error message but the program continues and uses the invalid input to run

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 +" . ");

}
}

I need it to continue to re-ask until 12 or something greater is entered. and then continue the program.

4

5 回答 5

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();
    }
于 2013-10-21T22:04:34.197 回答
1

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

于 2013-10-21T22:05:17.167 回答
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. ");
    }
}
于 2013-10-21T22:07:39.237 回答
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);
于 2013-10-21T22:14:44.233 回答
0

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

    int val;
    do {
        val = in.nextInt();
    } while (val < 12);
于 2013-10-21T22:04:03.490 回答