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

Posted

技术标签:

【中文标题】如果输入无效而不是继续输入无效,如何让程序重复?【英文标题】:How do you get your program to repeat if input is invalid instead of continuing with invalid input? 【发布时间】:2013-10-21 22:01:10 【问题描述】:

我正在尝试验证用户输入,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。请记住在while 循环中使用cuPerTerm = in.nextInt(); 再次获取用户输入。

【讨论】:

我包含了一个 while 循环,它要求输入,但知道它只是不断询问,即使它是有效的。我需要输入break吗?还是什么? while循环的一次迭代结束,如果条件不再是true,则循环结束;不需要break; 没关系它需要是 cuPerTerm 【参考方案2】:

这是一个简单的解决方案:

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】:

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

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();
    

【讨论】:

我包含了一个 while 循环,它要求输入,但知道它只是不断询问,即使它是有效的。我需要输入break吗?还是什么? 没关系它需要是 cuPerTerm @JessicaRush 在你的问题中检查第一行它说cuPerTerm &gt; 12 意味着有效输入应该大于12(不包括12)因此我的条件无效输入。【参考方案4】:

有一些陷阱:简单地做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);

【讨论】:

【参考方案5】:

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

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

【讨论】:

以上是关于如果输入无效而不是继续输入无效,如何让程序重复?的主要内容,如果未能解决你的问题,请参考以下文章

我怎样才能让这个重复而不被卡在无效输入上?

检查用户输入是不是为 NaN [重复]

使用拆分命令时如何发送无效的用户输入

在输入cmd里面输入mklink /X winsxs.link winsxs.moved怎么提示无效开关“x"

如果 URL 在播放框架中无效,则重定向到错误页面。 1.25?

如何区分 datetime-local 类型的部分填充或无效输入与空输入?