我已经尝试输入其他与我所需要的东西无关的东西,它会提示else if语句要求我再次输入..但为什么当我输入正确的东西时仍然要求我再次选择?为什么?为什么我的循环不会停止,即使我输入正确
这里是我的代码部分:
public static void choose()
{
Scanner read=new Scanner(System.in);
String shape = "";
do{
System.out.println("which shape you would like to choose");
shape=read.nextLine();
if(shape.equals("rectangle"))
{
System.out.println("enter width");
width=Double.parseDouble(read.nextLine());
System.out.println("enter length");
length=Double.parseDouble(read.nextLine());
System.out.println("enter color");
String color = read.nextLine();
}
else if (shape.equals("box"))
{
System.out.println("enter width");
width=Double.parseDouble(read.nextLine());
System.out.println("enter length");
length=Double.parseDouble(read.nextLine());
System.out.println("enter height");
height=Double.parseDouble(read.nextLine());
System.out.println("enter color");
String color = read.nextLine();
}
else
{
System.out.println("please enter only rectangle and box");
}
}while((shape !="rectangle" && shape !="box"));
这里我跑:
which shape you would like to choose
abc
please enter only rectangle and box
which shape you would like to choose
box
enter width
9
enter length
8
enter height
8
enter color
blue
which shape you would like to choose
*** shape!=“rectangle”***永远不会在java中使用字符串... –