我正在尝试验证用户输入,但我无法让它工作。
用户必须输入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');
{
}
}
}
答案 0 :(得分:0)
在while语句后面有;
,它不应该在那里,否则while循环是空的,而不是包含它后面的块。
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循环,请在其中添加类似这样的内容:
// 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');