Results 1 to 6 of 6
  1. #1
    ErikD99 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Exclamation This program compiles but doesnt run properly!

    This code compiles, but it doesnt generate a random number.. any suggestions?

    Java Code:
    import java.util.Scanner;
    
    public class DanielsonErikA4Q1{
    
    int maximum;
    
    
    	public static void main(String[]args){
    	int numRolls;
    	int maximum;
    	int rif=0;
    
    	Scanner kbd=new Scanner(System.in);
    
    	System.out.println("Enter the number of sides on the dice");
    	maximum=kbd.nextInt();
    
    	System.out.println("Enter the number of rolls");
    	numRolls=kbd.nextInt();
    
    	generateRandomNumber(maximum);
    
    	System.out.println("numsides is "+maximum+
    	"\nnumrolls is "+numRolls);
    
    	System.out.println("random is " +rif);
    
    
    	}// close main
    			public static void initializeArray(int initValue, int[] array){
    
    			}//close initializeArray
    
    			public static int generateRandomNumber(int maximum){
    
    
    				int rif=(int)(Math.random()*10)+1;
    
    
    					if(rif<=maximum){
    
    					return rif;
    
    					}
    
    					else{
    
    					while(rif>maximum){
    
    					maximum=(int)(Math.random()*10)+1;
    
    					}
    
    					return rif;
    					}
    
    			}//close
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    What does it generate instead?

    Your indenting makes me cry.

    Hint: You don't do anything with the value returned from generateRandomNumber(). Just because two variables share the same name in different scope, that doesn't mean setting one magically sets the other. Hope that helps.

  3. #3
    ErikD99 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    thank you. but it returns 0 all the time. how do i make it return the random number i generated from the generateRandomNumber method?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    Work your way through main. When do you change the value of the "rif" variable? Keep in mind that the rif variable in main is completely different from the rif variable in your generateRandomNumber() method.

  5. #5
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by ErikD99 View Post
    Java Code:
    				int rif=(int)(Math.random()*10)+1;
    
    
    					if(rif<=maximum){
    
    					return rif;
    
    					}
    
    					else{
    
    					while(rif>maximum){
    
    					maximum=(int)(Math.random()*10)+1;
    
    					}
    
    					return rif;
    					}
    
    			}//close
    
    }
    quick question cause i a bit new to java but would this portion only generate a d10?

  6. #6
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    try to save rif value in your main class:

    rif = generateRandomNumber(maximum);

Similar Threads

  1. Replies: 7
    Last Post: 09-28-2010, 12:17 AM
  2. Replies: 4
    Last Post: 01-01-2009, 03:58 AM
  3. Compiles in Netbeans, error in textpad
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 04:50 AM
  4. Program Compiles but Buttons do not display
    By ljk8950 in forum AWT / Swing
    Replies: 8
    Last Post: 08-11-2008, 03:41 AM
  5. My program doesnt display anything
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 10-19-2007, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •