Reputation: 41
So I've got 2 Mains and I'm trying to combine them to run together and struggling. The 1st rolls a single die 6000 times and shows the results and the second rolls 2 dice 11000 times and shows the results. What is the easiest way to combine these so they run 1 and than the other. Programs as follows:
public class DieTest
{
public static final int N = 6000;
public static void main(String[] args)
{
int[] d = new int[7];
for (int i = 1; i < 7; i++) d[i] = 0;
for (int k = 0; k < N; k++)
{
int roll = (int)(6.0*Math.random() + 1.0);
d[roll]++;
}
System.out.print("Rolls: " + N);
for (int i = 1; i < 7; i++)
System.out.print(", " + i + ": " + d[i]);
System.out.println();
}
}
and the second
public class Dice3
{
public static final int N = 11000;
public static int roll()
{
return (int)(6.0*Math.random() + 1.0);
}
public static void main(String[] args)
{
int[] d = new int[13];
for (int i = 1; i < 13; i++) d[i] = 0;
for (int k = 0; k < N; k++)
{
d[roll() + roll()]++;
}
System.out.print("Rolls: " + N);
for (int i = 2; i < 13; i++)
System.out.print(", " + i + ": " + d[i]);
System.out.println();
}
}
Upvotes: 0
Views: 91
Reputation: 13133
public class TwoDieFor
{
public static void main(String ... arguments)
{
DieTest.main(arguments);
Dice3.main(arguments);
}
}
Upvotes: 1
Reputation: 164139
1.Create another class to combine the code.
2.Create 1 method for each of the 2 main()
methods, of course with new names like roll1()
and roll2()
without parameters and paste inside them the code you already have.
3.Paste also the declarations of N
from DieTest
and Dice3
but rename the 2nd to M
and change every occurrence of N
to M
in the 2nd method you created.
4.You need also to paste the method roll()
.
5.Create a new main()
method like this:
public static void main(String[] args) {
roll1();
roll2();
}
Upvotes: 1