【发布时间】:2015-01-26 15:55:02
【问题描述】:
我正在自学 Java,我已经学习了几个星期,决定制作一个包含多个选项的程序。 第一部分是在两种动物之间进行选择,在本例中是海豹和河马。 在那之后,我在选择印章后遇到的问题是我想要选择印章后的选项,例如重复动作、终止动作或在用户随机输入内容时要求正确输入。
这是我的代码
import java.util.Scanner;
class animal {
//Animal variables
String name;
String type;
//Method or subroutine, something an object belonging to the class can do
void sound() {
System.out.println(name + " was petted! " + name + " went 'Gauuu!'");
}
}
public class Objectmethod {
public static void main(String[] args) {
Scanner scanner1 = new Scanner(System.in);
Scanner scanner2 = new Scanner(System.in);
//Creating an object under the class of Animal
animal animal1 = new animal();
animal1.name = "Baby Elephant Seal";
animal1.type = "Seal";
//Creating second animal
animal animal2 = new animal();
animal2.name = "Hippopotawhateveritis";
animal2.type = "Hippopotamus or however you spell it";
//Beginning prompt for user input
System.out.println("Would you like to pet the seal, or the hippo?");
//The code to recieve input of the user
String select = scanner1.nextLine();
//check user input to select correct object
while(true)
if(select.equals("seal")){
//Command that the animal sounds has.
animal1.sound();
//Prompt if the user would like to repeat
System.out.println("Would you like to pet the seal again?");
//second input recieving from user
String input = scanner2.nextLine();
//Checks user input for yes no or random crap
if(input.equals("yes")){
continue;
}
else if(input.equals("no")){
break;
}
else{
System.out.println("Answer yes or no you derpface.");
input = scanner2.nextLine();
}
}
else if(select.equals("hippo")){
animal2.sound();
break;
}
else{
System.out.println("You cray cray. Just pick one.");
select = scanner1.nextLine();
}
System.out.println("Thank you for participating.");
}
}
打字印章工作正常 然后当输入胡言乱语以获得“回答是或否你的derpface”的else响应时 它第一次工作,然后第二次返回响应。 这就是发生的事情
Would you like to pet the seal, or the hippo?
seal
Baby Elephant Seal was petted! Baby Elephant Seal went 'Gauuu!'
Would you like to pet the seal again?
"randominput"
Answer yes or no you derpface.
"secondrandominput"
Baby Elephant Seal was petted! Baby Elephant Seal went 'Gauuu!'
Would you like to pet the seal again?
no
Thank you for participating.
我有什么错误导致它转到“if”而不是“else”? -已解决-
新问题。这是我尝试修复脚本后的最后一部分。 我相信这个问题发生在终止之前的代码块中,但是我不确定它到底是什么,因为我试图应用上述修复并且它没有工作。 (“感谢参与”代码上方的 else 语句)已解决
(问题是将第一个“while(true)”移到其他所有内容之上。我将它移回原来在“scanner1”下方的位置,现在它可以正常运行
-注意-这是在应用第一个“while(true)”来稳健地重复抚摸动物之后。
else{
while(!select.equals("seal") && !select.equals("hippo")){
System.out.println("You cray cray. Just pick one.");
select = scanner1.nextLine();
}
问题示例:(引号代表用户输入。)
Would you like to pet the seal, or the hippo?
"neither"
You cray cray. Just pick one.
"neither"
You cray cray. Just pick one.
"seal"
Thank you for participating.
Would you like to pet the seal, or the hippo?
我希望它发生的方式:(引号代表用户输入。)
Would you like to pet the seal, or the hippo?
"neither"
You cray cray. Just pick one.
"neither"
You cray cray. Just pick one.
"seal"
Baby Elephant Seal was petted! Baby Elephant Seal went 'Gauuu!'
Would you like to pet the seal again?
...
...
//Rest of the program (Confirming repeated petting, or termination of program)
【问题讨论】:
-
如果我继续输入乱码或无法识别的输入,它会在正确响应和“是”响应之间交替。
-
我会立即提出的一个建议是摆脱其中一台扫描仪。这里有两个扫描仪没有任何意义。此外,您使用 select 获取初始字符串,但使用不同的字符串在 while 循环中获取新输入,但仍基于原始字符串进行比较。
-
我不确定是否需要两个。什么时候适合使用多台扫描仪? (包、类、对象..?)
-
您可能需要多台扫描仪的一个原因是,如果您从多个来源进行扫描(即,使用一台扫描仪从 system.in 扫描,而使用另一台扫描仪从文件扫描)。
标签: java if-statement while-loop