Java 扫描器在问答题中的应用

Java 扫描器在问答题中的应用,java,string,operations,Java,String,Operations,大家好,我在用java编写测验程序时遇到了一些问题,以下是我的问题:(注意:我没有为第3-10季度设置问题)。我遇到的问题是,一旦用户正确输入了第一个问题,并且显示了第二个问题,如果用户输入了第二个问题的正确答案:a^2+b^2=c^2,则“对不起,您输了,您的分数为1/10,仍将显示”。即使它应该是:“正确的!下一个问题:”我刚刚开始编码,如果这是一个简单的错误,我感到非常抱歉 import java.util.Scanner; public class Quiz { pu

大家好,我在用java编写测验程序时遇到了一些问题,以下是我的问题:(注意:我没有为第3-10季度设置问题)。我遇到的问题是,一旦用户正确输入了第一个问题,并且显示了第二个问题,如果用户输入了第二个问题的正确答案:a^2+b^2=c^2,则“对不起,您输了,您的分数为1/10,仍将显示”。即使它应该是:“正确的!下一个问题:”我刚刚开始编码,如果这是一个简单的错误,我感到非常抱歉

    import java.util.Scanner;

    public class Quiz {

public static void main(String[] args) {
    int Q1 = 0, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10 = 0;
    String Q2 = "";



    Scanner in = new Scanner(System.in);
    Scanner st = new Scanner(System.in);


    System.out.println("Welcome to the math quiz!, Type Reset at any time to start over");
    System.out.println("Q1: What is the square root of 9?");





        //beggining of questions:
    while (Q1 != 3) {
        Q1 = in.nextInt();
    if ( Q1 == 3) {
        System.out.println("Correct! Next Question:");
        System.out.println("Q2: What is pythagreoum's therom?");

    } else {
        System.out.println("Sorry, you lost, your score is 0/10");
    }
    }
  //end of Q1



    while (Q2 != "a^2+b^2=c^2" ) {
        Q2 = st.nextLine();
    if ( Q2 == "a^2+b^2=c^2" ) {
            System.out.println("Correct! Next Question:");  
    } 
    else {
        System.out.println("Sorry, you lost, your score is 1/10");
    }
    }

    //end of Q2 
            }
            }
尝试使用Q2.equals(“a^2+b^2=c^2”)

String
是一个对象,因此
Q2
是一个指针。将
Q2
与任何其他对象进行比较时,都会比较指针中的值(这是
字符串
的内存位置
Q2==///something
只有在
///something
也是
Q2
的内存地址(两个指针指向同一对象)时才会返回true。因此,您需要使用
equals()
在Java中比较
字符串时的方法


<>这不是你所问的具体问题,但是你的程序会给我键入其他正确答案,比如“代码> C^ 2=A^ 2+b^ 2</代码>,或者甚至类似于<代码> a^ 2 +b^ 2=c^ 2 < /代码>。在调用这个项目之前可能会考虑一些东西。

< p>比较<代码>字符串 >在使用
=
运算符时应特别小心。如果要比较的两个字符串都相同(即相同的字符串对象),它将只返回
true
。请使用
Q2.equals(“a^2+b^2=c^2”)
。有关
=
之间差异的更多信息,请阅读对于第二季度,它应该是:

while (!Q2.equals("a^2+b^2=c^2")) {
    Q2 = st.nextLine();
    if (Q2.equals("a^2+b^2=c^2")) {
        System.out.println("Correct! Next Question:");
    } else {
        System.out.println("Sorry, you lost, your score is 1/10");
    }
}

不要使用“==”来比较两个字符串。

您可以将所有问题存储在字符串数组中,如string[]questions=new string[n],其中n是要存储的问题数。您必须指定它。您可以对答案执行相同的操作。请注意,您可以使用string questions[]或string[]questions。 对于答案你也可以这样做

创建变量

String[] questions=new String[10];
String[] answers = new String[10];//make all answers upper case
questions[0]="Who solved fermat's last problem a^n+b^n=c^n where n>2?";
answers[0]="ANDREW WILES.";//fill up both arrays with 10 q's and a's
Scanner input=new Scanner(System.in);
String response;String rtoup;
Boolean useranswer;
int qlength=0;//the number of questions in the questions array
int score=0;//the user hasn't answered any questions yet
//Then could write a while for loop (I prefer the for loop)

for(int i=0;i<qlength;i++){
     System.out.println(questions[i]);
     response=input.Next();rtoup=response.toUpperCase();
   if(rtoup.equals(answers[i])){
     useranswer=true;score++;
  }
   if(useranswer==true)
   {
     System.out.println("Correct and your score is: "+score+"/10");
     }
   else 
     {
       System.out.println("False and your score is: "+score+"/10");
     }
}//end of for loop
对于
文件
在这种情况下的使用不是100%确定,但这至少会给你一些想法

关于
.equals
vs
=

如果您仍然想使用
=
,您可以使用
Integer.parseInt(“2132”)

搜索此站点以查找
字符串相等性
。。。
public void static main(String[] args){
   File file1=new File("file1path");
   File file2=new File("file2path")
   Scanner input1=new Scanner(file1);
   Scanner input2=new Scanner(file2);
  while(input1.hasNext()&&input2.hasNext())
    {
      //compare user response to input2
    }//end while
}//end main