Java Help

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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!" );


}
}
}

}
 
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]
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top