PDA

View Full Version : Java Help




Hylekmi
Mar 11, 2012, 04:36 PM
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.


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!



robvas
Mar 11, 2012, 04:40 PM
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.

Hylekmi
Mar 11, 2012, 04:42 PM
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!

elppa
Mar 11, 2012, 05:32 PM
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

Hylekmi
Mar 11, 2012, 05:39 PM
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. 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!

elppa
Mar 11, 2012, 05:45 PM
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.


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

chown33
Mar 11, 2012, 05:46 PM
Example:
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.