繁体   English   中英

如何让我的 Java 测验中的答案能够包含多个单词?

[英]How do I make an answer able to have more than one word in my java quiz?

我正在尝试为我的 GCSE 计算课程中的作业做一个测验,我需要一些帮助。 这段代码可以编译,但每当我运行代码并输入答案时,答案就会溢出到另一个问题的答案上。 例如,对于第一个问题,如果我在运行程序时键入答案为“某人先生”,则“先生”将成为第一个问题的答案,而“某人”将成为第二个问题的答案。

我不确定这是否与子字符串或什么有关,但请帮忙! :)。 如果您想知道为什么从未使用 iScore 变量的值,那是因为这不是完整的代码,只是前三个问题,分数打印在最后。 每个问题的“模板”没有区别,只是问题和答案不同。

public static void main(String[] args) throws InterruptedException
{
    //creates the Scanners needed
    Scanner szAnswer = new Scanner(System.in);
    Scanner szPlayAgain = new Scanner(System.in);

    //declares the variables for the answers
    String szFirstAnswer;
    String szSecondAnswer;
    String szThirdAnswer;

    //declares a variable for the score
    int iScore = 0;

    //declares a variable for whether or not the user wants to keep playing
    String szRestart;

    //creates a do while loop, keeps going as long as the user wants to play
    do {
        //tells the user that only one word answers are needed
        System.out.println("Only one word answers are needed!");

        //first question
        //Asks the question and collects the input
        System.out.println("Question 1:");
        System.out.print("Which Ancient Greek god was said to be the god of the sky? ");
        szFirstAnswer = szAnswer.next();

        //converts the answer to a format with the first letter upper case and the rest lower case
        szFirstAnswer = szFirstAnswer.substring(0,1).toUpperCase() + szFirstAnswer.substring(1);
        szFirstAnswer = szFirstAnswer.substring(0,1) + szFirstAnswer.substring(1).toLowerCase();

        //prints out the converted response
        System.out.println("You said: " + szFirstAnswer);

        //if the answer was Zeus, the print out correct and add 1 to the score
        if (szFirstAnswer.equals("Zeus")) {
            System.out.println("Correct!");
            iScore++;
        }
        //if the answer was not Zeus, print out a message with the correct answer
        else {
            System.out.println("Wrong! The correct answer was: Zeus");
        }

        //creates a line break
        System.out.println();


        //second question
        //Asks the question and collects the input
        System.out.println("Question 2:");
        System.out.print("Which team has won the most FA Cups since the competition started? ");
        szSecondAnswer = szAnswer.next();

        //converts the answer to a format with the first letter upper case and the rest lower case
        szSecondAnswer = szSecondAnswer.substring(0,1).toUpperCase() + szSecondAnswer.substring(1);
        szSecondAnswer = szSecondAnswer.substring(0,1) + szSecondAnswer.substring(1).toLowerCase();

        //prints out the converted response
        System.out.println("You said: " + szSecondAnswer);

        //if the answer was Arsenal, the print out correct and add 1 to the score
        if (szSecondAnswer.equals("Arsenal")) {
            System.out.println("Correct!");
            iScore++;
        }//end if

        //if the answer was not Arsenal, print out a message with the correct answer
        else {
            System.out.println("Wrong! The correct answer was: Arsenal");
        }//end else

        //creates a line break
        System.out.println();


        //third question
        //Asks the question and collects the input
        System.out.println("Question 3:");
        System.out.print("What is the currency of Japan? ");
        szThirdAnswer = szAnswer.next();

        //converts the answer to a format with the first letter upper case and the rest lower case
        szThirdAnswer = szThirdAnswer.substring(0,1).toUpperCase() + szThirdAnswer.substring(1);
        szThirdAnswer = szThirdAnswer.substring(0,1) + szThirdAnswer.substring(1).toLowerCase();

        //prints out the converted response
        System.out.println("You said: " + szThirdAnswer);

        //if the answer was Yen, the print out correct and add 1 to the score
        if (szThirdAnswer.equals("Yen")) {
            System.out.println("Correct!");
            iScore++;
        }//end if

        //if the answer was not Yen, print out a message with the correct answer
        else {
            System.out.println("Wrong! The correct answer was: Yen");
        }//end else

        //creates a line break
        System.out.println();

    }while (szRestart.equals("Y"));//end while

    //prints a thank you message to the screen
    System.out.println("Thanks for Playing!");

    //closes the scanners
    szAnswer.close();
    szPlayAgain.close();

}//end main

哪里有 szAnswer.next(); 你实际上应该有 szAnswer.nextLine(); 我相信。

.next() 获取输入的下一个单词,这就是为什么它停在第一个空格处,如 .nextLine(); 当你按下回车键时得到整行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM