Calculator + Class, Do while error?

Calculator + Class, Do while error?

Page 1 of 1

2 Replies - 1642 Views - Last Post: 23 February 2010 - 09:25 AM Rate Topic: -----

#1 Sarge_137  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 16-December 08

Calculator + Class, Do while error?

Posted 22 February 2010 - 11:23 AM

Hey all,
Basically I have made a program that asks you questions while randomizing two numbers and what operator is used in each question. You can choose the difficulty which affects the length of the game, however the count++ which is meant to increment is not doing so effectively and sometimes counts to 12 etc before realising it has reached the condition, I made a class for the program below is my program code:
/* The "MathsGame" class.





 * Function: A maths game for children, uses decisions, loops and uses my own class ('CalculationClass') for calculations as it seemed easier.

 * Error: Do while loop, isnt reaching condition.

 */
import java.io.*;
import java.awt.*;
public class MathsGame
{
    public static void main (String[] args) throws Exception
    {
        BufferedReader inKb = new BufferedReader (new InputStreamReader (System.in));
        CalculationClass cl = new CalculationClass (); //declaration of new CalculationClass cl
        int num1 = 0, num2 = 0, Operator = 0, Answer = 0, CorrectAnswerCount = 0, IncorrectAnswerCount = 0, Total, Count = 0, roundLimit = 0;
        double num3 = 0, num4 = 0, Total2 = 0;
        int difficulty = 0;
        String NewLine = "\n";
        int ans = 0;
        double ans2 = 0;
        //Intro:
        System.out.println ("This!" + NewLine);
        Thread.sleep (200);
        System.out.println ("Is" + NewLine);
        Thread.sleep (200);
        System.out.println ("The" + NewLine);
        Thread.sleep (200);
        System.out.println ("[|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|]");
        System.out.println ("[|]                                                            [|]");
        System.out.println ("[|]  [|][|][|][|][|]  [|][|][|] [|][|][|] [|]   [|] [|][|][|]  [|]");
        System.out.println ("[|]  [|]   [|]   [|]  [|]   [|]    [|]    [|]   [|] [|]        [|]");
        System.out.println ("[|]  [|]   [|]   [|]  [|]   [|]    [|]    [|][|][|] [|][|][|]  [|]");
        System.out.println ("[|]  [|]   [|]   [|]  [|][|][|]    [|]    [|]   [|]       [|]  [|]");
        System.out.println ("[|]  [|]   [|]   [|]  [|]   [|]    [|]    [|]   [|] [|][|][|]  [|]");
        System.out.println ("[|]                                                            [|]");
        System.out.println ("[|]                                                            [|]");
        System.out.println ("[|] [|][|][|][|][|]                            [|][|][|][|]    [|]");
        System.out.println ("[|] [|]         [|]                            [|]             [|]");
        System.out.println ("[|] [|]                                        [|]             [|]");
        System.out.println ("[|] [|]              [|][|][|] [|][|][|][|][|] [|]             [|]");
        System.out.println ("[|] [|]              [|]   [|] [|]   [|]   [|] [|][|][|]       [|]");
        System.out.println ("[|] [|]      [|][|]  [|]   [|] [|]   [|]   [|] [|]             [|]");
        System.out.println ("[|] [|]         [|]  [|][|][|] [|]   [|]   [|] [|]             [|]");
        System.out.println ("[|] [|][|][|][|][|]  [|]   [|] [|]   [|]   [|] [|][|][|][|] V.1[|]");
        System.out.println ("[|]                                                            [|]");
        System.out.println ("[|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|]");
        Thread.sleep (200);
        System.out.println ("----------------------------------------------");
        Thread.sleep (200);
        System.out.println ("New User? Enter UserName please: ");
        String UserName = inKb.readLine ();
        System.out.println ("Hello " + UserName);
        System.out.println ("Try Answer All the Questions Correctly!" + NewLine);
        System.out.println ("How difficult do you wish the game to be?" + NewLine);
        System.out.println ("(5) Short: easy" + NewLine);
        System.out.println ("(4) Medium: beginner" + NewLine);
        System.out.println ("(3) Long: Intermediate" + NewLine);
        System.out.println ("(2) Longer: Difficult" + NewLine);
        System.out.println ("(1) Large amount of questions: Insane!" + NewLine);
        System.out.println ("What difficulty?" + NewLine);
        //difficulty affects length of game.
        //Do while loop.
        do
        {
            difficulty = Integer.parseInt (inKb.readLine ()); //sets difficulty based on user input.
            Total = 0;
            //Sets roundLimit for do while loop.
            if (difficulty == 1)
            {
                roundLimit = 30;
            }
            if (difficulty == 2)
            {
                roundLimit = 25;
            }
            if (difficulty == 3)
            {
                roundLimit = 20;
            }
            if (difficulty == 4)
            {
                roundLimit = 15;
            }
            if (difficulty == 5)
            {
                roundLimit = 10;
            }
            Operator = (int) (Math.random () * 4);
            switch (Operator) //switch case
            {
                case 1:
                    {
                        num2 = (int) (Math.random () * 10);
                        num1 = (int) (Math.random () * 10);
                        cl.SetDataPlus (num1, num2);
                        cl.returnData ();
                        int answer = cl.returnData ();
                        System.out.println (num1 + " + " + num2 + " = ? ");
                        ans = Integer.parseInt (inKb.readLine ());

                        if (ans == answer)
                        {
                            System.out.println (answer + " Is the correct answer!");
                            CorrectAnswerCount++;
                            Count++;
                            cl.RefreshTotal ();
                        }
                        else if (ans != answer)
                        {
                            System.out.println ("No, The correct answer was: " + answer);
                            IncorrectAnswerCount++;
                            Count++;
                            cl.RefreshTotal ();

                        }
                    }
                case 2:
                    {
                        num2 = (int) (Math.random () * 10);
                        num1 = (int) (Math.random () * 10);
                        cl.SetDataMinus (num1, num2);
                        cl.returnData ();
                        int answer = cl.returnData ();
                        System.out.println (num1 + " - " + num2 + " = ? ");
                        ans = Integer.parseInt (inKb.readLine ());

                        if (ans == answer)
                        {
                            System.out.println (answer + " Is the correct answer!");
                            CorrectAnswerCount++;
                            Count++;
                            cl.RefreshTotal ();

                        }
                        else if (ans != answer)
                        {
                            System.out.println ("No, The correct answer was: " + answer);
                            IncorrectAnswerCount++;
                            Count++;
                            cl.RefreshTotal ();
                        }
                    }
                case 3:
                    {
                        num2 = (int) (Math.random () * 10);
                        num1 = (int) (Math.random () * 10);
                        cl.SetDataTimes (num1, num2);
                        cl.returnData ();
                        int answer = cl.returnData ();
                        System.out.println (num1 + " * " + num2 + " = ? ");
                        ans = Integer.parseInt (inKb.readLine ());

                        if (ans == answer)
                        {
                            System.out.println (answer + " Is the correct answer!");
                            CorrectAnswerCount++;
                            Count++;
                            cl.RefreshTotal ();
                        }
                        else if (ans != answer)
                        {
                            System.out.println ("No, The correct answer was: " + answer);
                            IncorrectAnswerCount++;
                            Count++;
                            cl.RefreshTotal ();
                        }
                    }
                case 4:
                    {
                        num3 = (int) (Math.random () * 10);
                        double nrestrict1 = num3 * 2;
                        num4 = (int) (Math.random () * 10);
                        double nrestrict2 = num4 * 2;
                        if (num3 < num4)
                        {
                            num3 = (int) (Math.random () * 10);
                            nrestrict1 = num3 * 2;
                            num4 = (int) (Math.random () * 10);
                            nrestrict2 = num4 * 2;
                        }
                        cl.SetDataDivide (num1, num2);
                        cl.returnData ();
                        int answer = cl.returnData ();
                        System.out.println (num1 + " Divided By " + num2 + " = ? ");
                        ans2 = Double.parseDouble (inKb.readLine ());
                        if (ans == answer)
                        {
                            System.out.println (answer + " Is the correct answer!");
                            CorrectAnswerCount++;
                            Count++;
                            cl.RefreshTotal ();

                        }
                        else if (ans != answer)
                        {
                            System.out.println ("No, The correct answer was: " + answer);
                            IncorrectAnswerCount++;
                            Count++;
                            cl.RefreshTotal ();
                        }
                    }

            }
        }



        while (Count <= roundLimit); //do while loop condition: exectute the do loop while Count is less than or equal to roundLimit which is set using the

        System.out.println (UserName + " got:" + CorrectAnswerCount + " Correct and " + IncorrectAnswerCount + " Wrong, Out of: " + roundLimit);

    }
} // main method
// MathsGame class


