0

を使用してjavax.swing.JOptionPaneいます。

  • ユーザーに製品番号、収益、および費用を入力してもらう必要があります。

  • 情報を検証して、収益が と の間020000あることを確認し、費用が と の間にあることを確認する必要が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 に答える