Algorithm for scissor paper stone

前端 未结 8 633
日久生厌
日久生厌 2021-02-06 20:03

I am using the following method which works but wondering if there is a better algorithm to perform the test. Is there a better way to do it? Doing this in C# but putting syntax

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-06 20:21

    \From A java beginner Perspective. User plays with the computer to infinity.

    import java.util.Scanner;
    
    public class AlgorithmDevelopmentRockPaperScissors{
        public static void main(String[] args){
    
            System.out.println("\n\nHello Eric today we are going to play a game.");
            System.out.println("Its called Rock Paper Scissors.");
            System.out.println("All you have to do is input the following");
            System.out.println("\n  1 For Rock");
            System.out.println("\n        2 For Paper");
            System.out.println("\n        3 For Scissors");
    
            int loop;
            loop = 0;
    
            while (loop == 0){
                System.out.println("\n\nWhat do you choose ?");
    
    
            int userInput;
            Scanner input = new Scanner(System.in);
            userInput = input.nextInt();
    
            while (userInput > 3 || userInput <= 0 ){ //ensure that the number input by the sure is within range 1-3. if else the loop trap.
    
                System.out.println("Your choice "+userInput+" is not among the choices that are given. Please enter again.");
                userInput = input.nextInt();
            }
    
    
    
            switch (userInput){
                case 1:
                System.out.println("You Chose Rock.");
                break;
    
                case 2:
                System.out.println("You Chose Paper.");
                break;
    
                case 3:
                System.out.println("You Chose Scissors");
                break;
    
                default:
                System.out.println("Please Choose either of the choices given");
                break;
    
            }
    
    
    
            int compInput;
            compInput = (int)(3*Math.random()+1);
    
            switch (compInput){
                case 1:
                System.out.println("\nComputer Chooses Rock.");
                break;
    
                case 2:
                System.out.println("\nComputer Chooses Paper.");
                break;
    
                case 3:
                System.out.println("\nComputer Chooses Scissors");
                break;
    
            }
    
            if (userInput == compInput){
    
                System.out.println(".........................................");
                System.out.println("\nYou Both chose the same thing, the game ends DRAW.");
                System.out.println(".........................................");
            }
    
            if (userInput == 1 && compInput == 2){
    
                System.out.println(".........................................");
                System.out.println("\nComputer wins because Paper wraps rock.");
                System.out.println(".........................................");
            }
    
            if (userInput == 1 && compInput == 3){
    
                System.out.println(".........................................");
                System.out.println("\nYou win because Rock breaks Scissors.");
                System.out.println(".........................................");
            }
    
            if (userInput == 2 && compInput == 1){
    
                System.out.println(".........................................");
                System.out.println("\nYou win Because Paper wraps Rock");
                System.out.println(".........................................");
            }
    
            if (userInput == 2 && compInput == 3){
    
                System.out.println(".........................................");
                System.out.println("\nComputer wins because Scissors cut the paper");
                System.out.println(".........................................");
            }
    
            if (userInput == 3 && compInput == 1){
    
                System.out.println(".........................................");
                System.out.println("\nComputer Wins because Rock Breaks Scissors.");
                System.out.println(".........................................");
            }
    
            if (userInput == 3 && compInput == 2){
    
                System.out.println(".........................................");
                System.out.println("\nYou win because scissors cut the paper");
                System.out.println(".........................................");
            }
    
            }
    
    
        }
    
    }
    

提交回复
热议问题