我尝试过验证码
int count = 0
int count2 = 0
while (!FreindName.equals("stop")){
System.out.println("Enter your freind's first name :");
FreindName = input.nextLine();
count++;
if (MyName.charAt(0) == FreindName.charAt(0)){
count2++;
问题是,在运行该程序并键入'stop'后,count是将stop作为一个值,并且包含在count ++中
例如我输入了3个名字山姆·琼·米歇尔,
然后我输入停止计数给出4
为什么不是3?
因为获取输入时您已经在while
内部,所以执行count
。如果您键入“停止”,则可以使用if
来检查count
之前。
例如:
if(FreindName != "stop")
count++