【问题标题】:Why is my String Variable empty after assigning something to it for a second time? [duplicate]为什么我的字符串变量在第二次分配给它之后是空的? [复制]
【发布时间】:2021-12-04 19:55:51
【问题描述】:

我正在为学校做一个项目,我正在努力让您在处理计算机提出的一系列问题时为自己设置一个名字。我希望用户能够在分配名称后立即更改他们的名称,如果他们不喜欢他们放下的内容或输入错误的内容。 现在程序第一次正确地分配了用户想要的名称,但是当它通过循环返回以将其更改为其他名称时,字符串保留为空白。 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);
     }
    }

'''

【问题讨论】:

  • 不,我不认为是这样,我必须添加 next(),因为它跳过了 nextLine()。没有它,它会直接越过用户应该能够输入其用户名的位置。

标签: java string java.util.scanner user-input


【解决方案1】:

您需要将bob.next() 行(接近do-while 循环的末尾)替换为bob.nextLine()

我相信bob.next() 不会使用在bob.nextBoolean() 调用后按 键输入的换行符。因此user = bob.nextLine(); 行(在do-while 循环的开始处)在第二次和后续循环迭代中使用该换行符。所以用bob.nextLine()替换bob.next()就可以解决问题了。

为了完整起见,这里是更正的代码:

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()?

【讨论】:

    【解决方案2】:

    当您想根据错误标志获得正确的用户名时,您不会为用户初始化值。 你应该用bob.nextLine 写这样的东西:

     System.out.print("Bob> Please type in a new Username\n"+un);
     user = bob.nextLine();
     i++;
    

    【讨论】:

    • 由于代码在循环中,循环的第一部分不会再次初始化变量吗?我明白你在说什么,但是在用户输入 false 后,它会要求他们输入一个新的用户名,然后它会循环回到应该能够接受“user”的新值的开头。
    • @TrinityDionne 好的,您不需要为其分配用户,但您应该使用 bob.nextLine() 而不是 bob.next()
    猜你喜欢
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2011-07-02
    • 2016-07-02
    • 2021-02-06
    • 2012-09-06
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多