I've been trying to write my first code ever..
I think I somehow managed to do it but there is a compiler error and I can't figure out what it is.
The program suppose to ask the user for 5 numers and then it print the maximum value and the minumum value.
Here is what I wrote:
and here is the error I get
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.util.Scanner.netxtInt
Enter five numbers
the wrrors are in these lines
a = keyboard.netxtInt ();
b = keyboard.netxtInt ();
c = keyboard.netxtInt ();
d = keyboard.netxtInt ();
e = keyboard.netxtInt ();
PS. I know there maybe another way to do it but I want to do it this way, and please exuse the mess because I am totaly new to this forum.
Edit: A more general comment - I'd suggest using an array of ints instead of five separate variables, as you'll be able to replace your complicated else/if statements with a loop and a simple test. Imagine what happens if you're now asked "that's great, but can you modify it to take 6/7/8 numbers instead?". With an array, that would be a matter of changing one number. With your approach, you've got a lot of changes to make.
it was really a silly mistake
I'm sure it is because I didn't sleep unitl now trying to write this code
thanks for your comment , you are write about the arry thing , and that is why I mentioned in my post that I know there are other methods ut I was asked to do it this way without arrys as I am totaly beggener to programing and I need to learn it step y step :-)
I'd start with using >= instead of >. If "a" > "b" then "a" is the max of "a" and "b". But if "a" >= "b" then "a" is still the max of "a" and "b". It's just that "b" might also be the max in the case when "a" == "b".
The easy way is of course to use nested calls to Math.max / Math.min:
Is there any modifications you suggest me to do with it ?
Rob thanks for the advice about using math.max it would really make it a lot easier but as I said I'm trying to learn java step by step and I would defenetly use your advice in the future
I worked on the code that was submitted to the forum the first time.
I was able to get this running I did a test case using numbers in this sequence1,2,3,4,5.
Result was Max was 5 Min was 1
Then I reverse the order of the sequence 5,4,3,2,1.
Result was Max was 5 Min was 1.
The last entry I noticed you had two sets of decision blocks. You might want to try using the decision block in the code with the scanner 9from the first version of your code) with the JOptionPane one (the last update to your code) and see if it works. Your algorithm(if/else statements) worked in the first version of your code. See if it works.
You might want to test the code using numbers like 10, 3, 30, 0, and 5 to test the way the program handles out of sequence numbers. Here's the code:
Try to trace the process using paper and pencil then when you think you have a understanding then compile and see if you were right. This will make you think like a compiler and you can start reading code. Note I commented out the code at the beginning of the program. Check out the comments I gave you some pointers.
Learning starts with a question and continues with a discovery Stay hungry for knowledge.