を使用して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');
}
}