0

我正在使用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');
    }
}
4

1 回答 1

0

代替

input = keyboard.nextLine();
repeat = input.charAt(0);

repeat = response3.charAt(0);

获取输入到输入对话框的字符串的第一个字符。

StringIndexOutOfBoundsException但是,如果用户在对话框中没有输入任何内容,这将引发一个错误,因此您必须确定该情况的默认值:

repeat = response3.isEmpty() ? 'n' : response3.charAt(0);

读取System.in基本上是针对 CLI 应用程序的。

还要检查“验证信息”while 循环。如果用户输入了无效值,他将被无限期地告知这一点,没有机会输入正确的值。

于 2013-09-19T20:41:02.617 回答