我的程序一直在跳过忽略我的布尔值的条件?

时间:2016-12-20 18:15:19

标签: java boolean

我的代码一直在跳到提示而不是循环回来,我不明白为什么。我仔细阅读了所有内容,一切似乎都在起作用,但是当我尝试运行它时,我可以订购一个项目,甚至不允许我决定是否要订购其他任何东西。我是编码的初学者,所以我可能会遗漏一些明显的东西。

import java.util.*;

public class OnlineOrdersFallonBryan
{
   public double value = 0;
   public double cost = 0;
   Scanner in = new Scanner(System.in);
   public void main ()
   {
        System.out.println("Welcome to Vegi-Man's Online Ordering!");
        boolean rightorWrong = true;
        while (rightorWrong == true)
        {
        System.out.println("Menu:");
        System.out.println("1. Vegi Burger                $3.00");
        System.out.println("2. Chicken-less Chicken Wings $4.00");
        System.out.println("3. Fried Vegis                $2.00");
        System.out.println("4. Vegan Milkshake            $1.50");
        System.out.println("5. Double Vegi Burger         $5.00");
        System.out.println("6. Onion Rings                $2.00");
        System.out.println("7. Fried Vegi of choice       $2.00");
        System.out.println("8. Vegan Icecream             $1.50");
        System.out.println("9. Vegan Chocolate Lava Cake  $4.50");
        System.out.println("10. Vegan Cheese or Sauces    $0.50");
        System.out.println("");
        System.out.println("Select a menu item: ");
        double menuchoice = in.nextDouble();
        if (menuchoice == 1)
        {
            value = cost + 3;
            System.out.println();
            System.out.println("Your current total is $" + cost + ".");
            System.out.println("Options: ");
            System.out.println("1. Order another item.");
            System.out.println("2. Checkout.");
            System.out.println();
            System.out.println("Select an option: ");
            String answer1 = in.nextLine ();
            rightorWrong = answer1.equalsIgnoreCase("yes");
        }
        if (menuchoice == 2)
        {
            value = cost + 4;
            System.out.println();
            System.out.println("Your current total is $" + cost + ".");
            System.out.println("Options: ");
            System.out.println("1. Order another item.");
            System.out.println("2. Checkout.");
            System.out.println();
            System.out.println("Select an option: ");
            String answer1 = in.nextLine ();
            rightorWrong = answer1.equalsIgnoreCase("yes");
        }
        if (menuchoice == 3)
        {
            value = cost + 2;
            System.out.println();
            System.out.println("Your current total is $" + cost + ".");
            System.out.println("Options: ");
            System.out.println("1. Order another item.");
            System.out.println("2. Checkout.");
            System.out.println();
            System.out.println("Select an option: ");
            String answer1 = in.nextLine ();
            rightorWrong = answer1.equalsIgnoreCase("yes");
        }
            if (menuchoice == 4)
    {
            value = cost + 1.5;
            System.out.println();
            System.out.println("Your current total is $" + cost + ".");
            System.out.println("Options: ");
            System.out.println("1. Order another item.");
            System.out.println("2. Checkout.");
            System.out.println();
            System.out.println("Select an option: ");
            String answer1 = in.nextLine ();
            rightorWrong = answer1.equalsIgnoreCase("yes");
        }
        if (menuchoice == 5)
        {
        value = cost + 5;
        System.out.println();
        System.out.println("Your current total is $" + cost + ".");
        System.out.println("Options: ");
        System.out.println("1. Order another item.");
        System.out.println("2. Checkout.");
        System.out.println();
        System.out.println("Select an option: ");
        String answer1 = in.nextLine ();
        rightorWrong = answer1.equalsIgnoreCase("yes");
    }
        if (menuchoice == 6)
    {
            value = cost + 2;
            System.out.println();
            System.out.println("Your current total is $" + cost + ".");
            System.out.println("Options: ");
            System.out.println("1. Order another item.");
            System.out.println("2. Checkout.");
            System.out.println();
            System.out.println("Select an option: ");
            String answer1 = in.nextLine ();
            rightorWrong = answer1.equalsIgnoreCase("yes");
        }
        if (menuchoice == 7)
        {
            value = cost + 2;
            System.out.println();
            System.out.println("Your current total is $" + cost + ".");
            System.out.println("Options: ");
            System.out.println("1. Order another item.");
            System.out.println("2. Checkout.");
            System.out.println();
            System.out.println("Select an option: ");
            String answer1 = in.nextLine ();
            rightorWrong = answer1.equalsIgnoreCase("yes");
        }
        if (menuchoice == 8)
        {
            value = cost + 1.5;
            System.out.println();
            System.out.println("Your current total is $" + cost + ".");
            System.out.println("Options: ");
            System.out.println("1. Order another item.");
            System.out.println("2. Checkout.");
            System.out.println();
            System.out.println("Select an option: ");
            String answer1 = in.nextLine ();
            rightorWrong = answer1.equalsIgnoreCase("yes");
            }
        if (menuchoice == 9)
        {
            value = cost + 4.5;
            System.out.println();
            System.out.println("Your current total is $" + cost + ".");
            System.out.println("Options: ");
            System.out.println("1. Order another item.");
            System.out.println("2. Checkout.");
            System.out.println();
            System.out.println("Select an option: ");
            String answer1 = in.nextLine ();
            rightorWrong = answer1.equalsIgnoreCase("yes");
        }
        if (menuchoice == 10)
        {
            value = cost + 0.5;
            System.out.println();
            System.out.println("Your current total is $" + cost + ".");
            System.out.println("Options: ");
            System.out.println("1. Order another item.");
            System.out.println("2. Checkout.");
            System.out.println();
            System.out.println("Select an option: ");
            String answer1 = in.nextLine ();
            rightorWrong = answer1.equalsIgnoreCase("yes");
        }
    }
    if (rightorWrong == false)
    {
        response1();
    }
    }
   public void response1()
    {
        System.out.println("Subtotal (with tax): $" + value * 1.0825);
        System.out.println("Tip: $");
        double tip = in.nextDouble ();
        double total = value + tip;
        System.out.println("Final Total: $" + total);
    }
}

1 个答案:

答案 0 :(得分:0)

here

描述了您的问题的解决方案和解释

当我们处于此状态时,您不应该检查答案是否为“是”而是1或2,因为选项是1.订购另一个项目。和2.结帐。

您应该使用debbuger或至少使用System.out.println()来查看程序中相关变量的值。