Java - closed loop

Greetings to Stack Overflow users, I come to you tonight to help with the Java program I created. I am relatively new to Java, so please excuse my ignorance about this topic. I made a Java program that the game "Scala" "Paper" "Scissors", and there seems to be an error in one of the statements.

import java.util.Scanner; public class TheAntlers { public static void main(String[] args) { int playerHumanWins = 0; int playerComputerWins = 0; int numberOfTies = 0; int computerResult; Scanner input = new Scanner(System.in); while(true) { String startGame; String playerHuman; String playerComputer = " "; System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): "); startGame = input.nextLine(); startGame = startGame.toUpperCase(); if(startGame.equals("N")) { System.out.println("NO!"); break; } else if(! startGame.equals("Y")) { startGame = startGame.toLowerCase(); System.out.println("Sorry, " + startGame + " is not a valid entry..."); } while(startGame.equals("Y")) { System.out.print("Please choose \"Rock\", \"Paper\", or \"Scissors\": "); playerHuman = input.nextLine(); computerResult = (int)(Math.random() * 3); playerHuman = playerHuman.toUpperCase(); if(computerResult == 1) { playerComputer = "ROCK"; } else if(computerResult == 2) { playerComputer = "PAPER"; } else if (computerResult == 3) { playerComputer = "SCISSORS"; } switch (playerHuman) { case "ROCK" : if(playerComputer.equals(playerHuman)) { System.out.println("Tie you both picked \"ROCK\""); numberOfTies++; } else if(playerComputer.equals("PAPER")) { System.out.println("Computer wins!"); playerComputerWins++; } else { System.out.println("You win, \"ROCK\" beats " + "\"" + playerComputer + "\""); playerHumanWins++; return; } break; case "PAPER" : if(playerComputer.equals(playerHuman)) { System.out.println("Tie you both picked \"PAPER\""); numberOfTies++; } else if(playerComputer.equals("ROCK")) { System.out.println("You win, \"PAPER\" beats " + "\"" + playerComputer + "\""); playerHumanWins++; return; } else { System.out.println("Sorry, the computer won!"); playerComputerWins++; } break; case "SCISSORS" : if(playerComputer.equals(playerHuman)) { System.out.println("Tie you both picked \"SCISSORS\""); numberOfTies++; } else if(playerComputer.equals("PAPER")) { System.out.println("You win, \"SCISSORS\" beats " + "\"" + playerComputer + "\""); playerHumanWins++; return; } else { System.out.println("Sorry, the computer won!"); playerComputerWins++; } break; default: playerHuman = playerHuman.toLowerCase(); System.out.println("Sorry, " + playerHuman + " is not a valid entry..."); break; } } } } } 

The problem I am facing is related to winning calculations. When I run the program and I go into rock several times until I win, the result will be you win, "ROCK" beats "" , but with any other option I get you win, "ROCK" beats "PAPER"

My question is: why do I get an empty callback when playing rock?

* Also, if you would like to indicate any other suggestions to help the novice, that would be great. *

+6
source share
3 answers

Math.random() * 3 is a number of at least 0 and less than 3.

After it is passed to int, it will be 0, 1 or 2.

  if(computerResult == 0) { playerComputer = "ROCK"; } else if(computerResult == 1) { playerComputer = "PAPER"; } else if (computerResult == 2) { playerComputer = "SCISSORS"; } 

Suggestions:

Be brief. You can change

 String startGame; startGame = input.nextLine(); startGame = startGame.toUpperCase(); 

to

 String startGame = input.nextLine().toUpperCase(); 

This is more readable when you do not need to scroll and scroll.

Also, be aware that equalsIgnoreCase() exists.

+5
source

This is not a complete newbie, but I would simulate the game using this code:

 enum Choice { ROCK, PAPER, SCISSORS } enum Result { COMPUTER_WINS, TIE, HUMAN_WINS } Result decide(Choice computer, Choice human) { if (human == computer) { return Result.TIE; } else if (…) { … } } 

Thus, you have a piece of code that processes the game itself, and another code handles user interaction.

+1
source

You will find that (int) (Math.random () * 3) leads to 0 sometimes and never 3, this is what gives you your space, since you have no result for 0.

In particular, its value when Math.random () returns less .33

0
source

Source: https://habr.com/ru/post/957099/


All Articles