我正在尝试编写这个方法,它会不断从用户那里读取数据,直到输入“退出”一词。我尝试使用break和for循环;它不起作用。我尝试使用 while,但即使输入“退出”一词,它也不会停止。任何想法如何解决这一问题?谢谢。
public void read(Scanner scanner){
while(3<4){
String in = scanner.nextLine();
Scanner scanner_2 = new Scanner(in);
String name = null;
if(scanner_2.hasNext()){
//create and add the user to the user container class
name = scanner_2.next();
System.out.println(name);
}
if(name == exit)
//stop or break the while loop
}
}
name == exit
是错的。
You want
name.equals(exit)
or
name.equals("exit")
分别取决于 exit 是变量还是字符串文字。在爪哇,==
表示引用相等(例如,这两个引用是否指向内存中的同一地址),而 .equals
表示对象等价,通常是overriden
在开发人员的课程中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)