-2

我正在学习编程,我想知道为什么我会出错,我不知道从哪里开始。请忽略静态无效问题_四到十和评论。我还需要有关如何改进代码的提示。感谢您的帮助,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
4

2 回答 2

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

请注意,您永远不需要从包中导入类(如MathStringjava.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!");
}

显然,您的程序中还需要更多内容,但也许这会让您走上一条更有成效的道路。

于 2013-05-01T02:00:23.270 回答
0

>=大于或等于不应该=>


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

将方法定义更改为:

public static void say_Before_First_Question()


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

于 2013-05-01T01:14:23.310 回答