[英]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.