QuizGame和提示中的语法错误

时间:2013-05-01 01:04:26

标签: java

我正在学习编程,我想知道为什么我会收到错误,我不知道从哪里开始。请忽略static void Question_Four to Ten和评论。我还需要有关如何改进代码的提示。谢谢您的帮助, ThawingOrb。

/**
 * @(#)QuizGameFinal2.java
 *
 *
 * @author
 * @version 1.00 2013/4/30
 */

import java.util.Scanner;
import java.lang.Math;
import java.lang.String;
public class QuizGameFinal2
{

    public static void main(String[]args)
    {

    int option_Selected;
    int option_Single_Player = 1;
    int option_Multiplayer = 2;
    int answer;
    int player_One_Answer;
    int player_Two_Answer;
    String response;
    int player_One_Winnings;
    int player_Two_Winnings;
    int winnings;
    int computer_Winnings;
    double computer_Answer;

    Scanner input2 = new Scanner(System.in);


    say_Intro();
    say_Before_First_Question();
    question_One();
    human_Answer();
    do
    {
      System.out.println("Enter a number between 1 and 3");
    } while (answer < 1 && answer => 4);

    if (answer == 1)
    {
        System.out.println("Correct Next Question");
        question_Two();
        human_Answer();
        do
        {
          System.out.println("Enter a number between 1 and 4");
        } while (answer < 1 && answer >= 5);

        if (answer == 1 )
        {
            System.out.println("Correct Next Question");
            question_Three();
            human_Answer();
        do
        {
          System.out.println("Enter a number between 1 and 4");
        } while (answer < 1 && answer >= 5);


            if (answer == 4)
            {
                System.out.println("Correct Next Question");

            }

            else
            {
                System.out.println("Incorrect you win 1000 dollers");
                winnings = 1000;
            }

        }

        else
        {
            System.out.println("Incorrect you win 500 dollers");
            winnings = 500;
        }


    }

    else
    {
        System.out.println("Incorrect you win 0 dollers");
        winnings = 0;
    }





    } // End of main method




    static void say_Intro()     // Intro Method
    {
    System.out.println("Welcome to the QuizGame");          // Player selects which mode
    System.out.println("Press 1 for Single Player");
    System.out.println("Press 2 for Multiplayer");

    Scanner input = new Scanner(System.in);                                 // Scanner for the entire game
    int option_Selected = input.nextInt();

    do {
        System.out.println("Please enter 1 or 2");
       } while (option_Selected != 1 || option_Selected != 2);

    }


    public void say_Before_First_Question()     // Before game method
        {
            System.out.println("Welcome to the quiz game.");
            Scanner input = new Scanner(s);
            System.out.print("Your response");
            response = input.next();
            System.out.println("");
            System.out.println(" If you get a question wrong your out ok?");
            System.out.print("Your Response:");
            response = input.next();
            System.out.println("Also you will be competing against a super computer, after you play then he will generate answers, if you have the most then you win");
            System.out.println("Ok first question");
        }

    static void human_Answer ()         // Human Answer Single Player Method
    {

        int answer=input2.nextInt();
    }

    static void player_One_Answer()     // Player One Multiplayer Method
    {
        int player_One_Answer = input2.nextInt();
    }

    static void player_Two_Answer()     // Player two multiplayer Method
    {
        int player_Two_Answer = input2.nextInt();
    }

    static void computer_Answer ()      // Computer answer
    {
        double computer_Answer = (1-1 + 1) * Math.random + 1;
               computer_Answer = (int)computer_Answer;
    }

    static void question_One()          // Question 1 method
    {
        System.out.println("What is an application");
        System.out.println("1: A program that performs a task   2:A mouse   3: java.util.Scanner");


    }

    static void question_Two()      // Question 2 to 10 methods below
    {

        System.out.println("What is the data type that hold the value 1");
        System.out.println("1: int  2:float 3: short 4: long ");

        do
        {
            System.out.println("Enter a number between 1 and 4");
        } while (answer < 1 && answer > 5);

    }

    static void question_Three()
    {
        System.out.println("What is a not a high level language");
        System.out.println("1: Java     2:C++   3: Colbolt      4: Machine Language ");

        do
        {
            System.out.println("Enter a number between 1 and 3");       // Do this if the person enters a number less than one and greater than 3
        } while (answer < 1 && answer > 4);

    }

/*  static void question_Five()
    {

    }

    static void question_Six()
    {

    }

    static void question_Seven()
    {

    }

    static void question_Eight()
    {

    }

    static void question_Nine()
    {

    }

    static void question_Ten()
    {

    }

    static void total_Winnings_Single_Player ()     // Calculating who wins method single player
    {
        if (computer_Winnings > winnings)
        {
            System.out.println("Computer Wins");
        }

            else
            {
                System.out.println("You win");
            }
            */
    }




  // End of program

2 个答案:

答案 0 :(得分:1)

你的程序是一个几乎无法解读的代码纠结。让我们改进您的程序结构。首先,请注意,处理问题的逻辑对于所有问题都是相同的。定义封装特定于问题的数据的Question类会很有用。然后,您可以编写一次逻辑。 Question类可能如下所示(包括用于初始化Question对象的构造函数):

class Question {
    /** The text of the question itself */
    public String question;

    /** The array of possible answers */
    public String[] answers;

    /** The index (zero-based) of the correct answer */
    public int correctAnswer;

    /** Construct a Question with the given values */
    public Question(String question, String[] answers, int correct) {
        this.question = question;
        this.answers = answers;
        this.correct = correct;
    }
}

让问题打印出来也是有道理的。因此,让我们在printQuestion类中添加Question方法:

class Question {
    . . . // as above

    public void printQuestion() {
        System.out.println(question);
        for (int i = 0; i < answers.length; ++i) {
            System.out.print((i + 1) + ": " + answers[i] + "   ");
        }
        System.out.println();
    }
}

现在让我们转向主程序。每次需要用户输入时,您都在创建一个新的Scanner对象。您只需要一个Scanner,但需要它才能访问所有需要它的代码。有两个选项:向需要输入的每个方法添加Scanner参数,或者使Scanner成为QuizGameFinal2类的实例变量。后者最有意义:

import java.util.Scanner;

public class QuizGameFinal2 {
    Scanner input2;

    public static void main(String[] args) {
        input2 = new Scanner(System.in);
        . . .
    }
}

请注意,您永远不需要从Math包导入类(例如Stringjava.lang);它们会自动导入。现在您可以开始编写用于处理问题的逻辑。首先,编写一个打印问题并获得用户答案的​​一般方法(这将是QuizGameFinal2的成员):

public static int questionAndAnswer(Question question) {
    question.printQuestion();
    int n = question.answers.length;
    int answer = input2.nextInt();
    while (answer < 1 || answer > n) {
        System.out.println("Enter a number between 1 and " + n);
        answer = input2.nextInt();
    }
    return answer - 1; // subtract 1 so it is a zero-based index
}

现在我们可以继续开发main()方法了。以下是它的概述:

public static void main(String[] args) {
    input2 = new Scanner(System.in);
    Question[] questions = {
        new Question("What is an application?",
            new String[] {"A program that performs a task",
                          "A mouse",
                          "java.util.Scanner"},
            0
        ),
        new Question("What is the data type that hold the value 1?",
            new String[] {"int", "float", "short", "long"},
            0
        ),
        . . . // etc.
    }
    say_Intro();
    say_Before_First_Question();
    for (Question q : questions) { // iterates through the questions in order
        int answer = questionAndAnswer(q);
        if (answer == q.answer) {
            System.out.println("Correct! Next Question");
        } else {
            System.out.println("Incorrect. You're out!");
            return;
        }
        // TODO: get computer answer; calculate winnings
    }
    System.out.println("Done!");
}

在您的计划中,您显然需要更多,但这可能会让您走上更高效的道路。

答案 1 :(得分:0)

大于或等于>=而不是=>


方法say_Before_First_Question需要是静态的,才能从另一个静态方法(main)调用。

将方法定义更改为:

public static void say_Before_First_Question()


Math.random是一种方法,因此您需要()。它应该是Math.random()