1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Java Help

Discussion in 'Software Development' started by Rhino24, Mar 31, 2008.

Thread Status:
Not open for further replies.
  1. Rhino24

    Rhino24 Thread Starter

    Joined:
    Feb 8, 2008
    Messages:
    41
    I wrote this code but all the encouragements are printing all together and the Good jobs are also doing the same thing. I need them to print individually. Can someone help me fix this problem.

    import java.util.Random;
    import java.util.Scanner;

    public class MultiplyTest {
    static Random seed = new Random();
    static Scanner scanner;

    public static void main(String[] args) {
    scanner = new Scanner(System.in);
    multiply();
    scanner.close();
    }

    private static void multiply() {
    int response = 0;
    int message = response % 4;


    while(response != -1) {
    int x = 1 + seed.nextInt(10); // [ 1 <= x <= 10]
    int y = 1 + seed.nextInt(10);
    System.out.print("How much is " + x +
    " times " + y + " ? : ");
    try {
    response = Integer.parseInt(scanner.nextLine());
    } catch(NumberFormatException e) {
    System.out.println("number format: " + e.getMessage());
    }

    while(response != -1 && response != x*y) {
    if( message == 0
    System.out.println( "No. Please try again:" );
    else
    if( message == 1 )
    System.out.println( "Wrong. Try once more:" );
    else
    if (message == 2 )
    System.out.println( "Don't give up!" );
    else
    System.out.println( "NO. Keep trying:" );

    { response = Integer.parseInt(scanner.nextLine());
    } catch(NumberFormatException e) {
    System.out.println("number format: " + e.getMessage());
    }
    }


    {
    if( message == 0 )
    System.out.println( "Very goood!" );
    else
    if( message == 1 )
    System.out.println( "Excellent" );
    else
    if (message == 2 )
    System.out.println( "Nice Work!" );
    else
    System.out.println( "Keep up the good work!" );


    }
    }
    }

    }
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi Rhino24,

    If I remembered well, you had to display a random message.
    - In your coding, the variable message is never assigned a value except for the statement int message = response % 4;.
    I guess you meant : int message = seed.nextInt(4);.
    - In your try ... catch... statements, no default value is assigned to the variable response when an exception is caught.
    - In order to prevent a tedious serie of if ... else ... if ... when you test the content of a integer, you may the switch statement :
    Code:
    [SIZE=2]
    switch(message) {
        case 0:
            [I]some statements[/I]
            break;
        case 1:
            [I]some statements[/I]
            break;
        default:
            [I]some statements[/I]
    }
    [/SIZE]
    - Also, it is not forbidden to use arrays to store the different messages.

    Here's my coding :
    Code:
    [SIZE=2]
    import java.util.Random;
    import java.util.Scanner;
    
    public class MultiplyTest {
    
        static Random seed = new Random();
        static Scanner scanner;
        static String[] rightMessage = {"Very goood!", "Excellent", "Nice Work!",
            "Keep up the good work!"
        };
        static String[] wrongMessage = {"No. Please try again : ", "Wrong. Try once more : ",
            "Don't give up! ", "NO. Keep trying : "
        };
    
        public static void main(String[] args) {
            scanner = new Scanner(System.in);
            multiply();
            scanner.close();
        }
    
        private static void multiply() {
            int response = 0;
    
            while (response != -1) {
                int x = 1 + seed.nextInt(10); // [COLOR=BLUE][ 1 <= x <= 10][/COLOR]
                int y = 1 + seed.nextInt(10);
                System.out.print("How much is " + x +
                        " times " + y + " ? : ");
                response = getResponse();
                if (response == -1) break; // [COLOR=BLUE]exit the while loop[/COLOR]
                if (response == (x * y)) displayMessage(true);
                while (response != -1 && response != (x * y)) {
                    displayMessage(false);
                    response = getResponse();
                    if (response == (x * y)) displayMessage(true);
                }
            }
        }
    
        // [COLOR=BLUE]return the user's entered integer value[/COLOR]
        // [COLOR=BLUE]if the entered value is not numeric, the value -1 is returned[/COLOR]
        private static int getResponse() {
            try {
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("number format: " + e.getMessage());
                return -1;                        
            }
        }
    
        // [COLOR=BLUE]show a random message with index varying from 0 to 3[/COLOR]
        // [COLOR=BLUE]in the appropriate array of messages[/COLOR]
        private static void displayMessage(boolean isRightAnswer) {
            int message = seed.nextInt(4);
            if (isRightAnswer) {
                System.out.println(rightMessage[message]);
            } else {
                System.out.println(wrongMessage[message]);
            }
        }
    }
    
    [/SIZE]
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/699105

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice