Results 1 to 5 of 5
- 10-23-2010, 05:24 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 23
- Rep Power
- 0
Any one help me fix JAVA code and explain ?
In this code , I can not execute this line
Java Code:if (positiveN == 0){ JOptionPane.showMessageDialog (null,"this is a void number"); }
This is original code
Java Code:import javax.swing.*; public class positiveNum{ public static void main (String[]args){ String userInput; int positiveN; userInput = JOptionPane.showInputDialog ("Enter a number here"); positiveN = Integer.parseInt (userInput); while ( positiveN != -1){ if (positiveN <0){ JOptionPane.showMessageDialog (null,"this is negative number"); }else if (positiveN >0){ JOptionPane.showMessageDialog (null,"this is positive number"); } userInput = JOptionPane.showInputDialog ("Enter a positive number here"); positiveN = Integer.parseInt (userInput); } [COLOR="Red"]if (positiveN == 0){ JOptionPane.showMessageDialog (null,"this is a void number"); }[/COLOR] JOptionPane.showMessageDialog (null,"End of processing"); } }
But in another code of my teacher , it work perfectly . But it look the same as my code .Anyone know why ? please explain for me , Thank so much.
This is my teacher code
Java Code:import java.util.Scanner; public class improvedGuessingGame { public static void main (String[] args) { Scanner kbd = new Scanner(System.in); int userGuess; int hiddenNumber; int numGuess = 0; hiddenNumber = (int) (Math.random() *11); System.out.print("Guess a number between 0 and 10: "); userGuess = kbd.nextInt(); numGuess++; while ((userGuess != hiddenNumber) && (numGuess <= 4)) { if ((userGuess < 0) || (userGuess > 10)) { System.out.println("You seem to have poorly understood the game."); } else if (hiddenNumber > userGuess) { System.out.println("You guessed too low."); } else { System.out.println("You guessed too high."); } System.out.print("Guess again: "); userGuess = kbd.nextInt(); numGuess++; } // now fixed!! [COLOR="Red"]if (userGuess == hiddenNumber) { System.out.println("You guessed correctly!!"); System.out.println("It took you " + numGuess + " guess(es)."); } else { System.out.println("Sorry, you took too many guesses."); }[/COLOR] System.out.println("End of processing."); } }Last edited by batista11b5; 10-23-2010 at 05:31 AM.
-
please change your quote tags to code tags so your code doesn't lose its formatting. The FAQ will show you how (or click on the link in my signature below).
Then you'll want to tell us more about your problem as you really don't tell us enough for us to be able to help you easily. For one thing, what errors are you getting?Last edited by Fubarable; 10-23-2010 at 05:30 AM.
- 10-23-2010, 05:32 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 23
- Rep Power
- 0
sorry , I just fix it to (code tag) ,please help me ,Thanks
- 10-23-2010, 05:44 AM #4
First what if the user enters the number '-1' from the start? It skips the while loop. Also, what if the user enters something that isn't a number? Does the teacher care about error checking at all? Also, your code segment is not inside of the while loop so even if the user enters 0, we'll never know.
Sincerely, Joshua Green
Please REP if I help :)
- 10-23-2010, 05:19 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 23
- Rep Power
- 0
I know how to fix it by another way
Java Code:import javax.swing.*; public class positiveNum2{ public static void main (String[]args){ String userInput; int positiveN; userInput = JOptionPane.showInputDialog ("Enter a number here"); positiveN = Integer.parseInt (userInput); while (positiveN != 0) { if (positiveN <0){ JOptionPane.showMessageDialog (null,"this is negative number"); }else if (positiveN >0){ JOptionPane.showMessageDialog (null,"this is positive number"); } userInput = JOptionPane.showInputDialog ("Enter a positive number here"); positiveN = Integer.parseInt (userInput); } if (positiveN == 0){ JOptionPane.showMessageDialog (null,"this is a void number"); JOptionPane.showMessageDialog (null,"End of processing"); } } }
Similar Threads
-
Please explain how this bit of code works.
By Allspark in forum New To JavaReplies: 4Last Post: 09-03-2010, 03:56 AM -
Please explain these 2 lines of code to me..
By murphaph in forum New To JavaReplies: 10Last Post: 01-19-2010, 02:11 PM -
how does this code work...explain me the execution please...
By vital parsley in forum New To JavaReplies: 3Last Post: 07-25-2008, 04:50 AM -
Iam new in Java Please explain to me
By vinaytvijayan in forum AWT / SwingReplies: 1Last Post: 12-30-2007, 11:35 AM -
need to explain this code
By reached in forum New To JavaReplies: 3Last Post: 12-03-2007, 10:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks