嗨,我正在尝试制作一个问题游戏来测试我的知识和能力 无论如何,我试图使用一个整数作为点和用户的每个问题 无论如何,答案得到了特别的分数,我正在尝试这样做
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;}
..还有另外两种情况,并且点整数不在代码示例区域中,无论如何都在上行,如果需要完整的代码,我会将其放入
最佳答案
关于您的代码,
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语句
switch(condition){
case 1:
//code
break;
case 2:
//code
break;
default:// some code;
}
关于java - 尝试向一个主要整数添加更多整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18277973/