Results 1 to 5 of 5
  1. #1
    batista11b5 is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    batista11b5 is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    sorry , I just fix it to (code tag) ,please help me ,Thanks

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    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 :)

  5. #5
    batista11b5 is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    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

  1. Please explain how this bit of code works.
    By Allspark in forum New To Java
    Replies: 4
    Last Post: 09-03-2010, 03:56 AM
  2. Please explain these 2 lines of code to me..
    By murphaph in forum New To Java
    Replies: 10
    Last Post: 01-19-2010, 02:11 PM
  3. how does this code work...explain me the execution please...
    By vital parsley in forum New To Java
    Replies: 3
    Last Post: 07-25-2008, 04:50 AM
  4. Iam new in Java Please explain to me
    By vinaytvijayan in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2007, 11:35 AM
  5. need to explain this code
    By reached in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •