Am new here and new in Java too ... am seeking help and knowledge from you guys ... Ive been working on methods for a week now and i have a guestion 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.
The main things are "for loop" and "variable assignment". 1) value "Number" variable is taking from user, after that you are assigning another value to it. remove that line Number = 1 + ( int ) ( Math.random() * 10);
2) place if condition in for loop as below
before wtring any program first write the required steps, take ur program as an example. read given problem line by line then convert into coding.
Step 1. need two numbers, generated using Math.random Ans) int firstNumber = 1 + ( int ) ( Math.random() * 10); int secondNumber = 1 + ( int ) ( Math.random() * 10);
Step 2. Aim of program to compare the product with user input Ans) int product = firstNumber * secondNumber; int input = JOptionPane.showInputDialog("How much is " +firstNumber+" times "+secondNumber+" ? ");
Step 3. Program has to check for corect answer, by asking input repeatedly Ans) put one "counter" variable to count the number of times user entered input. whenever the number of iterations are not know,use "while" loop
finally the ouput code will look like below
Hope this helps. All The Best [ March 22, 2005: Message edited by: sasi kala ]
Joined: Mar 22, 2005
Thank you :]
But i feel lost .. can't it be solved in a simpler way .. i mean without try and catch??
but then you lose the ability to recover (easily anyway) from the possibility that the user enters a non-integer for the answer .. your program will terminate with exceptions thrown to your operating system runtime.
Try it .. comment out the try-catch and change the signature to your main method as above, run the program and enter something like "a1" for your answer - the parseInt method will throw an exception and your program will abort in a very ugly way .. that's the whole idea behind exception handling in the first place.
[ March 22, 2005: Message edited by: John Dell'Oso ] [ March 22, 2005: Message edited by: John Dell'Oso ]
Also, I would advise against the use of "Number" as a variable name; Number is also a class found in the java.lang. package! Variable names should start with a lower-case letter (number would be fine, but not Number).
It is possible to do this without throwing exceptions. You could parse the number yourself (not recommended) or you could create your own dialog with a formatted text field that only accepts numbers (not recommended for beginners...). This way you add additional validation and you can ensure that the string will convert to a number without exception. However, just because you know that no exception is thrown does not mean that the compiler knows it. You would still need the try-catch block or the throws clause in the method declatation.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joined: Mar 22, 2005
mmmmmmmmmmmmmmmmmmmmmmmmmmm .. Can someone show me .. i dnt seem to understand how to act or start!
Joined: Mar 22, 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'd like to reiterate that variable names should start with lower case. If you follow this convention, you will avoid the name collision with classes that have the same name. Also, this helps other Java programmers be able to read your code more easily since to most of us here a name that starts with an upper case automatically suggests that it is for a class.
Hello all! Your website was recommended to me as a novice Java programmer. Having fun so far!
My question: in the code listed in this thread, the message dialog boxes give one the option of OK and Cancel. If I click on Cancel, it throws me into the Catch loop. It seems the only way out is to give it the correct answer!
While I'm sure this would appeal to the Nuns that taught me in grade school , is there an easy way to tell the code that "Cancel" is ok and let me go home? [ March 25, 2005: Message edited by: Joe Baumgarten ]
Joe B. <br /> <a href="mailto:firstname.lastname@example.org" rel="nofollow">email@example.com</a>