0

我正在尝试验证用户输入,但我无法让它工作。

用户必须输入介于 0-20,000 之间的收入金额,但仅此而已。

此外,用户必须输入介于 1500-10000 之间的费用,但不能超过或低于该值。

我也在尝试循环代码。我在询问用户是否有他们想要输入的其他记录,我正在计算记录已经完成了多少次。

谁能告诉我我做错了什么并指出我正确的方向?

这是我到目前为止所拥有的:

import javax.swing.JOptionPane;
import java.io.*;          // Access System.out
import java.util.Scanner;

public class RevenueScan
{
   public static void main(String[] args)
   {
      // Declarations
      Scanner in = new Scanner(System.in);
      int productNumber;
      float revenue;
      float expenses;
      double finalValue;
      char repeat;
      int counter = 0;
      String input;
      Scanner keyboard = new Scanner(System.in);

      // Do Loop to run    
      do
      {
         // Pop up to advise the user the conditions that have to be met for inputs
         System.out.println("Please ensure that your revenue is between 0 to 20,000.00 dollars."
                            + "\nPlease ensure that your expenses are between 1,500.000 to 10,000.00 dollars.");

         // Pop up to ask the user the values of the variables
         System.out.println("Enter in a Product Number (or-1 to END)"
                            + "\nEnter the Revenue"
                            + "\nEnter the Expenses");
         // Read in values  

         productNumber = in.nextInt();
         revenue = in.nextFloat();
         expenses = in.nextFloat();
         //States the values entered by user
         while (revenue < 0 || revenue > 20000 || expenses < 1500 || expenses > 10000);
         {
            System.out.println("You have entered in either an invalid revenue or expense. Please enter in valid numbers.");
            {
               System.out.println("Here is the product number you entered: " + productNumber + "."
                                  + "\nHere is the revenue you entered: " + revenue + "."
                                  + "\nHere are the expenses you entered: " + expenses + ".");
               counter++;
               //calculates final value
            }
         }
         finalValue = revenue - expenses;
         // Calculates final value and displays as net profit, loss or break even. 
         if (finalValue > 0)
         {
            System.out.println("You made a profit. Your net income is: " + finalValue);
         }
         else
            if (finalValue == 0)
            {
               System.out.println("You broke even. Your revenue was " + revenue + " your expenses were " + expenses);
            }
            else
               if (finalValue < 0)
               {
                  System.out.println("You have not made any profit. Your net loss is: " + finalValue);
               }
         System.out.println("Number of records: " + counter);
         //validate user input   
         System.out.println("Would you like to input more records?");
         System.out.println("Enter 'Y' for yes or 'N' for no.");
         input = keyboard.nextLine();
         repeat = input.charAt(0);
      }
      while (repeat == 'Y' || repeat == 'y');
      {
      }
   }
}
4

1 回答 1

0

;在 while-statement 之后有一个,它不应该在那里,否则 while-loop 是空的,而不是包含它后面的块。

while (revenue < 0 || revenue > 20000 || expenses < 1500 || expenses > 10000)
{
   System.out.println("You have entered in either an invalid revenue or expense. Please enter in valid numbers.");
   {
      System.out.println("Here is the product number you entered: " + productNumber + "."
                         + "\nHere is the revenue you entered: " + revenue + "."
                         + "\nHere are the expenses you entered: " + expenses + ".");
      counter++;
   }
}

但是一旦你解决了这个问题,上面的块就会一直循环,因为在那个循环中没有一个值可以改变。

此外,那些内括号{}有点毫无意义

我推荐这个:

if (revenue < 0 || revenue > 20000 || expenses < 1500 || expenses > 10000)
{
   System.out.println("You have entered in either an invalid revenue or expense. Please enter in valid numbers.");
   System.out.println("Here is the product number you entered: " + productNumber + "."
                      + "\nHere is the revenue you entered: " + revenue + "."
                      + "\nHere are the expenses you entered: " + expenses + ".");
   counter++;
   continue; // go to the start of the do-while loop
}

然后你还必须改变:

char repeat;

至:

char repeat = 'Y';

否则它不会编译,因为continue仍然会触发条件检查,并且repeat不会在第一次初始化,Java 不允许这样做。

如果您想坚持使用 while-loop,请在其中放入类似以下行的内容:

// tell user to input values again
System.out.println("Enter in a Product Number (or-1 to END)"
                   + "\nEnter the Revenue"
                   + "\nEnter the Expenses");
// read in values
productNumber = in.nextInt();
revenue = in.nextFloat();
expenses = in.nextFloat();

这将允许用户输入新值,直到满足条件。

do- while循环的格式是

do { ... }
while (...);

不是:

do { ... }
while (...) { ... }

所以改变:

while (repeat == 'Y' || repeat == 'y');
{
}

至:

while (repeat == 'Y' || repeat == 'y');
于 2013-09-15T19:47:31.507 回答