0

嗨,我正在尝试制作一个问题游戏来尝试我的知识和能力,无论如何我正在尝试使用整数作为积分,并且用户回答的每个问题都会获得特殊数量的积分,无论如何我试图这样做

      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;}

..还有另外两种情况,点整数不在代码示例区域中,如果完整代码是必要的,我会把它放在上面

4

1 回答 1

1

关于你的代码,

  if (IndiaAns2 == IndiaAnswer) 
          {int Twopoints = 2; Points = + Twopoints; } else{} 

应该是这样的

 if(indiaAns2.equals(indiaAnswer)){
  points += QUESTION_1_POINTS;
 }

whereQUESTION_1_POINTS 被定义为常量,如 `

public static final int  QUESTION_1_POINTS =2;

在那里你分配给points变量 , points + QUESTION_1_POINTS

points += someInteger   --> points = points + someInteger

一些建议,

1)遵循Java代码约定,变量名以小写开头

2)对于对象比较总是使用equals()而不是== 示例:

改变

if (IndiaAns2 == IndiaAnswer) 

至:

if (indiaAns2.equals(indiaAnswer)) 

3)你需要做switch语句

switch(condition){
case 1:
//code
break;
case 2:
//code 
break;
default:// some code;
}
于 2013-08-16T16:27:31.060 回答