嗨,我正在尝试制作一个问题游戏来尝试我的知识和能力 无论如何,我试图使用整数作为点和用户的每个问题 答案得到了一些特别的积分无论如何我试图这样做
switch (Ques){ case 1 : //first question about India and where it is in the map
System.out.println("in what continent India is?");
Scanner IndiaAns = new Scanner(System.in); //Scanner to receive user answer
String IndiaAns2 , IndiaAnswer ; //strings to be used to receive user input and matching with the correct ones
IndiaAns2 = IndiaAns.nextLine(); //Scanner will work here and receive...
IndiaAnswer = "asia"; //the correct answer here and will be matched with user ones
if (IndiaAns2 == IndiaAnswer)
{int Twopoints = 2; Points = + Twopoints; } else{}
case 2:
System.out.println("the Appstore founds in any phone model?");
Scanner Appstore =new Scanner(System.in);
String AppstoreAns1 ,AppstoreAns2; //strings saving
AppstoreAns1 = Appstore.nextLine(); //Scanner
AppstoreAns2 = "iphone"; //matching with user answer
if (AppstoreAns1 == AppstoreAns2)
{ int Threepoints = 3; Points = +Threepoints;} else { Points = +0;}
..还有另外两种情况,并且整数不在代码示例区域中的点是在上面的任何方式,如果完整的代码是必要的我会把它放在
答案 0 :(得分:1)
关于您的代码,
if (IndiaAns2 == IndiaAnswer)
{int Twopoints = 2; Points = + Twopoints; } else{}
应该像
if(indiaAns2.equals(indiaAnswer)){
points += QUESTION_1_POINTS;
}
QUESTION_1_POINTS
定义为常量,如`
public static final int QUESTION_1_POINTS =2;
您要分配给points
变量points + QUESTION_1_POINTS
。
points += someInteger --> points = points + someInteger
一些建议,
1)关注Java Code Conventions,变量名以小写
开头 2)对于对象比较,始终使用equals()
而不是==
例如:
更改
if (IndiaAns2 == IndiaAnswer)
为:
if (indiaAns2.equals(indiaAnswer))
3)你需要制作开关声明
switch(condition){
case 1:
//code
break;
case 2:
//code
break;
default:// some code;
}