有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java While循环在满足继续循环的条件时退出

无论我输入“y”还是“n”,程序都会退出while循环。我试着把一些==改成。但不管我进入什么循环都会继续

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();
}

共 (1) 个答案

  1. # 1 楼答案

    改变这个

    while(continueOrder == "y")
    

    while(continueOrder.equals("y"))