2015-10-12 119 views
-1

无论我输入“y”还是“n”,程序都会退出while循环。我试着将一些==改为.equals,但不管我进入循环的方式如何。当条件在循环中继续时遇到循环退出

System.out.println("Just Cycling Caps provides three types of caps: Small caps – ID: 1 Price: $4.50, Medium caps – ID: 2 Price: $7.00, and Large caps – ID: 3 Price: $9.00"); 
continueOrder = "y"; 
//While loop to get items ordered 
while(continueOrder == "y"){ 
    //get desired cap size and quantity 
    System.out.println("Enter the ID of the item you would like"); 
    capSize = reader.nextInt(); 
    if(capSize == 1){ 
    price = 4.5; 
    System.out.println("How many small caps would you like?"); 
    quantityOfSmall = reader.nextInt(); 
    priceOfSmall = quantityOfSmall * price; 
    } 
    else if(capSize == 2){ 
    price = 7.0; 
    System.out.println("How many medium caps would you like?"); 
    quantityOfMedium = reader.nextInt(); 
    priceOfMedium = quantityOfMedium * price; 
    } 
    else if(capSize == 3){ 
    price = 9.0; 
    System.out.println("How many large caps would you like?"); 
    quantityOfLarge = reader.nextInt(); 
    priceOfLarge = quantityOfLarge * price; 
    } 
    //Ask the user if they would like to continue ordering 
    System.out.println("Would you like to purchase another product? (y/n)"); 
    continueOrder = reader.next(); 
    continueOrder = continueOrder.toLowerCase(); 
} 

回答

0

更改此

while(continueOrder == "y") 

while(continueOrder.equals("y"))