Advertisement
Guest User

adasdsa

a guest
Oct 22nd, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package innlevering2;
  2. public class Oppgave1 {
  3. private static int dice1, dice2, diceSum;
  4. private static int twos, threes, fours, fives, sixes,
  5. sevens, eights, nines, tens, elevens, twelves;
  6.  
  7. public static void main(String[] args) {
  8. sumToTerninger();
  9. skrivStatistikk();
  10. skrivDiagram();
  11. }
  12. // 1 a)
  13. public static int sumToTerninger() {
  14. dice1 = (int)(Math.random()*6+1);
  15. dice2 = (int)(Math.random()*6+1);
  16. diceSum = dice1+dice2;
  17. return diceSum;
  18. }
  19. private static void antallKastLoop(int antallKast) {
  20. for (int i = 0; i < antallKast; i++)
  21. {
  22. int temp = sumToTerninger();
  23. if (temp == 2)
  24. twos++;
  25. else if (temp == 3)
  26. threes++;
  27. else if (temp == 4)
  28. fours++;
  29. else if (temp == 5)
  30. fives++;
  31. else if (temp == 6)
  32. sixes++;
  33. else if (temp == 7)
  34. sevens++;
  35. else if (temp == 8)
  36. eights++;
  37. else if (temp == 9)
  38. nines++;
  39. else if (temp == 10)
  40. tens++;
  41. else if (temp == 11)
  42. elevens++;
  43. else if (temp == 12)
  44. twelves++;
  45. }
  46. }
  47. // 1 b)
  48. private static void skrivStatistikk() {
  49. int kast = 100;
  50. antallKastLoop(kast);
  51. 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");
  52. 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);
  53. }
  54. // 1 c)
  55. private static void skrivDiagram() {
  56. int kast2 = 100;
  57. antallKastLoop(kast2);
  58. for (int i = 0; i < twos; i++) {
  59. System.out.print("2-");
  60. }
  61. for (int i = 0; i < threes; i++) {
  62. System.out.print("-");
  63. }
  64. System.out.println("");
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement