# Calculator + Class, Do while error?

Page 1 of 1

## 2 Replies - 2221 Views - Last Post: 23 February 2010 - 09:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=157608&amp;s=c484b1e34538211e1e1f821d5b631d8d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Sarge_137

Reputation: 0
• 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
{
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);
System.out.println ("Is" + NewLine);
System.out.println ("The" + NewLine);
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 ("[|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|][|]");
System.out.println ("----------------------------------------------");
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 ();
System.out.println (num1 + " + " + num2 + " = ? ");

{
Count++;
cl.RefreshTotal ();
}
{
Count++;
cl.RefreshTotal ();

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

{
Count++;
cl.RefreshTotal ();

}
{
Count++;
cl.RefreshTotal ();
}
}
case 3:
{
num2 = (int) (Math.random () * 10);
num1 = (int) (Math.random () * 10);
cl.SetDataTimes (num1, num2);
cl.returnData ();
System.out.println (num1 + " * " + num2 + " = ? ");

{
Count++;
cl.RefreshTotal ();
}
{
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.returnData ();
System.out.println (num1 + " Divided By " + num2 + " = ? ");
{
Count++;
cl.RefreshTotal ();

}
{
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 ()
{
}
}

Any help is appreciated.

Is This A Good Question/Topic? 0

## Replies To: Calculator + Class, Do while error?

### #2 toshiro

Reputation: 21
• 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.

### #3 Sarge_137

Reputation: 0
• 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.