-5

I'm trying to learn java on my own. I am now re-doing some programs I've done before, but in a different way. Now I am stuck with a for-loop. I have the following 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<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 all fine and do what I want it to. Now I want to create a constructor with a method that do all the countings for me. So far so good. But when it comes to printing it out, the output returns everything the number of times it does the calculation. i.e, if I set i<5 it prints 1 2 3 4 5 6 five times, and not only one time.

Here is the 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 help on this? Thank you very much for your time!

JJ72
  • 62
  • 9

1 Answers1

2

There are multiple things.

One is that some of your code is 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 the commented lines of code because they will reset the integers a-f and that is already being done. That also means that you don't need the variables a-f in the 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, the snips of code not needed are commented out.

The next thing is that when you write the code

System.out.println(diceObject);

that doesn't work because diceObject is an instance of a class (Dice) and it isn't a variable.

The way I got everything to work was by having the variables sa, sb, sc, sd, se, and sf added.

private static int sa = 0, sb = 0, sc = 0, sd = 0, se = 0, sf = 0;

I added these so that the results from each time the loop runs will be saved. I created a void to do this called saveResults(); The variables had to be static because the void was static too. I made the displayResults void (i'll get into that later) static so it could be accessed from a the Head class.

public void saveResults() {
    sa += a;
    sb += b;
    sc += c;
    sd += d;
    se += e;
    sf += f;
}

The += just means to add; The last thing I did was make the displayResults void. It just displays the variables sa-sf using System.out.println();

public static void displayResults() {
    System.out.println(sa + " " + sb + " " + sc + " " + sd + " " + se + " " + sf);
}

This all went together to make the final product. Here is the 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 am a beginner at java too. And a coincidence, for some time I was trying to teach it to myself. There is a course on codecademy.com that teaches you the basics. I recommend it and it is free. Sorry it took long to post this answer, it took a while to figure out what was wrong.