我试图做一个基本的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 –