Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用“是”或“否”选项窗格的java循环_Java_Loops - Fatal编程技术网

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

使用“是”或“否”选项窗格的java循环,java,loops,Java,Loops,我的目标是从键盘上读取一项费用的描述,然后读取一项实际费用,然后询问是或否,如果是,则返回要求另一项费用描述和费用,同时添加费用数量和用户输入的总费用。如果选项为“否”,我知道该怎么办我只是在将“是”选项循环回开始时遇到了问题 这是我到目前为止得到的 public static double budgetAmount; public static int totalExpenses; public static String expenseDescription; public static i

我的目标是从键盘上读取一项费用的描述,然后读取一项实际费用,然后询问是或否,如果是,则返回要求另一项费用描述和费用,同时添加费用数量和用户输入的总费用。如果选项为“否”,我知道该怎么办我只是在将“是”选项循环回开始时遇到了问题

这是我到目前为止得到的

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);
} 

你的逻辑看起来是颠倒的:如果我按yes,那么我应该继续在列表中添加更多项。。。 因此,改变这一点:

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


你有没有看过你的代码,想过你在那里做什么?如果首先检查
答案
是否为是,在这种情况下,终止整个过程。然后你再检查一遍以执行循环?这对您有意义吗?为什么要执行System.exit(0);如果答案是肯定的。我以为你想继续循环这个案例。嗯,是的,我知道如果我点击是的,它就会退出。哈哈,我不知道如何将它循环回顶部…因此我在这里。如果你这么聪明,也许你可以告诉我怎么做:)Ohhhhhhhhhhh等等,然后System.out.println(“再来一次”);那一定要在那里吗?它的目的是什么?
if (answer == JOptionPane.NO_OPTION)
    System.exit(0);
else if (answer == JOptionPane.YES_OPTION)
    System.out.println("One more time");