我已經嘗試輸入其他與我所需要的東西無關的東西,它會提示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中使用字符串... –