-3

I know how to create the array as i did below. But how would i check if the user inputed the same number. I tried using if statements but i kept getting errors. I also tried using a do while. I have to write a program that generates and stores 20 random numbers in the array Data[] and asks the user to enter their guess which i did. If their number appears in the array, they get 2 points for each occurrence, the array is printed and all positions where the lucky number can be found. If it doesn't appear in the array, I have to print the lowest and highest values of the array and allow only one more try. if the player gets it right the second time they get 1 point for each appearance.

int lucky;
int Data[]= new int[20];

for(int i=0;i<Data.length;i++){
    Data[i]=(int)(Math.random()*100);
}
for(int i=0;i<Data.length;++i){
    System.out.println(Data[i]+"\t");

}

String input1=JOptionPane.showInputDialog("Enter your lucky number");
lucky=Integer.parseInt(input1);
System.out.println(lucky);
001
  • 13,291
  • 5
  • 35
  • 66
J.Doe
  • 1
  • 6
  • Side note: If you show them the highest and lowest number, can't they just *"guess"* that for an automatic 1 point? – Tim Lewis Nov 09 '15 at 15:41
  • Also this is tagged Javascript but _is_ Java. I don't know Java that well, but it should have some sort of `indexOf` method right? – somethinghere Nov 09 '15 at 15:42
  • i think you want help for `But how would i check if the user inputed the same number` part? – alioguzhan Nov 09 '15 at 15:42
  • To receive help with your homework, include the code you tried that didn't work. The code you posted doesn't even attempt to search the array to find a match. – dsh Nov 09 '15 at 15:47
  • This post shows `indexOf` http://stackoverflow.com/questions/6171663/how-to-find-index-of-int-array-in-java-from-a-given-value @HasseBjork The tags were already changed by now. – somethinghere Nov 09 '15 at 15:50

2 Answers2

0

Something like this should work as you expect to see if the value exists in the array, it can be expanded to search and find for high/low values:

  int counter = 0; //Assume not found at first
    for (int i: Data) {
          if (i==Integer.parseInt(input1)){
                counter++;
          }
    }
    System.out.println("The numer was found "+Integer.toString(counter)+" time(s).");
JBiss
  • 131
  • 10
0
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int[] data = new int[20];
        int guess;
        int times;
        int score = 0;
        int tries = 0;

        fillArrayWithRandoms(data);

        while (tries < 2) {
            System.out.print("Guess a number: ");
            guess = sc.nextInt();

            if ((times = timesInArray(guess, data)) != 0) {
                if (tries == 0)
                    score += 2 * times;
                else
                    score += times;

                printArray(data);
                fillArrayWithRandoms(data);

                System.out.println("You are lucky! Score: " + score + "\n");
            } else {
                tries++;

                System.out.println("You are unlucky!");

                if (tries < 2)
                    printBoundaries(data);

                System.out.println();
            }
        }

        System.out.println("Game Over! Your score: " + score);
    }

    private static void printArray(int[] data) {
        for (int element : data) {
            System.out.print(element + " ");
        }

        System.out.println();
    }

    private static void fillArrayWithRandoms(int[] data) {
        for (int i = 0; i < data.length; i++) {
            data[i] = (int) (Math.random() * 100);
        }
    }

    private static int timesInArray(int guess, int[] data) {
        int occurrence = 0;

        for (int element : data) {
            if (element == guess)
                occurrence++;
        }

        return occurrence;
    }

    private static void printBoundaries(int[] data) {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;

        for (int element : data) {
            if (element < min) {
                min = element;
            }

            if (element > max) {
                max = element;
            }
        }

        System.out.println("Try a number between " + min + " and " + max);
    }
}
  • I would greatly appreciate an "accepted answer" flag to this answer if it helped you. Otherwise give me any feedback. Thank you. – Benjamin Lücking Nov 09 '15 at 20:05
  • I want it to output something like this: if the array is Data[]={3,1,8,31,25,6,3,20,41,12,3,23,7,3,65,49,5,0,13,17} and the user inputs 3. I want it to output: 3 can be found on the following positions: 0 6 10 13. 3 appears 4 times. You get 8 points And if they input 2 Then output: Lucky number 2 is not in the array! Lowest value:1 Highest value:65 and if they input it right the second time: i.e. 65 Then the output would be: You get 1 point! – J.Doe Nov 11 '15 at 15:44