为什么我的字符串变量在第二次赋值后为空?
Why is my String Variable empty after assigning something to it for a second time?
我正在为学校做一个项目,我正在努力使您可以在回答计算机提出的一系列问题时为自己设置一个名字。如果用户不喜欢他们输入的内容或输入错误的内容,我希望用户能够在分配后立即更改他们的名字。
现在,程序第一次正确地分配了用户想要的名称,但是当它通过循环返回将其更改为其他名称时,字符串留空。
Console Output
'''
import java.util.*;
public class JavaInputProdject
{
public static void main(String args[])
{
int i=0;
boolean boo = false;
int likeab = 0;
byte age;
boolean Old=false;
boolean aAge=true;
String user="User";
String un = user + "> ";
Scanner bob = new Scanner(System.in);
System.out.print("Bob> Hey User, My name is BOB.... what is your name?\n"+un);
do
{
user = bob.nextLine();
System.out.println("Bob> This is the Username you want? \""+ user +"\"(true/false)");
System.out.print(un);
if(bob.nextBoolean()==true)
{
boo = true;
un = user + "> ";
}
else
{
if(i>=3)
{
System.out.println("Bob> I realize it is kind of hard to pick a name but could you hurry up?");
}
System.out.print("Bob> Please type in a new Username\n"+un);
bob.next();
i++;
}
} while(boo==false);
}
}
'''
当你想根据错误标志获得正确的用户名时,你不会为用户初始化一个值。
你应该用 bob.nextLine
写这样的东西:
System.out.print("Bob> Please type in a new Username\n"+un);
user = bob.nextLine();
i++;
您需要将 bob.next()
行(接近 do-while
循环的末尾)替换为 bob.nextLine()
。
我相信 bob.next()
不会消耗在 bob.nextBoolean()
调用后按 键输入的换行符。因此 user = bob.nextLine();
行(在 do-while
循环的开始处)在第二次和后续循环迭代中使用该换行符。因此将 bob.next()
替换为 bob.nextLine()
将解决问题。
为了完整起见,这里是更正后的代码:
import java.util.Scanner;
public class JavaInputProdject {
public static void main(String[] args) {
int i = 0;
boolean boo = false;
int likeab = 0;
byte age;
boolean Old = false;
boolean aAge = true;
String user = "User";
String un = user + "> ";
Scanner bob = new Scanner(System.in);
System.out.print("Bob> Hey User, My name is BOB.... what is your name?\n" + un);
do {
user = bob.nextLine();
System.out.println("Bob> This is the Username you want? \"" + user + "\"(true/false)");
System.out.print(un);
if (bob.nextBoolean()) {
boo = true;
un = user + "> ";
}
else {
if (i >= 3) {
System.out.println(
"Bob> I realize it is kind of hard to pick a name but could you hurry up?");
}
System.out.print("Bob> Please type in a new Username\n" + un);
bob.nextLine();
i++;
}
} while (boo == false);
}
}
参考Scanner is skipping nextLine() after using next() or nextFoo()?
我正在为学校做一个项目,我正在努力使您可以在回答计算机提出的一系列问题时为自己设置一个名字。如果用户不喜欢他们输入的内容或输入错误的内容,我希望用户能够在分配后立即更改他们的名字。 现在,程序第一次正确地分配了用户想要的名称,但是当它通过循环返回将其更改为其他名称时,字符串留空。 Console Output '''
import java.util.*;
public class JavaInputProdject
{
public static void main(String args[])
{
int i=0;
boolean boo = false;
int likeab = 0;
byte age;
boolean Old=false;
boolean aAge=true;
String user="User";
String un = user + "> ";
Scanner bob = new Scanner(System.in);
System.out.print("Bob> Hey User, My name is BOB.... what is your name?\n"+un);
do
{
user = bob.nextLine();
System.out.println("Bob> This is the Username you want? \""+ user +"\"(true/false)");
System.out.print(un);
if(bob.nextBoolean()==true)
{
boo = true;
un = user + "> ";
}
else
{
if(i>=3)
{
System.out.println("Bob> I realize it is kind of hard to pick a name but could you hurry up?");
}
System.out.print("Bob> Please type in a new Username\n"+un);
bob.next();
i++;
}
} while(boo==false);
}
}
'''
当你想根据错误标志获得正确的用户名时,你不会为用户初始化一个值。
你应该用 bob.nextLine
写这样的东西:
System.out.print("Bob> Please type in a new Username\n"+un);
user = bob.nextLine();
i++;
您需要将 bob.next()
行(接近 do-while
循环的末尾)替换为 bob.nextLine()
。
我相信 bob.next()
不会消耗在 bob.nextBoolean()
调用后按 user = bob.nextLine();
行(在 do-while
循环的开始处)在第二次和后续循环迭代中使用该换行符。因此将 bob.next()
替换为 bob.nextLine()
将解决问题。
为了完整起见,这里是更正后的代码:
import java.util.Scanner;
public class JavaInputProdject {
public static void main(String[] args) {
int i = 0;
boolean boo = false;
int likeab = 0;
byte age;
boolean Old = false;
boolean aAge = true;
String user = "User";
String un = user + "> ";
Scanner bob = new Scanner(System.in);
System.out.print("Bob> Hey User, My name is BOB.... what is your name?\n" + un);
do {
user = bob.nextLine();
System.out.println("Bob> This is the Username you want? \"" + user + "\"(true/false)");
System.out.print(un);
if (bob.nextBoolean()) {
boo = true;
un = user + "> ";
}
else {
if (i >= 3) {
System.out.println(
"Bob> I realize it is kind of hard to pick a name but could you hurry up?");
}
System.out.print("Bob> Please type in a new Username\n" + un);
bob.nextLine();
i++;
}
} while (boo == false);
}
}
参考Scanner is skipping nextLine() after using next() or nextFoo()?