Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 11, 2012, 04:36 PM   #1
Hylekmi
macrumors regular
 
Join Date: Dec 2010
Java Help

Hey guys, I am writing a program that fills an array of 100 with values from 1 to six( like a dice roll game) Then i have to count the number of times the integer appears. THEN i have to output an asterisk for the amount of time each number occurs. I have everything done up until the asterisk part. I can't figure it out to save my life. heres my code.

Code:
public class finalJavaProject
{
	public static void main (String[]args)
	{	
		
		int numOne=0;
		int numTwo=0;
		int numThree=0;
		int numFour=0;
		int numFive=0;
		int numSix=0;
		char asterik ='*';
		
		
		
		int[] indRoll = new int[100];
		
		 for(int index=0;index<100;index++)
		 {
			 
			 
			 
			 indRoll[index]=(int) (Math.random()*6)+1;
			
			 if(indRoll[index]==1)
			 {
				 numOne++;
				 
				 System.out.print(asterik);
				
				 
			 }
			
			 if(indRoll[index]==2)
			 {
				 numTwo++;
				
				;
			 }
			 if(indRoll[index]==3)
			 {
				 numThree++;
			 }
			 if(indRoll[index]==4)
			 {
				 numFour++;
				 
			 }
			 if(indRoll[index]==5)
			 {
				 numFive++;
				 
			 }
			 if(indRoll[index]==6)
			 {
				 numSix++;
				 
			 }
			
			
		 }
		 System.out.println();
		 System.out.println("Number of 1's: "+numOne);
		 System.out.println("Number of 2's: "+numTwo);
		 System.out.println("Number of 3's: "+numThree);
		 System.out.println("Number of 4's: "+numFour);
		 System.out.println("Number of 5's: "+numFive);
		 System.out.println("Number of 6's: "+numSix);
		 System.out.println();
		 System.out.println();
		 System.out.println();
		 System.out.println();
		 System.out.println();
		 
	}//end main
	

}//end public

I can out put the correct number of asterisks for the first digit. But i need it to be seperate from the rest of it. The point of the asterisks is to make it look like a graph.

This assignment basically determines if i pass the class or not so any help would be greatly appreciated. Thanks in advance!
Hylekmi is offline   0 Reply With Quote
Old Mar 11, 2012, 04:40 PM   #2
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: USA
Just make a loop for each number 1-6, that runs the amount of times that number was rolled. And each time you go through the loop, print one asterisk (but don't print a 'return' or end of line.

Also, you might want to use a switch statement instead of a bunch of if statements.
robvas is offline   0 Reply With Quote
Old Mar 11, 2012, 04:42 PM   #3
Hylekmi
Thread Starter
macrumors regular
 
Join Date: Dec 2010
Quote:
Originally Posted by robvas View Post
Just make a loop for each number 1-6, that runs the amount of times that number was rolled. And each time you go through the loop, print one asterisk (but don't print a 'return' or end of line.

Also, you might want to use a switch statement instead of a bunch of if statements.
Thats a good idea. Thanks, I haven't learned what a switch statement is yet so i can't use it. Thanks though!
Hylekmi is offline   0 Reply With Quote
Old Mar 11, 2012, 05:32 PM   #4
elppa
macrumors 68040
 
elppa's Avatar
 
Join Date: Nov 2003
Code:
public class FinalJavaProject
{
	public static void main (String[] args)
	{	
		int numOne = 0;
		int numTwo = 0;
		int numThree = 0;
		int numFour = 0;
		int numFive = 0;
		int numSix = 0;
		char asterik = '*';

		int[] indRoll = new int[100];

		for(int index = 0; index < 100; index++)
		{
			indRoll[index] = (int)(Math.random() * 6) + 1;
			switch(indRoll[index])
			{
				case 1:
					numOne++;
					break;
				case 2:
					numTwo++;
					break;
				case 3:
					numThree++;
					break;
				case 4:
					numFour++;
					break;
				case 5:
					numFive++;
					break;
				case 6:
					numSix++;
					break;
			}
		}
		
		printOccurrences(1, numOne, asterik);
		printOccurrences(2, numTwo, asterik);
		printOccurrences(3, numThree, asterik);
		printOccurrences(4, numFour, asterik);
		printOccurrences(5, numFive, asterik);
		printOccurrences(6, numSix, asterik);
	}//end main
	
	private static void printOccurrences(int number, int occurrences, char symbol)
	{
		System.out.print("Number of " + number + "'s: ");
		for(int i = 0; i < occurrences; i++)
		{
			System.out.print(symbol);
		}
		System.out.println(" (" + occurrences + ")");
	}
}//end public
elppa is offline   0 Reply With Quote
Old Mar 11, 2012, 05:39 PM   #5
Hylekmi
Thread Starter
macrumors regular
 
Join Date: Dec 2010
Ok, I can't use that because we haven't learned that yet. Im still stuck on making the loop though. I can't get it to output the correct results.
Code:
public class finalJavaProject
{
	public static void main (String[]args)
	{	
		
		int numOne=0;
		int numTwo=0;
		int numThree=0;
		int numFour=0;
		int numFive=0;
		int numSix=0;
		
		int count=0;
		
		
		
		
		int[] indRoll = new int[100];
		
		 for(int index=0;index<100;index++)
		 {
			 
			 
			 
			 indRoll[index]=(int) (Math.random()*6)+1;
			
			 if(indRoll[index]==1)
			 {
				 numOne++;	
				 
			 }
			 if(indRoll[index]==2)
			 {
				 numTwo++;
			 }
			 if(indRoll[index]==3)
			 {
				 numThree++;
			 }
			 if(indRoll[index]==4)
			 {
				 numFour++;
				 
			 }
			 if(indRoll[index]==5)
			 {
				 numFive++;
				 
			 }
			 if(indRoll[index]==6)
			 {
				 numSix++;
			 }
			for(int i=0;i==numOne;i++)
			{
				count++;
				System.out.println(count);
			}
		 }
		
		 System.out.println();
		 System.out.println("Number of 1's: "+numOne);
		 System.out.println("Number of 2's: "+numTwo);
		 System.out.println("Number of 3's: "+numThree);
		 System.out.println("Number of 4's: "+numFour);
		 System.out.println("Number of 5's: "+numFive);
		 System.out.println("Number of 6's: "+numSix);
		 System.out.println();
		 System.out.println();
		 System.out.println();
		 System.out.println();
		 System.out.println();
		 
		
				
				
	}//end main
	

}//end public


----------

Got it!
Hylekmi is offline   -1 Reply With Quote
Old Mar 11, 2012, 05:45 PM   #6
elppa
macrumors 68040
 
elppa's Avatar
 
Join Date: Nov 2003
I wrote the loop for you if the previous post...

I put the switch statement in for demo purposes.

You don't need an if/switch statement inside the first loop if you use another array store the totals.

Code:
public class FinalJavaProject
{
	public static void main (String[] args)
	{
		char asterik = '*';
		int rolls = 100;
		int maxDieNumber = 6;
		int[] indRoll = new int[rolls];
		int[] occurrences = new int[maxDieNumber];

		for(int index = 0; index < rolls; index++)
		{
			indRoll[index] = (int)(Math.random() * maxDieNumber) + 1;
			occurrences[indRoll[index] - 1]++;
		}
		
		printOccurrences(occurrences, asterik);
	}//end main
	
	private static void printOccurrences(int[] occurrences, char symbol)
	{
		for(int i = 0; i < occurrences.length; i++)
		{
			System.out.print("Number of " + (i + 1) + "'s: ");
			printSymbol(symbol, occurrences[i]);
			System.out.println(" (" + occurrences[i] + ")");
		}
	}
	
	private static void printSymbol(char symbol, int repeat)
	{
		for(int i = 0; i < repeat; i++)
		{
			System.out.print(symbol);
		}
	}
}//end public

Last edited by elppa; Mar 11, 2012 at 05:56 PM. Reason: Added maxDieNum variable
elppa is offline   0 Reply With Quote
Old Mar 11, 2012, 05:46 PM   #7
chown33
macrumors 603
 
Join Date: Aug 2009
Example:
Code:
for ( int i = 0; i < someNumber; i++ )
{  System.out.print( "*" );  }
System.out.println();
This is basically what elppa posted, only he put it in a separate method. If you don't understand what it does, step through it by hand. Imagine someNumber has the value 5. What output is produced? Now imagine someNumber has the value 11. What's the output? The way I'd describe this loop is it outputs one asterisk for each incremental count of i.

If you know how arrays work, why are you keeping six separate variables? Seems like a perfect opportunity to have an array of six counters, and you increment the one whose number appears in the array of 100.
chown33 is offline   2 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
HELP! I need Java! But don't have an earlier version installed! bs2511feist OS X 10 Apr 14, 2011 07:24 PM
Java Help ///mdriver Mac Applications and Mac App Store 6 Oct 10, 2004 06:04 PM
Need Java Help... Wes Mac Applications and Mac App Store 0 Jun 13, 2004 11:23 AM
Java Help Wildcat Mac Help/Tips 2 Mar 11, 2003 01:33 PM
java help! mastabri Community 4 Nov 9, 2002 10:58 PM


All times are GMT -5. The time now is 11:10 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC