Results 1 to 6 of 6
- 12-01-2010, 07:13 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
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 }
- 12-01-2010, 07:18 PM #2
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.
- 12-01-2010, 07:24 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
thank you. but it returns 0 all the time. how do i make it return the random number i generated from the generateRandomNumber method?
- 12-01-2010, 07:26 PM #4
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.
- 12-03-2010, 03:40 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
- 12-03-2010, 08:44 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Method Doesnt turn negative number 0 and doesnt return 3
By anonb in forum New To JavaReplies: 7Last Post: 09-28-2010, 12:17 AM -
Simple program compiles but main class is not found-- not sure why
By damong in forum New To JavaReplies: 4Last Post: 01-01-2009, 03:58 AM -
Compiles in Netbeans, error in textpad
By Npcomplete in forum New To JavaReplies: 2Last Post: 11-21-2008, 04:50 AM -
Program Compiles but Buttons do not display
By ljk8950 in forum AWT / SwingReplies: 8Last Post: 08-11-2008, 03:41 AM -
My program doesnt display anything
By Bojevnik in forum AWT / SwingReplies: 2Last Post: 10-19-2007, 02:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks