我不明白为什么我的while循环不会中断,它一直在运行,我试着做LC10,我试着返回,但是没有什么能结束这个循环。
import java.util.Scanner;
public class BirthdayReminder
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String[] BDay = new String[10];
String[] friend = new String[10];
int Lc = 0;
String i;
while(Lc < 10) {
System.out.println("enter a friends name or zzz to quit");
i = input.nextLine();
if(i == "zzz") {
break;
}
else if(i != "zzz"){
friend[Lc] = i;
System.out.println("enter their birthday.");
i = input.nextLine();
BDay[Lc] = i;
Lc++;
return;
}
}
System.out.println("hi");
}
}
发布于 2018-06-02 07:15:48
您需要使用equals()
而不是==
来检查字符串相等性。if
中的两个代码块都不会被输入,因此Lc
永远不会递增。
https://stackoverflow.com/questions/50651990
复制相似问题