java - 尝试向一个主要整数添加更多整数

标签 java int

嗨,我正在尝试制作一个问题游戏来测试我的知识和能力 无论如何,我试图使用一个整数作为点和用户的每个问题 无论如何,答案得到了特别的分数,我正在尝试这样做

      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/

相关文章:

java - 当线程运行时重新绘制 swing 组件

Java:大集合和并发线程

c# - 如果 int 数组中的前一个元素 == bool 变量

Python:检查(非常大的)素数时为 "long int too large to convert to float"

c# - 在 C# 中将字符串转换为 int

c - 在C中手动将4位int(十进制)提取到字符串中

java - 在带分隔符的字符串中查找并删除子字符串(开始 : 55555, 结束:\n)

Java 方法到文件

java - Android中的一个字符串存在于多个Activity中?

c# - 为什么我不能将 int 拆箱为小数?