I need help with this java question.?

I need help with this java question.? Topic: How to write a hello program in java
June 20, 2019 / By Lilian
Question: Hello, I am writing a math practice game, where the player will be given random numbers, and are supposed to enter the answer. If the answer is wrong, there will be a message saying "incorrect" with the correct answer displayed, if its right, the message will say "correct". I also have to use void methods in this program. Now my problem is that I wrote the program as an IF ELSE statement and there's always an error message on the ELSE part.and I still haven't figured out how to use methods. whenever i attempt to use a method, theres always an error message in my compiler. int numb1,numb2, sum,userAnswer Scanner keyboard = new Scanner(System.in); Random randNumbers=new Random(); numb1 = randNumbers.nextInt(10); numb2 = randNumbers.nextInt(10); System.out.println("What is the answer to the" + " " + "following problem?"); System.out.println(numb1 + "+" + numb2 + "=?"); sum= numb1 + numb2; userAnswer=keyboard.nextInt(); if (userAnswer==sum); System.out.println("Correct!"); else System.out.println("Wrong answer!" + "The correct answer is" + " " + sum + "/nTry again"); This is what I have so far.
Best Answer

Best Answers: I need help with this java question.?

Justine Justine | 7 days ago
My guess is you're using a semicolon right after the IF's condition, which is why to the compiler, the ELSE comes out of the blue. if (condition); // due to the semi colon, the if part ends here (and does nothing) command(); // this is executed no matter what else command(); // this causes an error, since there's no previous if The proper syntax is: if (condition) command(); else command(); or if (condition) { command1(); command2(); ... } else { command1(); command2(); ... } (or a combination of a block and a single command) As for other errors, we obviously need to see the code. Here's example code: http://ideone.com/5wShbI
👍 202 | 👎 7
Did you like the answer? I need help with this java question.? Share with your friends

We found more questions related to the topic: How to write a hello program in java

Justine Originally Answered: I have downloaded Java language & API documentation from java.sun.com. How to start running Java?
Unfortunately, the other answerer is correct in saying java 1.5 is the latest version. I am assuming you have followed the installation instructions and are able to do the following: (1) open a command line window, if in MS Windows this is Command Prompt, or the old MS DOS Prompt. (2) type 'java' without the quotes at the command prompt. (2.1) if you get a help message, then you are ok to run programs. (2.2) if not, you have to install according to the instructions. :( (3) this time type 'javac' at the prompt, this should also produce a help message. (3.1) if you did not get the message, you need to install properly. :( Now that we have established you can compile and run the programs, you need to write the programs. Open a text file, and type the following in: public class HelloAnswers { public static main(String[] args) { System.out.println("Hello Yahoo! Answers!"); } } Then save the file as HelloAnswers.java ** Hopefully, the file has not been saved with a .txt extension, such as HelloAnswers.java.txt ** If it has, then you will need to rename the file, and remove the .txt Now, on the command line, change directory (folder) to the directory (folder) that contains the newly created HelloAnswers.java file. Type on the command line 'javac HelloAnswers.java' without the quotes, to compile the file. This should produce a HelloAnswers.class file upon successful compilation (compile). You can then type 'java HelloAnswers' on the command line, and it should run the program, and print out. 'Hello Yahoo! Answers!' without the quotes. To run in the java web start, you need to make a jar (java archive) file, and possible create a Graphical User Interface (GUI). For the GUI, you can use AWT or javax.swing Read the API docs, and have a look for some books on java as well. I found some books called 'Core Java' published by Sun Microsystems Press to be of valuable assistance in learning java, but some others may suffice. [edit] Obviously the code tags do not work :P
Justine Originally Answered: I have downloaded Java language & API documentation from java.sun.com. How to start running Java?
Well, first, I would have reccomended downloading JDK 1.5 but... Second, you need to set the path and classpath environmental variables in Windows. After you've done that, you can open a DOS shell to get a command line and type > javac MyApp.java > java MyApp I recommended setting a right click option in Windows so you can open a DOS shell to any folder you right click on. Addendum: Right click 'My Computer' select Properties at the bottom, then Advanced Tab, then Environmental Variables button. To the System EnVars you need to append a path the JDK's javac location. If there isn't already a classpath, add one to the user vars - classpath=.; You may need to Google setting-up classpath and path in Windows. This the source of a lot of frustration for new Java programmers on Windows. Or just download NetBeans Java IDE.
Justine Originally Answered: I have downloaded Java language & API documentation from java.sun.com. How to start running Java?
First do the path setting after down load eclipse its free u have to create a project from new link and crate a project and give some path after u create java class and give name of that class after writing program right click on project and click run tab and configure main class name in class and run ur program eclipse is good tool u can debug using this tool it will give options like vb when ur using eclips u will feel comfort write java programs ok ALL THE BEST
Justine Originally Answered: I have downloaded Java language & API documentation from java.sun.com. How to start running Java?
Download JCreator, it is a nice Free editor. Java is just the language / compiler, you need an editor to write the code in.

Justine Originally Answered: Small Java Question?
The answer is 11, but it's not calculated the way that you did it. Since x = 5 case 5 will execute, adding 3 to 5. Then the rest of the case statements will execute until either the end of the switch statement or until a break statement is encountered. So in this case you have cases 5 through 9 execute since there are no break statements. (Also there should be a semi colon after the x++ in case 9 but I assume that's just a typo). So you would get 5 + 3 + 1 + 2 -1 + 1 = 11. But if there were break statements after each case the answer would be 8 so that's probably your best bet.
Justine Originally Answered: Small Java Question?
There are no break statements in the code you have supplied so as it stands the answer is 11. I would assume break statements after each case and say the answer required is 8. Have fun.

If you have your own answer to the question how to write a hello program in java, then you can write your own version, using the form below for an extended answer.