mercredi 15 juin 2016

Rock, paper, lizard, spock. User has to input 100 games and it program should give the outcome of how many of each were choosen

I got this code from youtube, but I don't know how to ask the user to input 100 games and show the outcome of how many of each were used.

import java.util.Scanner;

public class RockPaperSissors {

public static void main(String[] args) {
    Scanner console = new Scanner (System.in);

    String choice = console.nextLine();

    if (isValid(choice)){
        int aIChoice = (int)(Math.random()*3);
        int personChoice = getVal(choice);
        System.out.println("You picked " + choice);
        System.out.println("The computer picked " + getAIChoice(aIChoice));
        System.out.println("You " + didPersonWin(personChoice, aIChoice));
    }
    else {
        System.out.println("That is not a valid input!");
    }
}
public static String getAIChoice(int x){
    if (x == 0){
        return "Rock";
    }
    if (x ==1){
        return "Paper";
    }
    if (x == 2){
        return "Scissors";
    }
    return null;
}
public static String didPersonWin(int pChoice, int computerChoice){
    if (pChoice == 0){
        if (computerChoice != 1){
            if (computerChoice != 0){
                return "WIN" ;
            }
            return "Tie";
        }
        return "Lose";
    }
    if (pChoice == 1){
        if (computerChoice != 2){
            if (computerChoice != 1){
                return "WIN";
            }
            return "Tie";
        }
        return "Lose";
    }
    if (computerChoice != 0){
        if (computerChoice != 2){
            return "Win";
        }
        return "Tie";
    }
    return "Lose";
}
public static boolean isValid(String string){
    if (string.equalsIgnoreCase("rock")){
        return true;
    }
     if (string.equalsIgnoreCase("paper")){
        return true;
    }
     if (string.equalsIgnoreCase("scissors")){
         return true;
     }
        return false;
}
public static int getVal(String string){
    if (string.equalsIgnoreCase("rock")){
        return 0;
    }
    if (string.equalsIgnoreCase("paper")){
        return 1;
    }
    else {
        return 2;
    }

}

}

Aucun commentaire:

Enregistrer un commentaire