This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kaggs
Kaggs wrote ...

2020/9/3

how to check for a value in array

Kaggs Kaggs

2020/9/3

#
1
2
3
4
5
6
7
8
9
10
11
12
public void randomArray()
    {
       int RandomList = Arrays.asList(RandomNumArray);
       for(int i = 0; i < questionAmount; i ++)
       {
            int num = RandomNum(questionAmount);
            System.out.println(num);
            int result = RandomList.contains(quesitonAmount);
             
       }
        
    }
what i what to do it store random number in to a array but before that i want to check if it already exist in the array beacuse i only what unique number in it. questionAmount is a value that the user input. like if they want to do 5 question. it would generaate 5 random unique number. then this array would be out in a nother array called questionArray where all the question a store and use the value that is stored in randomarray to call the question
danpost danpost

2020/9/3

#
What does RandomNumArray contain? Show all codes used to create the array (to what it is when this randomArray method is called).
Kaggs Kaggs

2020/9/3

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
int questionAmount;
    final int ARRAY_SIZE = 10;
    int[] randomNumArray = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};    
    String[] questionArray = {"Accused money launderers tried to move $1 million in cooler bags"}
 /**
     * Act - do whatever the Game wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.      
        menu();
       
         
    }   
    public void menu()
    {
        Object[] options = {"News Quiz Game", "Fake Count Game"};
       int i = JOptionPane.showOptionDialog(null, "What game mode would you like to play","Quiz Game",
       JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
        
       if (i == 1)
       {
         System.out.println("you have picked Fake games") ;
         FakeGames();
       }
       if (i == 0)
       {
            
           JOptionPane.showMessageDialog(null, "You have picked News Quiz Game");
           NewsQuiz();
       }
    }
    public void FakeGames()
    {
        QuestionAmount();
        randomArray();
         
        
    }
    public void NewsQuiz()
    {
        QuestionAmount();
    }
    public void QuestionAmount()
    {
      String str;
        
         while (true)
      {
       str = JOptionPane.showInputDialog("input the amount of question you went to try between 2-5");
       questionAmount = Integer.parseInt(str);
       if (questionAmount <= 10 && questionAmount >=3)
       {
           break;
            
       }
        
       }
         
    }
    public int RandomNum(int num)
    {
        int RandNum = Greenfoot.getRandomNumber(num);
        //System.out.println(RandNum);
        return RandNum;
    }
    public void randomArray()
    {
        int i, num;
       for( i= 0; i < questionAmount; i ++)
       {
            
           num = RandomNum(questionAmount);
           //System.out.println(num); 
           boolean duplicate = Arrays.asList(randomNumArray).contains(num);
                  
           if(duplicate == false)
           {
               System.out.println("no");
               randomNumArray[i] = num ;
           }
                 
            
         
           
            
             
       }
       for(i = 0; i < questionAmount; i ++)
         System.out.println(randomNumArray[i]);
    }
    
}
the reason the this many methods is beacuse my teacher wants the assessment to be modular. so the randomArray methods is where i need help with. im trying to store uniqe value in to the randomArray
danpost danpost

2020/9/3

#
Kaggs wrote...
im trying to store uniqe value in to the randomArray
Then place unique values in it:
1
for (int i=0; i<ARRAY_SIZE; i++) randomNumArray[i] = i;
Now, you only need to shuffle the values (by performing many swaps, via a loop, or using the java.util.Collections' shuffle method). Line 3 can simply be:
1
int[] randonNumArray = new int[ARRAY_SIZE];
Kaggs Kaggs

2020/9/4

#
1
2
3
4
5
6
7
8
9
public void shuffleArray()
    {
        
       List <Integer> intList = Arrays.asList(randomNumArray);
       Collections.shuffle(intList);
       intList.toArray(randomNumArray);
       for(int i = 0; i < questionAmount; i ++)
         System.out.println(randomNumArray[i]);
    }
it worked just did a shuffle methods and now i get random number from 0-9
You need to login to post a reply.