Results 1 to 14 of 14
Thread: New to Programming!!HW Help!
- 10-15-2010, 11:18 PM #1
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
New to Programming!!HW Help!
HEEEY Guy . Im having trouble exactly getting my code to do what the problem says here it is..[
Loops. Using the Die class defined in Chapter 4, design and implement a driver class called CountFaces whose main method rolls a die 100 times and counts how many times each face comes up. Using (a) while() loop, (b) do .. while() and (c) for loop, print the results. Use switch, not if statement.
public class Crapz {
public static void main(String[] args) {
die die1 = new die();
int x;
x = 0;
die1.getFaceValue();
die1.roll();
while (x <=100) {
System.out.println(die1.roll());
x ++;
}
do {
System.out.println(die1.roll());
x++;
} while(x <= 100);
for ( x=1 ;x<=100;x++) {
System.out.println(die1.roll());
x ++;
}
}
}
ps i have no clue how to actually collect the numbers to store them...
- 10-15-2010, 11:27 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
-
Your do/while loop won't work unless you reset x to 0. One way to store the results is to create an array of 7 ints before a loop, (I'd just ignore the 0th item in the array), get the int returned from the roll and increase the number in the array corresponding to that roll by one. For instance, if roll returns 4, then increase the 4th item in the array by
myArray[4]++;
or
myArray[4] = myArray[4] + 1;
Only you won't hard-code this as "4" but instead will use an int variable to hold the result of the roll and use that number as your array index. So in pseudocode:
Java Code:create an array of 7 ints called myArray for x goes from 0 to less than 100 get roll and store in rollResult variable. Increase the value held by myArray[rollResult] by one end for loop
- 10-15-2010, 11:48 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Mhm, i think that would be a good solution, but that is not what the assignment is asking for (i think the goal is, to bring the switch statement closer to him)
my pseudocode:
Java Code:create six int variables for x goes from 0 to less than 100 get roll and store in rollResult variable. //maybe in a method: switch with rollResult - increase the the right variable in the right case end for loop
-
- 10-16-2010, 12:34 AM #6
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Hi guys thanks for the advices .. I AM STILL STRUGGLING TO COMPILE THIS.I CAN GET IT TO ROLL THE DICE 100TIMES AND PRINT.. CANT THINK OF WHAT TO DO. I DONT WANT ANSWERS JUST GUIDANCE!!!:( WILL NOT QUIT) !!!!!!!!
- 10-16-2010, 12:55 AM #7
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
In replies to you Fubarable
is this right so far about rolling to die and storing the result in a variable?
public class ugh {
public static void main(String[]args) {
int a,s,d,f,g,h,FaceValue;
a = 0;
s=0;
die die1 = new die();
while (a<=5) {
die1.getFaceValue();
FaceValue = s;
}
}
}
- 10-16-2010, 02:04 AM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
HEEEY Guy . Im having trouble exactly getting my code to do what the problem says here it is..[
Loops. Using the Die class defined in Chapter 4, design and implement a driver class called CountFaces whose main method rolls a die 100 times and counts how many times each face comes up. Using (a) while() loop, (b) do .. while() and (c) for loop, print the results. Use switch, not if statement.
not sure about my switch statement as I never use them. you could do this as do while but I don't see the point if x is set to 0 to begin with anyway.Java Code:public class CountFaces { public static void main(String[] args) { die die1 = new die(); // die has six faces i.e 1,2,3,4,5,6 int face1=0; // occurence of 1 rolled int face2=0; // occurence of 2 rolled int face3=0; // occurence of 3 rolled int face4=0; // occurence of 4 rolled int face5=0; // occurence of 5 rolled int face6=0; // occurence of 6 rolled int x=0; while (x <=100) { // assuming die1.roll() returns an int value of 1,2,3,4,5 or 6 int rolledNumber=die1.roll(); switch(rolledNumber){ case 1: face1++; break; case 2: face2++; break; case 3: face3++; break; case 4: face4++; break; case 5: face5++; break; case 6: face6++; break; default: break; } // end of switch x++; } } // not sure why you would need a for loop System.out.println("1 occurred "+face1+" times"); System.out.println("2 occurred "+face2+" times"); System.out.println("3 occurred "+face3+" times"); System.out.println("4 occurred "+face4+" times"); System.out.println("5 occurred "+face5+" times"); System.out.println("6 occurred "+face6+" times"); } }
i.e replace while loop with
Java Code:int x=0; do{ // sumplace inside the do while x++; }while(x<=100);
- 10-16-2010, 02:27 AM #9
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
wow thats an astronomical chance that your in Ireland and have the same exact problem as me. First off you code is way more draw out than mines. The bad thing is that im in the same spot i was 5 hours ago. I've just started programming and trying to improve but class is getting quite intense...
- 10-16-2010, 02:31 AM #10
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Erm I am in Ireland (not with your problem) but trying to help you solve it :D .. probably did ;)
- 10-16-2010, 02:34 AM #11
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Wowwww thankss you definitely did!i need advice as to get better fast i have the time..yourr good!:):):):):):)
- 10-16-2010, 02:38 AM #12
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Hmm advice to get better? code every oppurtunity, read good books and apply the knowledge, LEARN HOW TO DEBUG, be a tad bit obsessive about getting code to work, have fun.
I would start by understanding the logic of this program's solution.
- 10-16-2010, 02:42 AM #13
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Thanks again!:)
- 10-16-2010, 02:43 AM #14
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
new to programming need help with something
By surg3y3 in forum New To JavaReplies: 4Last Post: 01-26-2010, 03:13 AM -
programming website?
By ragarwal in forum Forum LobbyReplies: 11Last Post: 12-21-2009, 12:37 PM -
GUI Programming Help
By sirwiggles in forum New To JavaReplies: 4Last Post: 04-28-2009, 04:53 AM -
New to Programming . . .Need Help
By DSutta22 in forum New To JavaReplies: 2Last Post: 09-10-2008, 05:19 AM -
programming
By abcdefg in forum New To JavaReplies: 9Last Post: 03-10-2008, 10:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks