Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package innlevering2;
- public class Oppgave1 {
- private static int dice1, dice2, diceSum;
- private static int twos, threes, fours, fives, sixes,
- sevens, eights, nines, tens, elevens, twelves;
- public static void main(String[] args) {
- sumToTerninger();
- skrivStatistikk();
- skrivDiagram();
- }
- // 1 a)
- public static int sumToTerninger() {
- dice1 = (int)(Math.random()*6+1);
- dice2 = (int)(Math.random()*6+1);
- diceSum = dice1+dice2;
- return diceSum;
- }
- private static void antallKastLoop(int antallKast) {
- for (int i = 0; i < antallKast; i++)
- {
- int temp = sumToTerninger();
- if (temp == 2)
- twos++;
- else if (temp == 3)
- threes++;
- else if (temp == 4)
- fours++;
- else if (temp == 5)
- fives++;
- else if (temp == 6)
- sixes++;
- else if (temp == 7)
- sevens++;
- else if (temp == 8)
- eights++;
- else if (temp == 9)
- nines++;
- else if (temp == 10)
- tens++;
- else if (temp == 11)
- elevens++;
- else if (temp == 12)
- twelves++;
- }
- }
- // 1 b)
- private static void skrivStatistikk() {
- int kast = 100;
- antallKastLoop(kast);
- System.out.printf("%-6s%10s%10s%10s%10s%10s%10s%10s%10s%10s%10s%10s\n", "Sum", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");
- System.out.printf("%-6s%10d%10d%10d%10d%10d%10d%10d%10d%10d%10d%10d\n", "Antall", twos, threes, fours, fives, sixes, sevens, eights, nines, tens, elevens, twelves);
- }
- // 1 c)
- private static void skrivDiagram() {
- int kast2 = 100;
- antallKastLoop(kast2);
- for (int i = 0; i < twos; i++) {
- System.out.print("2-");
- }
- for (int i = 0; i < threes; i++) {
- System.out.print("-");
- }
- System.out.println("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement