Java程序无法读取用户输入

时间:2015-04-20 01:24:52

标签: java input

我正在尝试读取用户输入,似乎没有将变量设置为输入。

例如:

System.out.println("Would you like Syrup?(Y/N)");
   String wantSyrup = keyboard.next();
   char syrupWant = wantSyrup.charAt(0);

   switch (syrupWant){

   case 'y':
       System.out.println("Which syrup would you like?");
        System.out.println("Hot fudge, Chocolate, Caramel, Strawberry");
         syrupChoice = keyboard.nextLine();
        keyboard.nextLine();
        System.out.println("You entered: "+syrupChoice);

     if((syrupChoice.equalsIgnoreCase(hotFudge))
                || (syrupChoice.equalsIgnoreCase(chocolateSyrup))
                || (syrupChoice.equalsIgnoreCase(caramel))
                || (syrupChoice.equalsIgnoreCase(strawberrySyrup)))
            {
                System.out.println(syrupChoice);
            }else{
                System.out.println("Not a valid choice. You now have Hot Fudge as your syrup.");
                syrupChoice = hotFudge;
            }
            break;
        default: break;
    }

当我输入任何类型的糖浆时,控制台会回来。

Would you like Syrup?(Y/N)
y
Which syrup would you like?
Hot fudge, Chocolate, Caramel, Strawberry
caramel
You entered: 
Not a valid choice. You now have Hot Fudge as your syrup.

整个守则(不包括圣代课程)


import java.util.Scanner;
import java.text.*;

public class SundaeLab {

    public static void main(String[] args){

    Sundae userSundae = new Sundae();
    Scanner keyboard = new Scanner(System.in);
     String hotFudge = "Hot Fudge";
     String chocolateSyrup = "chocolate";
     String caramel = "caramel";
     String strawberrySyrup = "strawberry";
     String syrupChoice = "";
     String vanilla = "vanilla";
     String peanutButter = "peanut butter";
     String chocolate = "chocolate";
     String coconut = "coconut";
     String cookieDough = "cookie Dough";
     String coffee = "coffee";
     String strawberry = "strawberry";
     String butterPecan = "butter pecan";
     double subtotal;
     double tax;
     final double SUFFOLK_SALES_TAX = .08625;
     double costOfSundae;
     DecimalFormat df = new DecimalFormat("#.##");



    System.out.println("What flavor of Ice Cream would you like?");
    System.out.println("Your choices are: Vanilla, Peanut Butter, Chocolate, Coconut, Cookie Dough,");
    System.out.println(" Coffee, Strawberry, Butter Pecan.");

    String flavor = keyboard.nextLine();
    System.out.println("You entered: "+flavor);
    System.out.println("How many scoops of ice cream would you like?");
    System.out.println("One scoop is $1.79. Additional scoops are $1.00 Each.");
    System.out.println("You can have 6 scoops max");
    int numberOfScoops = keyboard.nextInt();
    System.out.println("You entered: "+numberOfScoops);
    String dToppings = "";

    //System.out.UserSundae.getFlavor();
    if (flavor.equalsIgnoreCase("vanilla")){
        userSundae.setFlavor(vanilla);
    }
    if (flavor.equalsIgnoreCase("peanut butter")){
        userSundae.setFlavor(peanutButter);
    }
    if (flavor.equalsIgnoreCase("chocolate")){
        userSundae.setFlavor(chocolate);
    }
    if (flavor.equalsIgnoreCase("coconut")){
        userSundae.setFlavor(coconut);
    }
    if (flavor.equalsIgnoreCase("cookie dough")){
        userSundae.setFlavor(cookieDough);
    }
    if (flavor.equalsIgnoreCase("coffee")){
        userSundae.setFlavor(coffee);
    }
    if (flavor.equalsIgnoreCase("strawberry")){
        userSundae.setFlavor(strawberry);
    }
    if (flavor.equalsIgnoreCase("butter pecan")){
        userSundae.setFlavor(butterPecan);
        }

    if (!flavor.equalsIgnoreCase(vanilla) && !flavor.equalsIgnoreCase(coffee) && !flavor.equalsIgnoreCase(peanutButter)&&
            !flavor.equalsIgnoreCase(chocolate)&& !flavor.equalsIgnoreCase(coconut) && !flavor.equalsIgnoreCase(cookieDough)&& 
            !flavor.equalsIgnoreCase(strawberry)&& !flavor.equalsIgnoreCase(butterPecan)){
        userSundae.setFlavor(vanilla);
        flavor = vanilla;
        System.out.println("We Changed your flavor to Vanilla");
        }

    if ( numberOfScoops <= 6 && numberOfScoops > 0){}
    else{
        userSundae.Sundae();
        System.out.println("We Changed your scoop amount to: 2");
        numberOfScoops = 2;
    }
    if (numberOfScoops>1){
    subtotal = (numberOfScoops - 1)*1.00 + 1.79;
    }
    else{
        subtotal = 1.79;
    }
    System.out.println("Would you like Whipped Cream?(Y/N)");
    String wantsWhipped = keyboard.next();
    char whippedWants = wantsWhipped.charAt(0); 

    if ( whippedWants == 'y'){
        String dTopppings = dToppings  + "Whipped Cream ";
        System.out.println("Whipped cream added");
    }

   System.out.println("Would you like Syrup?(Y/N)");
   String wantSyrup = keyboard.next();
   char syrupWant = wantSyrup.charAt(0);

   switch (syrupWant){

   case 'y':
       System.out.println("Which syrup would you like?");
        System.out.println("Hot fudge, Chocolate, Caramel, Strawberry");
         syrupChoice = keyboard.nextLine();
        keyboard.nextLine();
        System.out.println("You entered: "+syrupChoice);

          if((syrupChoice.equalsIgnoreCase(hotFudge))
                    || (syrupChoice.equalsIgnoreCase(chocolateSyrup))
                    || (syrupChoice.equalsIgnoreCase(caramel))
                    || (syrupChoice.equalsIgnoreCase(strawberrySyrup)))
                {
                    System.out.println(syrupChoice);
                }else{
                    System.out.println("Not a valid choice. You now have Hot Fudge as your syrup.");
                    syrupChoice = hotFudge;
                }
                break;
            default: break;
        }

    System.out.println("Our Deluxe toppings are an extra cost of .75 cents each.");
    System.out.println("Would you like to see them? (Y/N)");
    String deluxeList = keyboard.nextLine();
    if(deluxeList.equalsIgnoreCase("y")){

    System.out.println("Would you like M&M's(Y/N)");
    String mM = keyboard.nextLine();
    if (mM.equalsIgnoreCase("y")){
    dToppings = dToppings + "M&M's ";
    subtotal = subtotal + .75;
    System.out.println("M&M's Added");
}
    System.out.println("Would you like Crushed Oreos(Y/N)");
    String oreos = keyboard.nextLine();
    if (oreos.equalsIgnoreCase("y")){
    dToppings = dToppings + "Crushed Oreos ";
    subtotal = subtotal + .75;
    System.out.println("Crushed Oreo's Added");

    }
    System.out.println("Would you like Reeses pieces(Y/N)");
    String reeses = keyboard.nextLine();
    if (reeses.equalsIgnoreCase("y")){
    dToppings = dToppings + "Reese's Pieces ";
    subtotal = subtotal + .75;
    System.out.println("Reese's Peices Added");

    }
    System.out.println("Would you like Brownie Crunchies(Y/N)");
    String crunchies = keyboard.nextLine();
    if (crunchies.equalsIgnoreCase("y")){
    dToppings = dToppings + "Brownie Crunchies  ";
    subtotal = subtotal + .75;
    System.out.println("Brownie Crunchies Added");

    }
    System.out.println("Would you like Mint Chocolate Chips(Y/N)");
    String chips = keyboard.nextLine();
    if (chips.equalsIgnoreCase("y")){
    dToppings = dToppings + "Mint Chocolate Chips ";
    subtotal = subtotal + .75;
    System.out.println("Mint Chocolate Chips Added");

    }   
    }
    tax = subtotal * SUFFOLK_SALES_TAX;
    costOfSundae = tax + subtotal;

    System.out.println("You got "+numberOfScoops+" Scoops of "+flavor);
    System.out.println("Your syrup is: "+syrupChoice);
    System.out.println("Your toppings are: "+dToppings);
    System.out.println("Your subtotal is: "+ df.format(subtotal));
    System.out.println("the tax is: "+df.format(tax));
    System.out.println("Your total is: "+df.format(costOfSundae));
    }
}

2 个答案:

答案 0 :(得分:1)

更改

syrupChoice = keyboard.nextLine();

代表

syrupChoice = keyboard.next();

正如您在此输出中所看到的那样:

You entered: 

即使您输入了caramel,也显然不是syrupChoice中存储的内容。

请参阅What's the difference between next() and nextLine() methods from Scanner class?了解两种方法之间的区别。

答案 1 :(得分:1)

在keyboard.next()之后添加keyboard.nextLine()。这将读入在输入Y或N响应后仍在缓冲区中挂起的换行符。