gpt4 book ai didi

java.lang.NullPointerException 表单用户输入

转载 作者:行者123 更新时间:2023-11-29 04:57:56 27 4
gpt4 key购买 nike

大家好,我正在尝试创建一个循环,直到用户输入正确的字符选择。当我输入错误的选择时,我得到错误 java.lang.NullPointerException。这可能与我输入的方式有关,但如果不需要,我不想更改它。 choice 是该类的私有(private)成员。

char wf() { 
Scanner input = new Scanner(System.in);
System.out.println("What is your choice? (x/o)");
choice = input.findInLine(".").charAt(0);

while (choice != 'x' && choice != 'o') {
System.out.println("You must enter x or o!");
choice = input.findInLine(".").charAt(0);
}

return choice;
}//end wf

最佳答案

修改函数如下(我测试过这段代码):

char wf() { 
Scanner input = new Scanner(System.in);
System.out.println("What is your choice? (x/o)");
char choice = input.findInLine(".").charAt(0);

while (choice != 'x' && choice != 'o') {
System.out.println("You must enter x or o!");
choice = input.next().charAt(0);
}

return choice;
}//end wf

关于java.lang.NullPointerException 表单用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33067801/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com