Java Eclipse 控制台在比较用户输入值和字符串值之前终止程序
Java Eclipse console terminates program before comparing values of user input and string value
所以我在为我弟弟和我 运行 制作这个程序时遇到了问题。该程序假设要求用户输入,然后通过一系列 "if" 语句将其与多个字符串值进行比较。相反发生的是用户提供他们的输入,然后程序立即终止。我已经为此工作了几个小时,并且开始对此感到非常恼火。这是我到目前为止输入的代码:
package package1;
import java.util.Scanner;
public class Psychic_Calculator {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Hello user, please type your name below:");
String a = scan.nextLine();
System.out.println("Welcome " + a + ", think of a number. Once you have your number, type 'okay' below!");
String b = scan.nextLine();
if (b == "okay"){
System.out.println("Now, add '11' to your number and type 'okay' below.");
}
else if (b == "Okay"){
System.out.println("Please don't capitalize 'okay', try typing it again!");
String c = scan.nextLine();
if (c == "okay"){
System.out.println("Now, add '11' to your number and type 'okay' below.");
String d = scan.nextLine();
if (d == "okay"){
System.out.println("Now, add '2' to your new number, then type 'okay' below.");
String e = scan.nextLine();
if (e == "okay"){
System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");
String f = scan.nextLine();
if (f == "okay"){
System.out.println("Your new number is '13'. Don't even try denying it.");
}
}
}
}
if (c == "Okay"){
System.out.println("I already told you not to capitalize 'okay', try typing it again, you idiot!");
String g = scan.nextLine();
if (g == "okay"){
System.out.println("Now, add '11' to your number and type 'okay' below.");
String h = scan.nextLine();
if (h == "okay"){
System.out.println("Now, add '2' to your new number, then type 'okay' below.");
String i = scan.nextLine();
if (i == "okay"){
System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");
String j = scan.nextLine();
if (j == "okay"){
System.out.println("Your new number is '13'. Don't even try denying it.");
}
}
}
}
}
if (c != "okay") {
while (c != "okay") {
System.out.println("Do you even know how to spell 'okay'?" + "'" + c + "' does not spell 'okay', you moron! Try typing 'okay' again.");
String n = scan.nextLine();
if (n == "okay"){
System.out.println("Finally, you learned how to spell 'okay'. Your vocabulary is now one word larger, you're welcome. Now, please add '11' to your number and then type 'okay'(correctly this time).");
String k = scan.nextLine();
if (k == "okay"){
System.out.println("Now, add '2' to your new number, then type 'okay' below.");
String l = scan.nextLine();
if (l == "okay"){
System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");
String m = scan.nextLine();
if (m == "okay"){
System.out.println("Your new number is '13'. Don't even try denying it.");
}
}
}
}
else {
System.out.println(a + ", " + "you have failed to type 'okay' too many times! You have no idea how to spell 'okay' you electricutin' motherboarder! Go shove your face in a pile of computer chips and grow a pair!");
System.out.println("(of RAM cartriges...I meant to say RAM cartriges).");
}
}
}
}
}
}
问题在于您如何比较字符串。将 b == "okay"
更改为 b.equals("okay")
将所有 ==
比较更改为 .equals()
或 .equalsIgnoreCase()
。
对于否定,使用 (!(c.equals("okay"))
在Java中,==
将按值比较原始类型,但会按内存地址比较对象。换句话说,当你说 b == "okay"
它不是在进行值比较时,它是在检查这两个对象是否指向相同的内存地址,这当然是错误的。
更新:关于您编写此程序的方式的一些事情。
1) 您不必要地创建了很多字符串对象。在绝对需要另一个字符串对象之前,最好重新使用一个字符串对象。这适用于您正在使用的任何对象。尽量不要分配不必要的对象。
2) 除了所有的 if 语句,您可以定义一个指令数组,像这样并压缩您的代码:
String [] instr = {"Add 2", "Add 11", "Subtract Original"};//array of directions
String [] invalidResp = {"Wrong", "You can't spell", "Try Again", "No"};//array of responses to print if user doesnt type 'okay' properly
int instrCounter = 0;//you don't really need this but it helps with readability
String userInput = "";//only string object you'll need =)
while (instrCounter < instr.length){//I couldve just as easily used a for loop instead.
userInput = scan.nextLine();
while (!userInput.equals("okay")){
userInput = scan.nextLine();
System.out.println(invalidResp[(int) (Math.random()*invalidResp.length)]);
//will print a random invalid response from the invalidResp array
System.out.println(userInput + " is not how you spell okay");
}//end while
System.out.println("Great, now, " + instr[instrCounter]);
instrCounter++;
}//end outer while
请记住,当您编写代码时,您希望它相当通用和灵活。我编写代码的方式,我可以添加到 instr 数组或添加更多无效响应,并且我不会不必要地创建对象。
就像我在内联评论中所说的那样,我本来可以很容易地为外循环使用 for 循环,但为了可读性并确保您理解我在做什么,我使用了 while 循环,因为我相信它们是阅读起来更直观。
游戏:
同时(运行){
System.out.println("------------------------");
继续游戏;
//命名while循环然后你就可以break了;出。
所以我在为我弟弟和我 运行 制作这个程序时遇到了问题。该程序假设要求用户输入,然后通过一系列 "if" 语句将其与多个字符串值进行比较。相反发生的是用户提供他们的输入,然后程序立即终止。我已经为此工作了几个小时,并且开始对此感到非常恼火。这是我到目前为止输入的代码:
package package1;
import java.util.Scanner;
public class Psychic_Calculator {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Hello user, please type your name below:");
String a = scan.nextLine();
System.out.println("Welcome " + a + ", think of a number. Once you have your number, type 'okay' below!");
String b = scan.nextLine();
if (b == "okay"){
System.out.println("Now, add '11' to your number and type 'okay' below.");
}
else if (b == "Okay"){
System.out.println("Please don't capitalize 'okay', try typing it again!");
String c = scan.nextLine();
if (c == "okay"){
System.out.println("Now, add '11' to your number and type 'okay' below.");
String d = scan.nextLine();
if (d == "okay"){
System.out.println("Now, add '2' to your new number, then type 'okay' below.");
String e = scan.nextLine();
if (e == "okay"){
System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");
String f = scan.nextLine();
if (f == "okay"){
System.out.println("Your new number is '13'. Don't even try denying it.");
}
}
}
}
if (c == "Okay"){
System.out.println("I already told you not to capitalize 'okay', try typing it again, you idiot!");
String g = scan.nextLine();
if (g == "okay"){
System.out.println("Now, add '11' to your number and type 'okay' below.");
String h = scan.nextLine();
if (h == "okay"){
System.out.println("Now, add '2' to your new number, then type 'okay' below.");
String i = scan.nextLine();
if (i == "okay"){
System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");
String j = scan.nextLine();
if (j == "okay"){
System.out.println("Your new number is '13'. Don't even try denying it.");
}
}
}
}
}
if (c != "okay") {
while (c != "okay") {
System.out.println("Do you even know how to spell 'okay'?" + "'" + c + "' does not spell 'okay', you moron! Try typing 'okay' again.");
String n = scan.nextLine();
if (n == "okay"){
System.out.println("Finally, you learned how to spell 'okay'. Your vocabulary is now one word larger, you're welcome. Now, please add '11' to your number and then type 'okay'(correctly this time).");
String k = scan.nextLine();
if (k == "okay"){
System.out.println("Now, add '2' to your new number, then type 'okay' below.");
String l = scan.nextLine();
if (l == "okay"){
System.out.println("Now, subtract your original number from your new number, then type 'okay' below.");
String m = scan.nextLine();
if (m == "okay"){
System.out.println("Your new number is '13'. Don't even try denying it.");
}
}
}
}
else {
System.out.println(a + ", " + "you have failed to type 'okay' too many times! You have no idea how to spell 'okay' you electricutin' motherboarder! Go shove your face in a pile of computer chips and grow a pair!");
System.out.println("(of RAM cartriges...I meant to say RAM cartriges).");
}
}
}
}
}
}
问题在于您如何比较字符串。将 b == "okay"
更改为 b.equals("okay")
将所有 ==
比较更改为 .equals()
或 .equalsIgnoreCase()
。
对于否定,使用 (!(c.equals("okay"))
在Java中,==
将按值比较原始类型,但会按内存地址比较对象。换句话说,当你说 b == "okay"
它不是在进行值比较时,它是在检查这两个对象是否指向相同的内存地址,这当然是错误的。
更新:关于您编写此程序的方式的一些事情。
1) 您不必要地创建了很多字符串对象。在绝对需要另一个字符串对象之前,最好重新使用一个字符串对象。这适用于您正在使用的任何对象。尽量不要分配不必要的对象。 2) 除了所有的 if 语句,您可以定义一个指令数组,像这样并压缩您的代码:
String [] instr = {"Add 2", "Add 11", "Subtract Original"};//array of directions
String [] invalidResp = {"Wrong", "You can't spell", "Try Again", "No"};//array of responses to print if user doesnt type 'okay' properly
int instrCounter = 0;//you don't really need this but it helps with readability
String userInput = "";//only string object you'll need =)
while (instrCounter < instr.length){//I couldve just as easily used a for loop instead.
userInput = scan.nextLine();
while (!userInput.equals("okay")){
userInput = scan.nextLine();
System.out.println(invalidResp[(int) (Math.random()*invalidResp.length)]);
//will print a random invalid response from the invalidResp array
System.out.println(userInput + " is not how you spell okay");
}//end while
System.out.println("Great, now, " + instr[instrCounter]);
instrCounter++;
}//end outer while
请记住,当您编写代码时,您希望它相当通用和灵活。我编写代码的方式,我可以添加到 instr 数组或添加更多无效响应,并且我不会不必要地创建对象。 就像我在内联评论中所说的那样,我本来可以很容易地为外循环使用 for 循环,但为了可读性并确保您理解我在做什么,我使用了 while 循环,因为我相信它们是阅读起来更直观。
游戏: 同时(运行){ System.out.println("------------------------");
继续游戏;
//命名while循环然后你就可以break了;出。