vendredi 31 mai 2019

Math.Random always setting random number to 0

Creating a program where a random number is generated using math.random which i have put into a function and called this function in the program to make things tidier but the random number that gets outputted is always what the integer secret_number is set to, and i am not sure how to fix this to create a random number. I would like to keep the math.random in the function and use parameters

package guessinggame3;
import java.util.Scanner; //imports scanner to read from keyboard
import java.util.ArrayList;


public class GuessingGame3 { //start of public class

static Scanner kboard = new Scanner(System.in); //calls scanner 


public static void main(String args[]) //start of main 
{

System.out.println("Welcome to the guessing game, the computer will 
generate a random number that you have to guess, good luck!"); //opening 
message explaining how to play 

int secret_number = 0;
int number_of_guesses = 0;
int user_guess ;
ArrayList<Integer> entered_numbers = new ArrayList<Integer>();


generate_random_number(secret_number); //calls he function 
generate_random_number to say the random number has been generated 




for(int i=0; i<20;i++) { // start of for loop sets user attempts to 20


System.out.println("Please make your guess"); //asks user to enter make 
their guess             
user_guess = kboard.nextInt();
number_of_guesses++; // adds on to the counter after each user guess


if(entered_numbers.contains(user_guess)) //checks if entered number = 
number stored in array
{
System.out.println("You have already entered this number"); //displays 
error message if user enters same number again
continue;
}

else 
{
entered_numbers.add(user_guess);
if (user_guess == secret_number)
System.out.println("Your guess is corret you win!");
}

if (user_guess < secret_number)
{
System.out.println("Guess is too low");
}

if (user_guess > secret_number) 
{
System.out.println("Your guess is too high");
}


System.out.println (20 - number_of_guesses + " Guesses remaining"); 
//Tells the user how many guesses they have remaining 


} //end of for loop

} //end of main

static void generate_random_number(int secret_number)  //start of function 
generate_random_number function passing secret_number as a parameter
{       
secret_number = (int)(Math.random()*100) + 1; //generates random number 
between 0 and 100
System.out.println("The computer has generated it's number"); //lets the 
user know that the random number has been generated
} //end of function generate_random_number

} //end of public class




Aucun commentaire:

Enregistrer un commentaire