简体   繁体   English

如果发生错误,如何提示用户重新输入值,以及如何提供继续检查工资率或退出程序的选项?

[英]How to prompt the user to re-enter a value if a mistake was made and How to give an option to keep on checking pay rates or to exit the program.?

Java application that inputs one salesperson's items and their corresponding amount sold for last week, then calculates and displays that salesperson's earnings. Java应用程序输入一个销售人员的商品及其上周的相应销售额,然后计算并显示该销售人员的收入。

for (int item =1; item < 5; item++){

    System.out.printf("\nHow many of item %d have you sold: ", item);
    count=input.nextInt();

    if (item == 1)      
        sales = item1* count;

    if (item == 2)
        sales2 = item2 * count;

    if (item == 3)
        sales3 = item3 * count;

    if (item == 4)
    sales4 = item4 * count;
}

You could keep this looping until user inputs the correct amount.You can make use of a default value which the user enters to exit the loop. 您可以一直循环,直到用户输入正确的数量。您可以使用用户输入的默认值退出循环。 Based on my understanding: 根据我的理解:

while(true)
{
       System.out.printf("\nHow many of item %d have you sold: (Enter -999 to exit)", item);
        count=input.nextInt();
        if(count== -999) //Or some default value of your choice, exit the loop
        {
              System.out.printf("Program terminated");  
              break;
        }
       for (int item =1; item < 5; item++)
       {


        if (item == 1)

          sales = item1* count;

        if (item == 2)
      sales2 = item2 * count;

        if (item == 3)
             sales3 = item3 * count;

         if (item == 4)
        sales4 = item4 * count;
        }   

}

A while loop is better, so you can do something like this: 使用while循环更好,因此您可以执行以下操作:

While(true){     
    System.out.printf("\nHow many of item %d have you sold: ", item);
    count =input.nextInt();

    if(certain condition is reached)
      break;

    if (item == 1)
      sales = item1* count;
    else if (item == 2)
      sales2 = item2 * count;
    else if (item == 3)
      sales3 = item3 * count;
    else if (item == 4)
      sales4 = item4 * count;    
}

Then you can add a break statement somewhere within one of the control if conditions or else where within the loop to exit the loop. 然后,你可以添加一个break中的控制的一个地方声明if内环路条件或其他地方退出循环。 For example, you can use a counter to count how many times they have entered values and then break out of the loop when that is reached. 例如,您可以使用计数器来计算它们输入值的次数,然后在达到该值时退出循环。 It is all up to you and your requirements. 这完全取决于您和您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户输入参数之外的数字,如何循环提示用户重新输入数字 - How to loop a prompt for a user to re-enter a number if they input a number outside of the parameters 当要求用户在Java中循环输入一些字符串时,如何仅保留文本打印一次 - How to keep Text print only once when asking a user to re-enter some string in loop in Java 我如何要求用户重新输入他们的选择? - How can I ask the user to re-enter their choice? 如何在用户登录失败时重新进入 - How to make it re-enter when user login fail 如果值不正确,使用户重新输入值 - make a user re-enter values if value not correct 在第二个for循环中,如何使用户重新输入考试分数(如果为负)? - How to do I make the user re-enter the exam scores if its negative, in the second for loop? 试图提示用户重新进入catch块,但是catch块终止了吗? - Trying to prompt the user to re-enter from the catch block, but the catch block terminates? Java-异常处理-如何重新输入无效输入 - Java - Exception handling - How to re-enter invalid input 如果值无效,如何提示用户重新输入其输入值? - how do i prompt a user to re enter their input value, if the value is invalid? 退出并重新进入后,应用程序的行为会有所不同 - App behaves different after exit and re-enter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM