我試圖做一個基本的8球計劃,並得到了這一切工作,但我想嘗試改變它,所以如果有人問某些問題有一個預定義回答。不知道爲什麼,這是否與字符串語句不工作
package Main;
import java.util.Random;
import java.util.Scanner;
public class eightBall {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Ask a yes or no question");
String question = input.next();
double rnd = Math.random();
int random = (int)(rnd*10);
int numberOfChoices = 5;
if(question.equalsIgnoreCase("Will i win the lottery?")){
System.out.println("The almigty 8 ball can not tell you this answer.");
}else{
switch(random % numberOfChoices){
case 0:
System.out.println("No");
break;
case 1:
System.out.println("Yes");
break;
case 2:
System.out.println("Maybe");
break;
case 3:
System.out.println("Definitely");
break;
case 4:
System.out.println("Definitely Not");
break;
}
}
}
}
我真的不知道爲什麼我的if
說法有沒有工作,因爲如果我運行它,並問:「請問我贏了彩票?」,我從那個switch語句的東西。
我的猜測是'question'在最後包含換行符('\ n','\ r'或'\ r \ n',具體取決於操作系統)。如果你用'question.toLower()開始嘗試,會發生什麼?startsWith(「我會贏彩票嗎?」)? –
可能重複:http://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class –