我正在尝试验证用户输入,但我无法让它工作。
用户必须输入介于 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');
{
}
}
}