简体   繁体   中英

Finding positions of a number in an array and giving points, Java

I'm trying to write a program that generates and stores 20 random numbers in the array Data[] and asks the user to enter their guess. If their number appears in the array, they get 10 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. For example: Data[]={3,1,8,31,25,6,3,20,41,12,3,23,7,3,65,49,5,0,13,17} and if the user inputs 3 then the program would output

3 can be found on the following positions:
0 6 10 13
3 appears 4 times. You get 40 points!

and for the same Data[]={3,1,8,31,25,6,3,20,41,12,3,23,7,3,65,49,5,0,13,17} if the user inputs 2 then the program would output

Sorry your lucky number 2 is not in the array! 
Hint: Lowest Value:1 Highest Value 65  Try again with a different number in this range.

if they input 65 on their second try then the program would output

You get 1 point(s)!

This is my code I have tried but it is not working. It keeps telling me my numbers are not in the array. The lowest and highest values work. I do not know how to fix it. I have to have this done by tomorrow. Any help would be greatly appreciated.

String input1 = JOptionPane.showInputDialog("Please enter your lucky number between 0 and 100.");
luck=Integer.parseInt(input1);
System.out.println("Input " +luck);

int Data[] = new int [20];
for( int i=0; i<Data.length;++i){
    Data[i] = (int) (Math.random() * 100);
    System.out.print(Data[i]+ " \t");
}
for( int i=0; i<Data.length;++i){
    if(Data[i]==luck){
        System.out.println(luck+ " can be found in the following positions: ");
        System.out.println(i);
        score=score+10;
        System.out.println(luck+ " appears " +(luck/10)+ " times. You get " +score+ " points.");
    }else if(Data[i]!=luck){
        Arrays.sort(Data);
        System.out.println();
        System.out.println(luck+ " is not in the array.");
        System.out.println("Hint: " +Data[0]+ " is the lowest value and " +Data[19]+ " is the highest.");
        input1 = JOptionPane.showInputDialog("Please enter another number between 0 and 100.");
        luck=Integer.parseInt(input1);
    }
}
System.out.println();
System.out.println("Score: " +score);

}}

The problem is you are not checking your value against every value in your array. If the entered value does not match the first value in your Array (i=0), then you are asking the user for another value.

What you have to do is ask for a value, compare it against EVERY value in the table, then make your decision. Either it's present or its not.

I added a boolean. We set it to true if we find our number, otherwise we display the message and ask the user to try again.

Try this:

    boolean found = false;
    for( int i=0; i<Data.length;++i){
        if(Data[i]==luck){
            System.out.println(luck+ " can be found in the following positions: ");
            System.out.println(i);
            score=score+10;
            System.out.println(luck+ " appears " +(luck/10)+ " times. You get " +score+ " points.");
            found = true;
        }
    }
    if(!found){
            Arrays.sort(Data);
            System.out.println();
            System.out.println(luck+ " is not in the array.");
            System.out.println("Hint: " +Data[0]+ " is the lowest value and " +Data[19]+ " is the highest.");
            input1 = JOptionPane.showInputDialog("Please enter another number between 0 and 100.");
            luck=Integer.parseInt(input1);
        }

Finally, you need to implement a counter inside your loop, and then display your information as required... Right now, it will display "# can be found in the following positions" and other messages for each instance found in the array. Implement a counter, and a way to track indexes of values found, and then you can display all that information coherently from outside the for loop

Edit: To give you more complete implementation... This code should basically take you almost where you need to go:

public static void main(String[] args) {
    int Data[] = new int [20];
    for( int i=0; i<Data.length;++i){
        Data[i] = (int) (Math.random() * 100);
        System.out.print(Data[i]+ " \t");
    }

    while(true){
        String input1 = JOptionPane.showInputDialog("Please enter your lucky number between 0 and 100.");
        int luck=Integer.parseInt(input1);
        int score = 0;
        String positions = "";
        int counter = 0;
        System.out.println("Input " +luck);

        boolean found = false;
        for( int i=0; i<Data.length;++i){
            if(Data[i]==luck){
                positions +=  i + " ";
                counter++;
                score=score+10;
                found = true;
            }
        }
        if(found){
            System.out.println(luck+ " can be found in the following positions: ");
            System.out.println(positions);
            System.out.println(luck+ " appears " + counter + " times. You get " +score+ " points.");

        }
        else{
            Arrays.sort(Data);
            System.out.println();
            System.out.println(luck+ " is not in the array.");
            System.out.println("Hint: " +Data[0]+ " is the lowest value and " +Data[19]+ " is the highest.");
        }

        System.out.println();
        System.out.println("Score: " +score);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM