2021年1月23日星期六

Assigning an enum based on the user input

I'm trying to build a simple console TicTacToe game. I'm trying to get the user to either select Circle or Cross to play as. I have an enum for Circle and Cross.

Everytime I run the program, I get the else output, even when I type in either "Circle" or "Cross".

Not looking for code answers, just looking to see if someone can point me in the direction of my mistake.

import java.util.Scanner;    public class Game {        private Model model;      private Model player;        public Game() {            Scanner input = new Scanner(System.in);          System.out.println("Do you want to play as Cross or Circle");          input.nextLine();            if(input.equals(Model.valueOf("CROSS"))) {              player = Model.CROSS;          } else if(input.equals(Model.valueOf("CIRCLE"))) {              player = Model.CIRCLE;          } else {              System.out.println("Invalid choice, please choose either Circle or Cross");          }        }  }  
public enum Model {    CROSS, CIRCLE  }  
https://stackoverflow.com/questions/65866171/assigning-an-enum-based-on-the-user-input January 24, 2021 at 08:58AM

没有评论:

发表评论