-1
import java.util.*;

public class Testing {

    public static void main(String[] args) {

        Random rand = new Random();
        int random = (int)(Math.random() * 3 + 1);

        //to see what number to guess
        System.out.println(random);

        int score = 0;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter Number: ");
        String num = input.nextLine();
        int count = 0;

        while (count <= 5) {
            if (random == 1) {
                if (num.equals("one")) {
                    System.out.println("correct");
                    score++;
                }
                else {
                    System.out.print("Wrong!");
                }
            }
            else if (random == 2) {
                if (num.equals("two")) {
                    System.out.println("correct");
                    score++;
                }
                else {

                    System.out.print("Wrong!");
                }
            }
            else {
                if (num.equals("three")) {
                    System.out.println("correct");
                    score++;
                }
                else {
                    System.out.print("Wrong!");
                }
            }
            count++;
        }
        System.out.println(score);
    }
}

How do I make it that will ask 5 different random numbers ?.

After every good guess, the score should be incremented by 1.

After every guess (good or bad), it should proceed to another random number?

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93

2 Answers2

0

To Read 5 numbers from the user, write a code something like below

public Integer [] read5NumbersFromConsole(Scanner input){
    Integer [] inputNumber = new Integer [5];   
  for(int i =0; i< 5; i++){
     System.out.print("Enter Number: ");
        inputNumber[i] = input.nextInt();

   }
return inputNumber;
}

The rest I left it for you to implement...

Yohannes Gebremariam
  • 2,225
  • 3
  • 17
  • 23
0

Strange that you allocate a Random class to not use it.

You should use it, and write rand.nextInt(3 + 1) (exclusive, thus the +1) rather than (int)(Math.random() * 3 + 1).

Your while loop can be simplified by a for loop: for (int count = 0; count < 5; ++count). Beware, initializing your variable to 0 and going to <= 5 will result to 6 iterations, not 5.

You do not need to create an array of 5 numbers in your case, you can simply generate a new random number for each guess, inside your loop.

You could also reduce redundancy in your code with a map like 1 to "one", 2 to "two", etc.

In the end, I would write it like this:

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

public class Testing {

    public static void main(String[] args) {
        int score = 0;
        Random rand = new Random();
        Scanner input = new Scanner(System.in);

        HashMap<Integer, String> intToString = new HashMap<>();

        intToString.put(1, "one");
        intToString.put(2, "two");
        intToString.put(3, "three");

        for (int count = 0; count < 5; ++count) {
            // "nextInt" is exclusive! Doing +1 converts [0;2] to [1;3]
            int random = rand.nextInt(3 + 1);

            // To see what number to guess
            System.out.println(random);

            String num = input.nextLine();

            // Get the string representation
            String response = intToString.get(random);

            if (num.equals(response)) {
                System.out.println("correct");
                ++score;
            } else {
                System.out.println("Wrong!");
            }
        }

        System.out.println(score);
    }
}

If you are using Java 1.7+, you can use ThreadLocalRandom.current() rather than Random. Related post here.

Community
  • 1
  • 1
kagmole
  • 2,005
  • 2
  • 12
  • 27