我正在使用javax.swing.JOptionPane
。
我需要用户输入产品编号,收入和费用。
我需要验证这些信息,以确保收入介于0
和20000
之间,并确认费用介于1500
之间和10000
。我需要确保如果他们输入无效收入或费用,则会提示他们,并且不允许程序继续。
该计划需要能够确定是否存在净利润,亏损或收支平衡。
用户必须可以选择输入多条记录。此外,我需要计算用户输入信息的次数。
我觉得我能够淘汰很大一部分代码。
当用户输入无效的收入或费用时,它会循环显示消息,并且不会再次返回输入值的功能。
我也不确定如何让用户输入“Y”来重新循环整个程序。
有人可以借给我一些帮助吗?
/**
* The program will output the Product Number, Revenue, Expenses, as well as the Net Income
*/
import javax.swing.JOptionPane;
import java.io.*; // Access System.out
import java.util.Scanner;
public class RevenueJopt
{
public static void main(String[] args)
{
// Declarations
double finalValue;
char repeat;
int counter = 1;
String input;
Scanner keyboard = new Scanner(System.in);
// Do Loop to run
do{
// Advise the user the conditions that have to be met for inputs
JOptionPane.showMessageDialog(null,"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.");
// Ask user the values of the variables
String response = JOptionPane.showInputDialog(null, "Enter in a Product Number(or -1 to END)");
String response1 = JOptionPane.showInputDialog(null, "Enter the Revenue?");
String response2 = JOptionPane.showInputDialog(null, "Enter the Expenses?");
// Read in values
int productNumber = Integer.parseInt(response);
float revenue = Float.parseFloat(response1);
float expenses = Float.parseFloat(response2);
//While loop to Validate Information
while(revenue < 0 || revenue > 20000 || expenses < 1500 || expenses > 10000) {
JOptionPane.showMessageDialog(null,"You have entered in either an invalid revenue or expense. Please enter in valid numbers.");
{
JOptionPane.showMessageDialog(null,"Here is the product number you entered: " + productNumber + "."
+ "\nHere is the revenue you entered: " + revenue + "."
+ "\nHere are the expenses you entered: " + expenses + ".");
JOptionPane.showMessageDialog(null,"Enter in a Product Number (or-1 to END)"
+ "\nEnter the Revenue"
+ "\nEnter the Expenses");
//When this part runs, it goes into an infinite cycle. I am not sure how to break free of this.
counter++;
//calculates final value
}
}
finalValue = revenue - expenses;
// Calculates final value and displays as net profit, loss or break even.
if (finalValue > 0) {
JOptionPane.showMessageDialog(null, "You made a profit. Your net income is: "+finalValue);
} else if (finalValue == 0) {
JOptionPane.showMessageDialog(null, "You broke even. Your revenue was "+ revenue +" your expenses were " +expenses);
} else if (finalValue < 0) {
JOptionPane.showMessageDialog(null,"You have not made any profit. Your net loss is: "+finalValue);
}
JOptionPane.showMessageDialog(null,"Number of records: " +counter);
//validate user input
JOptionPane.showMessageDialog(null,"Would you like to input more records?");
String response3 = JOptionPane.showInputDialog(null, "Enter 'Y' for yes or 'N' for no.");
// I am not sure how to hold the value "Y" to make the loop keep repeating
input = keyboard.nextLine();
repeat = input.charAt(0);
counter++;
}
while(repeat == 'Y' || repeat == 'y');
}
}
答案 0 :(得分:0)
替换
input = keyboard.nextLine();
repeat = input.charAt(0);
与
repeat = response3.charAt(0);
获取输入到输入对话框中的字符串的第一个字符。
但是,如果用户在对话框中没有输入任何内容,则会抛出StringIndexOutOfBoundsException
,因此您必须确定该案例的默认值:
repeat = response3.isEmpty() ? 'n' : response3.charAt(0);
从System.in
读取基本上是用于CLI应用程序。
同时循环检查“验证信息”。如果用户输入无效值,将无限期地通知他,无法输入正确的值。