Print a for-loop, Java -
i'm trying learn java on own. re-doing programs i've done before, in different way. stuck for-loop. have following code:
public class head { public static void main(string[] args) { int = 0; int b = 0; int c = 0; int d = 0; int e = 0; int f = 0; for(int i=0; i<1000; i++){ int dice1 = 1 + (int)(math.random() * ((6 - 1) + 1)); if (dice1 == 1){ a++; } else if(dice1 == 2){ b++; } else if(dice1 == 3){ c++; } else if(dice1 == 4){ d++; } else if(dice1 == 5){ e++; } else if(dice1 == 6){ f++; } } system.out.println("ones: " + a); system.out.println("twos: " + b); system.out.println("threes: " + c); system.out.println("fours: " + d); system.out.println("fives: " + e); system.out.println("sixes: " + f); } }
this works fine , want to. want create constructor method countings me. far good. when comes printing out, output returns number of times calculation. i.e, if set i<5 prints 1 2 3 4 5 6 5 times, , not 1 time.
here bad code: public class head { public static void main(string[] args) {
int a=0; int b=0; int c=0; int d=0; int e=0; int f=0; for(int i=0; i<5; i++){ int dice1 = 1 + (int)(math.random() * ((6 - 1) + 1)); dice diceobject = new dice(dice1, a, b, c, d, e, f); diceobject.countnumbers(); // system.out.println(diceobject); } } } public class dice { private int a=0, b=0, c=0, d=0, e=0, f=0; private int dice1; public dice(int dice1, int a, int b, int c, int d, int e, int f){ this.dice1 = dice1; this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; this.f = f; } public int getdice1(){ return dice1; } public void countnumbers(){ if (dice1 == 1){ a++; } else if(dice1 == 2){ b++; } else if(dice1 == 3){ c++; } else if(dice1 == 4){ d++; } else if(dice1 == 5){ e++; } else if(dice1 == 6){ f++; } } }
any on this? thank time!
there multiple things.
one of code unnecessary.
public dice(int dice1 /*, int a, int b, int c, int d, int e, int f*/ ){ this.dice1 = dice1; //this.a = a; //this.b = b; //this.c = c; //this.d = d; //this.e = e; //this.f = f; }
you don't need commented lines of code because reset integers a-f , being done. means don't need variables a-f in head class.
public class head { public static void main(string[] args) { //int a=0; //int b=0; //int c=0; //int d=0; //int e=0; //int f=0; for(int i=0; i<5; i++){ int dice1 = 1 + (int)(math.random() * ((6 - 1) + 1)); dice diceobject = new dice(dice1 /*, a, b, c, d, e, f*/ ); diceobject.countnumbers(); diceobject.saveresults(); } system.out.println("a b c d e f"); dice.displayresults(); } }
again, snips of code not needed commented out.
the next thing when write code
system.out.println(diceobject);
that doesn't work because diceobject instance of class (dice) , isn't variable.
the way got work having variables sa, sb, sc, sd, se, , sf added.
private static int sa = 0, sb = 0, sc = 0, sd = 0, se = 0, sf = 0;
i added these results each time loop runs saved. created void called saveresults(); variables had static because void static too. made displayresults void (i'll later) static accessed head class.
public void saveresults() { sa += a; sb += b; sc += c; sd += d; se += e; sf += f; }
the += means add; last thing did make displayresults void. displays variables sa-sf using system.out.println();
public static void displayresults() { system.out.println(sa + " " + sb + " " + sc + " " + sd + " " + se + " " + sf); }
this went make final product. here code: head class:
public class head { public static void main(string[] args) { for(int i=0; i<5; i++){ int dice1 = 1 + (int)(math.random() * ((6 - 1) + 1)); dice diceobject = new dice(dice1); diceobject.countnumbers(); diceobject.saveresults(); } system.out.println("a b c d e f"); dice.displayresults(); }
} dice class: public class dice { private int a=0, b=0, c=0, d=0, e=0, f=0;
private static int sa = 0, sb = 0, sc = 0, sd = 0, se = 0, sf = 0; private int dice1;
public dice(int dice1){ this.dice1 = dice1; } public int getdice1(){ return dice1; } public void countnumbers(){ if (dice1 == 1){ a++; } else if(dice1 == 2){ b++; } else if(dice1 == 3){ c++; } else if(dice1 == 4){ d++; } else if(dice1 == 5){ e++; } else if(dice1 == 6){ f++; } } public void saveresults() { sa += a; sb += b; sc += c; sd += d; se += e; sf += f; } public static void displayresults() { system.out.println(sa + " " + sb + " " + sc + " " + sd + " " + se + " " + sf); } }
i beginner @ java too. , coincidence, time trying teach myself. there course on codecademy.com teaches basics. recommend , free. sorry took long post answer, took while figure out wrong.
Comments
Post a Comment