TheRealL

Untitled

Oct 26th, 2011
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class steinSaksPapir {
  5.     Random rand = new Random();
  6.     int randomNumber = rand.nextInt(3) + 1;
  7.    
  8.     public static void main(String[] args) {
  9.         Scanner input = new Scanner(System.in);
  10.         Random rand = new Random();
  11.        
  12.         final int ROCK = 1, PAPER = 2, SCISSORS = 3;
  13.         int rps;
  14.         boolean userWins = false;
  15.         boolean programWins= false;
  16.        
  17.         do {
  18.             rps = rand.nextInt(3) + 1;
  19.             System.out.print("Hva velger du? (Stein/Saks/Papir) ");
  20.             String choice = input.nextLine();
  21.            
  22.             System.out.print("Programmet valgte ");
  23.             if (rps == ROCK) System.out.println("stein");
  24.             else if (rps == PAPER) System.out.println("papir");
  25.             else if (rps == SCISSORS) System.out.println("saks");
  26.            
  27.             if(choice.equals("stein")) {
  28.                 userWins = rps == SCISSORS;
  29.                 programWins = rps == PAPER;
  30.             } else
  31.             if (choice.equals("saks")) {
  32.                 userWins = rps == PAPER;
  33.                 programWins = rps == ROCK;
  34.             } else
  35.             if (choice.equals("papir")) {
  36.                 userWins = rps == ROCK;
  37.                 programWins = rps == SCISSORS;
  38.             }          
  39.             else {
  40.                 System.out.print("Du må velge steinm saks eller papir!");
  41.                 choice = input.nextLine();
  42.             }    
  43.            
  44.     }
  45.             while (!userWins && !programWins);
  46.             System.out.print("A winner is ");
  47.             if (userWins) System.out.println("you!");
  48.             else System.out.println("program!");
  49. }
  50. }
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment