/    Sign up×
Community /Pin to ProfileBookmark

Methods again !!

Good Afternoon ..

As you may have noticed i’ve been working on methods for a week now and 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.

[code]
import javax.swing.JOptionPane;
import java.lang.Math;

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

String strmsg1, strmsg2, strmsg3;
String strNumber;
int Number;
int counter;

strNumber= JOptionPane.showInputDialog(“How much is 3 times 7 ? “);
Number= Integer.parseInt (strNumber);

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

Number = 1 + ( int ) ( Math.random() * 10);
counter = 0;

if (Number == 21)
{
JOptionPane.showMessageDialog(null, strmsg1);
}

else if (Number != 21)
{
JOptionPane.showMessageDialog(null, strmsg2);
}

for ( counter= 0; counter < 11; counter++);

}// end for

}// end class
[/code]

I feel dizzy :p from 2 hours am working on this

:p

to post a comment
Java

5 Comments(s)

Copy linkTweet thisAlerts:
@NichoLeauthorMar 23.2005 — Help:rolleyes:
Copy linkTweet thisAlerts:
@daveyMar 23.2005 — what is wrong with it?
Copy linkTweet thisAlerts:
@Khalid_AliMar 23.2005 — [i]Originally posted by davey [/i]

[B]what is wrong with it? [/B][/QUOTE]


Can't you tell?

I will see if I get some time 2night when I get home and try to help u out.
Copy linkTweet thisAlerts:
@ccoderMar 23.2005 — Inside a continuous loop:

1) generate 2 random numbers (not 1 as in your code)

2) calculate the result

3) zero the attempts counter

4) generate the prompt string using the 2 random numbers

5) inside a 2nd continuous loop(internal to 1st loop):

5a) increment the attempts counter

5b) prompt the user for an answer

5c) compare the user's answer to the computed result - if the answer is correct, show message strmsg1 else show message strmsg2

BTW, the test [b]if (Number != 21)[/b] is not necessary. If you hit the [b]else[/b] code, you know the answer was wrong.
Copy linkTweet thisAlerts:
@NichoLeauthorMar 24.2005 — Thank you guys for your help ?

I really appreciate it ... I tried to work on your hints and comments and this is what i reaches ...

<i>
</i>import javax.swing.JOptionPane;
import java.lang.Math;

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

<i> </i> String strmsg1, strmsg2, strmsg3;
<i> </i> String strNumber, strUserNum;
<i> </i> int Number, Number1, Number2, intUserNum;
<i> </i> int counter, intCount;

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

<i> </i> strmsg1= "Excellent. You got it";
<i> </i> strmsg2= "No. Try again";

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

<i> </i> counter = 0; //setting counter to 0


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

<i> </i> {

<i> </i> JOptionPane.showMessageDialog(null, strmsg1);

<i> </i> }

<i> </i> else //the guessed number is not equal to 21

<i> </i> {

<i> </i> JOptionPane.showMessageDialog(null, strmsg2);

<i> </i> }

<i> </i> while (intUserNum != 21)

<i> </i> {

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

<i> </i> }



<i> </i> }// end loop

<i> </i>}// end class


:rolleyes: Hope that i am on the right track
×

Success!

Help @NichoLe spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...