0

嗨,我在用 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

4 回答 4

0

在比较Strings时,使用运算符时应该非常小心==。它只会true在被比较的两个字符串相同(即相同的 String 对象)时返回。使用Q2.equals("a^2+b^2=c^2"). ==更多关于和.equals阅读这个的区别

于 2013-10-04T21:47:41.163 回答
0

对于第二季度,它应该是:

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

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

于 2013-10-04T21:48:30.617 回答
0

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

String是对象,Q2指针也是。与Q2其他任何东西比较比较指针中的值(这是String. 的内存位置,仅当也是(指向同一个对象的两个指针)的内存地址时Q2 == //something才会返回 true 。因此,您需要在以下情况下使用该方法在 Java 中比较s。//somethingQ2equals()String

并不是说这与您要问的特定问题有任何关系,而是您的程序会因为输入其他正确答案(例如c^2=a^2+b^2,甚至类似A^2+B^2=C^2. 在您将此项目称为完成之前,可能需要考虑一些事情。

于 2013-10-04T21:42:49.823 回答
0

您可以将所有问题存储在字符串数组中,例如 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

或者,您可以编写所有问题并将它们保存在某个文本文件中,例如 file1,然后您可以让 java 从该 txt 文件中逐行读取。然后你可以将所有答案写在另一个文本文件中,比如 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

不是 100% 确定File在这种情况下使用,但这至少会给你一些想法

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

于 2013-10-05T03:34:58.700 回答