[英]Beginner - Java Programming While Loop
This code keeps on looping every time I enter something. 每当我输入内容时,此代码都会不断循环播放。 How to fix it?
如何解决?
public void inputPlayer()
{
Scanner input = new Scanner(System.in);
while(name.length() < 1)
{
System.out.println("Name: ");
this.setName(input.nextLine());
//name = input.nextLine();
if(name.length() < 1)
System.err.println("Must have one or more characters");
}
}
Heres' a screenshot of the whole thing: 以下是整个内容的屏幕截图:
The problem is visible in the screenshot, and is with your setName()
method, which is currently: 该问题在屏幕快照中可见,并且与您的
setName()
方法有关,该方法当前为:
public void setName(String newName) {
newName = name;
}
The assignment is the wrong way around, so you never update your name
field. 分配方式错误,因此您永远不会更新
name
字段。 It should be name = newName;
它应该是
name = newName;
. 。
because your setName function is wrong 因为您的setName函数是错误的
it should be: 它应该是:
public void setName(String newName) {
name = newName;
}
you put newName = name; 你把newName = name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.