java循环使用是或否选项窗格

时间:2016-04-27 00:52:19

标签: java loops

我的目标是从键盘上读取费用的描述,然后是实际费用,然后询问是或否,如果是,它会循环回去询问另一个费用描述和费用,同时增加费用数量和总用户输入。我知道如果选项不是该怎么办我只是在将yes选项循环回到开始时遇到问题

继承人到目前为止我所拥有的

public static double budgetAmount;
public static int totalExpenses;
public static String expenseDescription;
public static int expense;
public static boolean proceed;
public static String value;
public static double runningTotal;
public static int answer;

public static void main(String[] args) {

    String bdgtAmnt1 = JOptionPane.showInputDialog("Enter Budget Amount:");
    budgetAmount = Double.parseDouble(bdgtAmnt1);
    do {  

    expenseDescription = JOptionPane.showInputDialog("Enter the description of the Expense in 32 letters");
    String expense1 = JOptionPane.showInputDialog("Enter Expense Amount:");
    budgetAmount = Double.parseDouble(expense1);
    if (answer == JOptionPane.YES_OPTION) runningTotal += expense;
    totalExpenses++;

    answer =  JOptionPane.showConfirmDialog(null, "Are there any more items to be entered?", "End Expense Report", JOptionPane.YES_NO_OPTION);

    if (answer == JOptionPane.YES_OPTION)
        System.exit(0);
    else if (answer == JOptionPane.NO_OPTION)
        System.out.println("One more time");

    } while(answer == JOptionPane.YES_OPTION);
} 

1 个答案:

答案 0 :(得分:1)

你的逻辑看起来倒错了:如果我按是,那么我应该继续在列表中添加更多项目...... 所以改变这个:

if (answer == JOptionPane.YES_OPTION)
    System.exit(0);
else if (answer == JOptionPane.NO_OPTION)
    System.out.println("One more time");

有:

if (answer == JOptionPane.NO_OPTION)
    System.exit(0);
else if (answer == JOptionPane.YES_OPTION)
    System.out.println("One more time");