And here is my class:
/*

 *Class: CalculationClass
 
 *Function: Easier to work out Calculations using the class I made.
 
  
 
*/

class CalculationClass //constructor
{

    private int number1, number2, total; //declaration of variables
    private double number3, number4, total2; //accessable only to class
    public CalculationClass ()
    {
    }


    public void SetDataPlus (int NumHold1, int NumHold2)
    {
        number1 = NumHold1; //assigns data to 'number1' + 'number2'
        number2 = NumHold2;
        total = number1 + number2; //assigns data to total dependant on number1, 2.
    }


    public void SetDataMinus (int NumHold1, int NumHold2)
    {
        number1 = NumHold1;
        number2 = NumHold2;
        total = number1 - number2;
    }


    public void SetDataTimes (int NumHold1, int NumHold2)
    {
        number1 = NumHold1;
        number2 = NumHold2;
        total = number1 * number2;
    }


    public void SetDataDivide (int NumHold1, int NumHold2)
    {
        number3 = NumHold1;
        number4 = NumHold2;
        total2 = number1 / number2;
    }
 public void RefreshTotal ()
 {
 total = 0;
 total2 = 0;
 }

    public int returnData ()
    {
        return total; //returns data
    }
}


Any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Calculator + Class, Do while error?

#2 toshiro  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 137
  • Joined: 27-June 09

Re: Calculator + Class, Do while error?

Posted 22 February 2010 - 11:47 AM

a do while first executes the code, and then checks the condition, switch to a regular while loop, which checks the condition, executes, and checks the condition to see if the while block will continue to execute.
Was This Post Helpful? 0
  • +
  • -

#3 Sarge_137  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 16-December 08

Re: Calculator + Class, Do while error?

Posted 23 February 2010 - 09:25 AM

Thanks, you were correct. While loop was needed.

Thanks again.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1