PDA

View Full Version : Method


NichoLe
03-24-2005, 07:22 PM
Hello :rolleyes:

i have a question which i don't seem to understand how it goes ... i tried it but i need you guys to check if am on the right track ...


Question:
Create a class LearnToMultiply

The class should contain one main method. In the main method, use Math.random method to produce two positive one digit integers(between 1 and 10). The program should ask the user about the result of multiplying these two integers. "HOW MUCH IS 3 TIMES 7?"

The user will input the answer. The program checks the answer, if it is correct, it will display "Excellent, You got it". If it is wrong it will display "No, Try again". The user will continue to enter until he guesses the correct answer. The program will display at the end, the number of trials.





import javax.swing.JOptionPane;
import java.lang.Math;

class LearnToMultiply
{
public static void main (String args[])
{

String strmsg1, strmsg2, strmsg3;
String strNumber, strUserNum;
int Number, Number1, Number2, intUserNum;
int counter, intCount;

//ask the user to answer the question
strNumber= JOptionPane.showInputDialog("How much is 3 times 7? ");
Number= Integer.parseInt (strNumber);//converting the entered data [[string]] to integer

strmsg1= "Excellent. You got it";
strmsg2= "No. Try again";

//creating random numbers
Number1 = 1 + ( int ) ( Math.random() * 10);
Number2 = 1 + ( int ) ( Math.random() * 10);

counter = 0; //setting counter to 0


if (Number == 21) //the guessed number is equal to 21

{

JOptionPane.showMessageDialog(null, strmsg1);

}

else //the guessed number is not equal to 21

{

JOptionPane.showMessageDialog(null, strmsg2);

}

while (intUserNum != 21)

{

strUserNum = JOptionPane.showInputDialog("Guess another number");
intUserNum = Integer.parseInt(strUserNum);
intCount = count + 1; //adding to the number of trials

}



}// end loop

}// end class

Jason
03-24-2005, 08:34 PM
Though you don't really ask a question nor can I give you code for your homework I will try and explain what I think you should do without using code.

first, it seems that the math.random function that creates two seperate intergers and the ones that are supposed to be asked of to the user are the same...they are hard coded as 3*7 right now in your program, that needs to be changed. Your while loop and if statements are contradicting and basically asking the same thing, you should have the if statements in a single infinite loop and when the question is right or wrong display the appropriate answer.



Jason

tboss132
03-24-2005, 08:34 PM
I don't know Java but after studying your code, i think the problem will be the while loop.
I would do it like this
1) set counter = 0
2) Input number
if right
show right message
else
show wrong message
intCount = count + 1; //adding to the number of trials

If inCount = Number of trials availble
exit
else
Return to step 2
Hope this made some sense.
Hey Jason, you posted seconds before me

NichoLe
03-25-2005, 11:26 AM
Thank you all :)

I got it :thumbsup:


import javax.swing.JOptionPane;
import java.lang.Math;

class LearnToMultiply
{
public static void main (String args[])
{

int num1,num2,product;
int count, inputVal;
String input;


// pick two random integers from 1 to 10
num1 = 1 + (int)(Math.random() * 10);
num2 = 1 + (int)(Math.random() * 10);
product = num1 * num2;

// ask user for product
count = 0;


do
{
input = JOptionPane.showInputDialog("How much is " + num1 +
" times " + num2 + "?");
inputVal = Integer.parseInt(input);
count++;

if(inputVal == product)
JOptionPane.showMessageDialog(null, "Excellent, you got it in " +
count + " attempts.");
else
JOptionPane.showMessageDialog(null, "No, try again.");
}
while(inputVal != product);

System.exit(0);
}
}




Thank you again