1

我的代码应该模拟类似于自动售货机的东西。但是当我输入一个不是我的选项之一的价格时出现了问题,例如0.82程序仍然运行。我如何让它只接受我的一个选项?

import java.util.Scanner;

public class VendingMachine 
{
    public static void main (String[] args)
    {
        double price;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Choose your price. Your options are: ");
        double i;
        for (i=0.25; i<=1.25; i+=0.25)
             System.out.printf("$%.2f\n", i );

        System.out.println("Enter your selection now: ");
        price=keyboard.nextDouble();
        System.out.printf("You chose the $%.2f option. ",price);    
        double deposit;

        if (price<=1.00) {
            System.out.println("Please insert 1 dollar. *This machine only accepts Loonies*");
            deposit=1;
        } else {
            System.out.println("Please insert 2 dollars.*This machine only accepts Loonies*");
            deposit=2;
        }

        System.out.println("Please press 'Enter' to simulate inserting money. ");
        new Scanner(System.in).nextLine();

        double change;
        change = deposit-price;
        System.out.printf("Your change is $%.2f\n",change);
    }
}

我尝试过这样的事情,但它不起作用。做这个的最好方式是什么。

if (price==i)
    System.out.println("You entered " + price);
else {
    System.out.println("Invalide choice. Please try again.")
    System.exit(0);
}

如果您发现它更容易阅读,这里有一张图片。

4

4 回答 4

3

您可以使用某种循环(whiledo-whilefor),它将继续执行代码,直到满足(或不满足)条件。

这是一个例子:

do {
   code line 1;
   code line 2;
   code line 3;
   ...
} while(yourCondition);

如果yourCondition满足(yourCondition == true),代码将返回code line 1(将执行do和之间的代码块while),一旦条件不满足(yourCondition == false),它将停止。yourCondition可以是任何返回真/假结果的表达式 ( boolean),例如2+2==4.

如果您想在yourCondition不满足的情况下继续循环,您可以!在表达式之前添加一个,这将评估与您的booleanlike this相反的值(!yourCondition)

现在,如果您了解它的工作原理,您可以轻松地将其应用到您的代码中。

于 2013-06-12T23:36:40.660 回答
2

如果您希望用户仅输入您显示的价格,我建议以下内容,您应根据您的确切需求进行编辑。

    //given you an open scanner
    boolean isCorrectPrice = false;

    System.out.println("enter price");
    price = in.nextDouble();
    while(!isCorrectPrice)
    {
       if(price%0.25==0 && price<=1.25 && price>0)
       {
          System.out.println("you entered "+price);
          IsCorrectPrice = true;
          continue;
       }
       System.out.println("incorrect price, re-enter ");
       price = in.nextDouble();
    }
   //your code after user enters correct price

这将进行检查。如果您的价格发生变化,您所要做的就是更改最高价格,前提是它仍然可以被0.25整除或条件价格检查。

于 2013-06-13T01:30:27.350 回答
0

Use BigDecimal (instead of double) to work with money. Its exact -- double isn't. http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html

I would write a function to get the user input. It would not return until the user had entered an allowed value.

于 2013-06-12T23:41:18.223 回答
0

尽管我真正的答案是评论中的答案,但您可以使用类似的东西。递归检查是否给出了正确的值。

import java.util.Scanner;

public class VendingMachine {

    static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("Choose your price. Your options are: ");
        for (double i = 0.25; i <= 1.25; i += 0.25) {
            System.out.printf("$%.2f\n", i);
        }

        double price = checkMultipleValues(0.25,1.25, 0.25);

        System.out.printf("You chose the $%.2f option. ", price);

        double deposit;
        if (price <= 1.00) {
            System.out.println("Please insert 1 dollar. *This machine only accepts Loonies*");
            deposit = 1;
        } else {
            System.out.println("Please insert 2 dollars.*This machine only accepts Loonies*");
            deposit = 2;
        }

        System.out.println("Please press 'Enter' to simulate inserting money. ");
        new Scanner(System.in).nextLine();

        double change;
        change = deposit - price;
        System.out.printf("Your change is $%.2f\n", change);
    }

    private static double checkMultipleValues(double initial,double last,double step) {

        System.out.println("Enter your selection now: ");
        double price = keyboard.nextDouble();

        for (double i = initial; i <= last; i += step) {
            if (price == i) {
                return price;
            }
        }

        return checkMultipleValues( initial, last, step);
    }
}


附录


既然你喜欢@Sello 回答你为什么不把它和@MrD 结合起来并有类似的东西

do {
    System.out.println("enter price");
    price = in.nextDouble();
//    System.out.println("you entered " + price);
} while (!(price % 0.25 == 0 && price <= 1.25 && price > 0));
于 2013-06-13T00:18:37.283 回答