使用扫描仪选择以前的用户输入

时间:2014-08-24 07:48:40

标签: java input

我正在测试一些用户选择数字0和0的代码。 1 - 3. 1 - 3选择一个选项并将一个数字加到总数中,0退出并打印最终总数。代码似乎工作,但只是按顺序,例如:类型1,2,3,0。我的问题是如何制作它以便用户可以在任何点输入任何选择,例如:2,2,3,0然后打印最终总数。

import java.util.*;
import javax.swing.*;


public class Coffee {

    public static void main(String[] args) {

        double AmericanPrice;
        double EspressoPrice;
        double LattePrice;

        double totalBill;

        AmericanPrice = 1.99;
        EspressoPrice = 2.50;
        LattePrice = 2.15;

        int selectionBill;
        int selectionA;
        int selectionE;
        int selectionL;

        selectionBill = 0;
        selectionA = 1;
        selectionE = 2;
        selectionL = 3;

        totalBill = 0.0;

        Scanner user = new Scanner(System.in);
        while(true) { 
            System.out.print("Place your order: ");

            if(user.nextInt() == selectionA) {
                totalBill = totalBill + AmericanPrice;
                System.out.print("Place your order: ");
            }

            if(user.nextInt() == selectionE) {
                totalBill = totalBill + EspressoPrice;
                System.out.print("Place your order: ");
            }

            if(user.nextInt() == selectionL) {
                totalBill = totalBill + LattePrice;
                System.out.print("Place your order: ");
            }

            if(user.nextInt() == selectionBill) {
                System.out.print("Total: "+totalBill);
                break;
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

正如JB所说,实际上调用nextInt()消耗"输入,所以再次调用它获得下一个。你想要的是调用它然后比较,并在循环中重复,如:

int answer = user.nextInt();
while(answer != 0) {
  if(answer == selectionA) {
    ...
  }
  if(answer == selectionB) {
    ...
  }
  answer = user.nextInt();
}

请注意" switch"也许是比这里更好的选择。

答案 1 :(得分:1)

Martin和JB都是正确的,开关可以正常工作。没有必要使用if循环,只有在这里可以正常工作。

Scanner user = new Scanner(System.in);
System.out.println("Place your order: ");
int answer = user.nextInt();
while(answer !=0)
{
    totalBill = answer + totalBill;
    System.out.println("Place your order: ");
    answer = user.nextInt();
}
System.out.println("Total: " + totalBill);
}
}

答案 2 :(得分:0)

代码中发生了什么:

//creating infinite loop
while(true) {

    //every time we're returing to the begining of the loop we're asking about the order.
    System.out.print("Place your order: ");  

    //here we're hanging the execution of the program to wait for users input. If the users input is not equal to selection A we're not doing anything. So if you input i.e 3, you will go exactly to...
    if(user.nextInt() == selectionA) {  
        totalBill = totalBill + AmericanPrice;
        System.out.print("Place your order: ");
    }

    //...here. And here you again wait for the input. If it's not 2 you'll go further without writing anything to the user. And not adding to the bill.
    if(user.nextInt() == selectionE) {
        totalBill = totalBill + EspressoPrice;
        System.out.print("Place your order: ");
    }
    //... and so on.
}

所以你想做的是先输入 - 无论是什么,然后将它与所有可能性进行比较。

就像那样:

while(true) { 

    System.out.print("Place your order: ");

    int orderId = user.nextInt();

    if(orderId == selectionA) {
        totalBill = totalBill + AmericanPrice;
    } else if(orderId == selectionE) {
        totalBill = totalBill + EspressoPrice;
    } else if(orderId == selectionL) {
        totalBill = totalBill + LattePrice;
    } else if(orderId == selectionBill) {
        System.out.print("Total: "+ totalBill);
        break;
    }
}

而不是所有这些if-else语句,您可以使用switch statement更优雅。