[英]How do you make sure only one output is used even if the criteria are met for two outputs?
我当时在做一个小项目,没有什么疯狂的事要向朋友们展示,当我尝试创造两个不同的机会时,我遇到了一个问题。 我试图这样做,以便如果数字Grenerator提出数字1,则将采用与提出不同数字的方法不同的方法。 但是,当我尝试时,两种方法都运行了。 我希望可以尝试一种不同的方法。 让我知道您是否有任何见解。
我已在“ Stackoverflow”和常规搜索中签入了正在教我的Java书籍,并在线阅读了这些书籍。
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
Random myRandom = new Random();
long randomNumber;
System.out.println("What is your question my puppet?");
keyboard.nextLine();
randomNumber = myRandom.nextInt(10);
if (randomNumber ==1) {
System.out.println("Try rewordding that, it doesn't sound right");
keyboard.close();
}
if (randomNumber > 5) {
System.out.println("Ah, I can sense that yes is the answer you're looking for.");
} else {
System.out.println("Ah, I can sense that no is the answer you're looking for.");
}
keyboard.close();
}
}
我希望如果选择了数字1,则会出现消息“尝试改写,听起来不正确”,但由于它也遵循弹出消息的数字小于5的标准。
if
在第二个(当前是独立的)之前添加else
。 喜欢,
if (randomNumber == 1) {
System.out.println("Try rewordding that, it doesn't sound right");
// keyboard.close(); // not needed you have another close call later.
} else if (randomNumber > 5) {
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.