samedi 7 août 2021

Only supposed to pull 100 random values but is pulling more than that. How do I get only 100 random numbers pulled between the values of 0-100?

The results print the grade and how many times that grade was pulled but it doesn't stop at 100. I am trying to get the program to generate 100 random grades between 0 and 100 and save them in an array. Then the program is supposed to print the grade letter and how many times the program generated that grade. Bellow is the code I created. Still in the early stages of learning Java.

import java.util.Random;
public class array{
    public static void main(String[] args){
        Random rd=new Random();             //creating random object
        int[] randomValue=new int[100];     //declaring array
        int count=0;
        for(int i=0; i<randomValue.length; i++){
            randomValue[i]=rd.nextInt(100);     //storing random numbers
            if(randomValue[i]>94){
                count++;
                System.out.println("A" + count);
            }
            else if(randomValue[i]>92&&randomValue[i]<95){
                count++;
                System.out.println("A-" + count);
            }
            else if(randomValue[i]>90&&randomValue[i]<93){
                count++;
                System.out.println("B+" + count);
            }
            else if(randomValue[i]>86&&randomValue[i]<91){
                count++;
                System.out.println("B" + count);
            }
            else if(randomValue[i]>84&&randomValue[i]<87){
                count++;
                System.out.println("B-" + count);
            }
            else if(randomValue[i]>82&&randomValue[i]<85){
                count++;
                System.out.println("C+" + count);
            }
            else if(randomValue[i]>78&&randomValue[i]<83){
                count++;
                System.out.println("C" + count);
            }
            else if(randomValue[i]>76&&randomValue[i]<79){
                count++;
                System.out.println("C-" + count);
            }
            else if(randomValue[i]>74&&randomValue[i]<77){
                count++;
                System.out.println("D+" + count);
            }
            else if(randomValue[i]>69&&randomValue[i]<75){
                count++;
                System.out.println("D" + count);
            }
            else{
                count++;
                System.out.println("F" + count);
            }
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire