测验问题中的扫描仪

时间:2013-10-04 21:39:17

标签: java string operations

您好我在java中制作测验程序时遇到问题,这就是我所拥有的:(注意:我没有为Q3-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 
            }
            }

4 个答案:

答案 0 :(得分:0)

尝试Q2.equals("a^2+b^2=c^2")

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

并不是说这与您提出的具体问题有关,但是您的程序会因为键入c^2=a^2+b^2等其他正确答案,甚至类似A^2+B^2=C^2之类的错误来指责我。在你完成这个项目之前,可能需要考虑一些事情。

答案 1 :(得分:0)

在比较Strings时,使用==运算符时应该非常小心。如果被比较的两个字符串都相同(即相同的String对象),它将只返回true。使用Q2.equals("a^2+b^2=c^2")。有关==.equals阅读THIS

之间差异的更多信息

答案 2 :(得分:0)

对于Q2,它应该是:

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

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

答案 3 :(得分:0)

您可以将所有问题存储在字符串数组中,如String [] questions = new String [n],其中n是您要存储的问题数。你必须指定它。您可以为答案做同样的事情。请注意,您可以使用String questions []或String []问题。 你也可以为答案做同样的事。

创建变量

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

或者你可以写出所有问题并将它们保存在一些文本文件中,例如file1,然后你可以逐行从该txt文件中读取java。然后你可以把另一个文本文件中的所有答案写成file2。

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

在此上下文中对File使用不是100%肯定,但这至少会为您提供一些想法

关于.equals vs == 如果您仍想使用==,则可以使用Integer.parseInt("2132")将用户输入(假设它们是字符串数字)转换为整数;