如何退出循环?

时间:2014-07-10 12:12:15

标签: java loops

这是我班上的作业。它是一个自动订购系统。我还是Java的新手,所以一切都不一定就是点击。我认为大多数事情在很大程度上都有效,但我遇到的主要问题是制作循环本身并为它做出退出。

import java.util.Scanner;
public class Metal {
public static void main (String[] args) {
    double PRICE1 = 5.00;
    double PRICE2 = 7.00;
    double PRICE3 = 3.50;
    double PRICE4 = 0.75;
    double TAX = 0.05;

    System.out.println ("Metal Down Your Mouth Menu");
    System.out.println ();
    System.out.println ();
    System.out.println ();
    System.out.println ("1. Black Sabbath Burgers (Hamburgers With Black Buns) " + PRICE1);
    System.out.println ("2. Rack of Lamb of God (Rack of Lamb) " + PRICE2);
    System.out.println ("3. Texas Hippie Collar Greens (Collar Greens) " + PRICE3);
    System.out.println ("4. Pepsi " + PRICE4);
    System.out.println ("Press any other button to stop ordering.");


    Scanner userInput = new Scanner(System.in);
    int itemnumber = 0;
    while (itemnumber < 1 || itemnumber > 4) {
    System.out.print("Enter the item number of the item you wish to order: ");
    itemnumber = userInput.nextInt();
    }

    System.out.print ("How many?");
    int amount = userInput.nextInt();

        double subtotal = 0;
        double total = 0;
        double price = 0;
        double taxes = 0;
        String name = "";

    switch (itemnumber){
        case 1: name = "Black Sabbath Burgers"; price = PRICE1; break;
        case 2: name = "Rack of Lamb of God"; price = PRICE2; break;
        case 3: name = "Texas Hippie Collar Greens"; price = PRICE3; break;
        case 4: name = "Pepsi"; price = PRICE4; break;
    }
        subtotal = price * amount;
        total = subtotal + total;   
        System.out.print("Price for items: " + subtotal);
        System.out.print("Price Total: " + total);
}

1 个答案:

答案 0 :(得分:1)

这是我第一次在这个网站上发帖,但我想我发现了你的问题。有两个大的错误,用箭头表示:

while (itemnumber >= 1 || <-- itemnumber <= 4) {
System.out.print("Enter the item number of the item you wish to order: ");
itemnumber = userInput.nextInt();
} <--

1)这应该是&#39;&amp;&amp;&#39;不是&#39; ||&#39;。你希望它在范围内。现在它读取的数字必须大于或等于1或小于或等于4,这都是整数。

2)你过早地关闭你的循环。您的代码现在所做的事情(在&amp;&amp;开关之后)需要数字1-4并且不断重复&#34;输入项目编号....&#34;直到你把数字放在范围内,然后继续。

修复:有几种方法可以解决这个问题。因此,我的解决方案将在以后解释:

import java.util.Scanner;
public class Metal {
public static void main (String[] args) {
double PRICE1 = 5.00;
double PRICE2 = 7.00;
double PRICE3 = 3.50;
double PRICE4 = 0.75;
double TAX = 0.05;

System.out.println ("Metal Down Your Mouth Menu");
System.out.println ();
System.out.println ();
System.out.println ();
System.out.println ("1. Black Sabbath Burgers (Hamburgers With Black Buns) " + PRICE1);
System.out.println ("2. Rack of Lamb of God (Rack of Lamb) " + PRICE2);
System.out.println ("3. Texas Hippie Collar Greens (Collar Greens) " + PRICE3);
System.out.println ("4. Pepsi " + PRICE4);
System.out.println ("Press any other button to stop ordering.");


Scanner userInput = new Scanner(System.in);
int itemnumber = 0;
System.out.print("Enter the item number of the item you wish to order: ");
itemnumber = userInput.nextInt();
double total = 0;
while (itemnumber >= 1 && itemnumber <= 4) {


System.out.print ("How many?");
int amount = userInput.nextInt();

    double subtotal = 0;
    double price = 0;
    double taxes = 0;
    String name = "";

switch (itemnumber)
{
    case 1: name = "Black Sabbath Burgers"; price = PRICE1; break;
    case 2: name = "Rack of Lamb of God"; price = PRICE2; break;
    case 3: name = "Texas Hippie Collar Greens"; price = PRICE3; break;
    case 4: name = "Pepsi"; price = PRICE4; break;
}
    subtotal = price * amount;
    total = subtotal + total;   
    System.out.print("Price for items: " + subtotal);
    System.out.print("Enter the item number of the item you wish to order: ");
    itemnumber = userInput.nextInt();
}
System.out.print("Price Total: " + total);        
}
}

说明:从本质上讲,你有90%的喜欢它。我提到了上面提到的&#39;}&#39;到最后:

     itemnumber = userInput.nextInt();
  } <--

这样,它会遍历此代码,直到用户结束。 此外,您的循环不需要太多修复。它可以与&amp;&amp;和固定。但是,您必须在循环之前放置该顶行。

System.out.print("Enter the item number of the item you wish to order: ");
itemnumber = userInput.nextInt();

然后你在循环的末尾放置相同的行来重置itemnumber。你的循环做的是如果itemnumber在1和4之间,它执行以下代码。否则,它会停止。通过在进入循环之前进行检查,可以设置itemnumber,以便循环检查。然后你将下一个输入放在循环的末尾,这样你的程序就可以完成它的第一次执行,然后继续下一个。

此外,您应该移动变量&quot; total&#39;如上所述,不在循环中。如果你继续循环并将其重置为0,那么你的总数将每次输出0。最好保持循环的完全创建,以及循环内的修改。

小提示,请使用System.out.println();而不是System.out.print();它将输出放在自己的线上。看起来更好一点。

我认为这涵盖了它。如果您想要更多解释,我非常乐意将其提供给您。一旦你习惯了,Java就很有趣了。这需要时间。 :d