2013-11-04 89 views
-2

你好我是新來創建我自己的類。我創建了主程序,但不知道從我的自定義類開始運行程序所需的位置。如何在我的自定義類中設置構造函數?

這裏是我的主:

import java.util.Scanner; 

public class MathQuiz { 
    public static final char symbol_MULTIPLY = '*'; 
    public static final char symbol_ADD = '+'; 

    public static void main(String[] args) { 
    int choice; 

    ShowExampleProblems(); 

    Scanner inputReader = new Scanner(System.in); 

    do { 
     System.out.println("[1] Addition Quiz"); 
     System.out.println("[2] Multiplication Quiz"); 
     System.out.println("[3] Mixed Problem Quiz"); 
     System.out.println("[4] Quit"); 
     System.out.print("\nSelect ==>"); 

     choice = inputReader.nextInt(); 
     System.out.println(); 

     switch (choice) { 
     case 1: 
      AdditionQuiz(); 
      break; 
     case 2: 
      MultiplicationQuiz(); 
      break; 
     case 3: 
      MixedQuiz(); 
      break; 
     case 4: 
      break; 
     default: 
      System.out.println("Invalid selection."); 
      System.out.println(); 
     } 

    } 
    while (choice != 4); 

    System.out.println("Thanks for using the Math Quiz Program."); 
    System.out.println(); 

    } 

    /** 
    * *****************Static Methods******************** 
    */ 
    public static void ShowExampleProblems() { 
    System.out.println("Math Quiz - Sample Problems"); 
    System.out.println("---------------------------\n"); 

    MathProblem p_add = new MathProblem(symbol_ADD); 
    MathProblem p_multiply = new MathProblem(symbol_MULTIPLY); 
    System.out.println("Sample Addition Problem:\t" + p_add); 
    System.out.println("Sample Multiplication Problem:\t" + p_multiply); 
    System.out.println("\n"); 
    } 

    public static void AdditionQuiz() { 
    MathProblem p_add = new MathProblem(symbol_ADD); 
    DoQuiz(p_add); 
    } 

    public static void MultiplicationQuiz() { 
    MathProblem p_multiply = new MathProblem(symbol_MULTIPLY); 
    DoQuiz(p_multiply); 
    } 

    public static void MixedQuiz() { 
    MathProblem p_mixed = new MathProblem(); 
    DoQuiz(p_mixed); 
    } 

    public static void DoQuiz(MathProblem problem) { 
    Scanner keyboard = new Scanner(System.in); 

    int correct = 0; 
    int answer; 

    for (int i = 1; i <= 10; i++) { 
     problem.setNewValues(); 
     System.out.println(i + ") " + problem); 
     System.out.print("\tAnswer ==> "); 
     answer = keyboard.nextInt(); 
     if (problem.isCorrect(answer)) { 
     System.out.println("\tCorrect!\n"); 
     correct++; 
     } 
     else { 
     System.out.print("\tSorry, the correct answer is "); 
     System.out.println(problem.getAnswer() + "\n"); 
     } 
    } 

    System.out.println("You got " + correct + " out of 10 items correct.\n"); 
    } 
} 

這裏是我的自定義類:

import java.util.Random; 

public class MathProblem { 
    //************************************** 
    //***********MEMBER VARIABLES*********** 
    //************************************** 
    private int number1, number2; //The 2 numbers in the math problem 
    private char symbol;   //The operation: '+' for adding, '*' for multiplying 
    private boolean mixOperations; //Flag: can problem be either addition or multiplication? 

    //********************************** 
    //***********CONSTRUCTORS*********** 
    //********************************** 
    // One of these methods is called when the object is created 
    //--------------------------------------------------------------------------- 
    //Default Constructor 
    //Sets up a MathProblem object that can be either addition or multiplication 
    //Turns "mix" flag ON 
    //Generates initial values for problem 
    //--------------------------------------------------------------------------- 
    public MathProblem() { 
    mixOperations = true; 
    Random generator = new Random(); 
    number1 = generator.nextInt(10) + 1; 
    number2 = generator.nextInt(10) + 1; 
    } 

    //--------------------------------------------------------------------------- 
    //Alternate Constructor 
    //If a specific operation is designated, set up MathProblem object so that 
    // only that operation will be used 
    //Turns "mix" flag OFF 
    //Sets symbol to the operation specified (+ or *) 
    //Generates initial values for problem 
    //--------------------------------------------------------------------------- 
    public MathProblem(char operation) { 
    mixOperations = false; 
    Random generator = new Random(); 
    number1 = generator.nextInt(10) + 1; 
    number2 = generator.nextInt(10) + 1; 
    } 

    //*********************************** 
    //***********CLASS METHODS*********** 
    //*********************************** 
    //***********Methods to access information about the MathProblem object*************** 
    //--------------------------------------------------------------------------- 
    //Method: getAnswer 
    //Parameters: none 
    //Returns: the answer to the current math problem (an integer) 
    //--------------------------------------------------------------------------- 
    public int getAnswer() { 
    return 0; 
    } 

    //--------------------------------------------------------------------------- 
    //Method: isCorrect 
    //Parameters: answer to be checked (an integer) 
    //Returns: TRUE if answer is correct, FALSE if it is incorrect 
    //--------------------------------------------------------------------------- 
    public boolean isCorrect(int answer) { 
    return false; 
    } 

    //**********Method to update data in the MathProblem object********** 
    //--------------------------------------------------------------------------- 
    //Method: setNewValues() 
    //Parameters: none 
    //Returns: nothing (void) 
    //Task: Generate 2 new random numbers for the MathProblem object 
    //  Store these in the number1 and number2 data members 
    //  If problem allows mixed operations, randomly set symbol to + or * 
    //--------------------------------------------------------------------------- 
    public void setNewValues() { 
    } 

    //**********Method to create printable string representing MathProblem object********** 
    //--------------------------------------------------------------------------- 
    //Method: toString() 
    //Parameters: none 
    //Returns: A String representation of this object 
    //   The String returned is in one of the following formats, 
    //   depending on whether it is an addition or multiplication problem: 
    //    7 + 4 = ? 
    //    7 * 4 = ? 
    //--------------------------------------------------------------------------- 
    public String toString() { 
    return "?"; 
    } 
} 

任何反饋意見是非常感謝!

+0

你的問題是什麼? – buzzsawddog

+1

這看起來像一個家庭作業問題。 –

+0

如何在我的自定義類中設置構造函數?我很迷茫,我只需要指出正確的方向。 – user2953452

回答

1

使用

public MathQuiz(pars // if you need parameters) 
{ 

} 

把所有的類在他們自己的文件(ClassName.java),因爲它使代碼更容易閱讀和理解。

+0

這個,並把它放在它自己的文件(MathQuiz.java) – MadConan

+0

真的,我忘了我會添加 –

相關問題