0

I am attempting to create a C# program with two classes that will make a decision based on the user's input to output a response (ex. the user says rock and the computer says paper) that makes the user always lose. I currently have errors saying

"Cannot implicitly convert type 'int' into type 'string'" on lines with all of the cases ("case1:", "case2:", "case3:").

Yet I have defined the "nameClassif" as a string just above. I also have a warning: Warning CS0649: Field 'Player.classif' is never assigned to, and will always have its default value null

using System;
using static System.Console;

namespace Tes
{
    class PlayerApp
    {
        public static void Main()
        {
            Player player1 = new Player();
            player1.PlayerChoice = InputValue();
            player1.Classif = InputValue();
            Clear();

            Write(player1);
            Write("\n\n\n\n");
            ReadKey();
        }

        public static string InputValue()
        {
            Write("Please enter rock, paper, or scissors:\t");
            return ReadLine();
        }

    }

    class Player
    {
        private string classif;


        // constructors
        public Player()
        { }

        public Player(string pC)
        {
            PlayerChoice = pC;
        }

        // properties
        public string PlayerChoice { get; set; }

        public string Classif { get; set; }

        public double SetFine()
        {
            if (classif == "rock")
            {
                WriteLine("The computer chose paper. You lose.");

            }

            else if (classif == "paper")
            {
                WriteLine("The computer chose scissors. You lose.");
            }

            else if (classif == "scissors")
            {
                WriteLine("The computer chose rock. You lose.");
            }


            return SetFine();
        }

public string ReturnNameOfClassification()
        {

            string nameClassif;
            switch (classif)
            {
                case 1: nameClassif = "rock"; break;
                case 2: nameClassif = "paper"; break;
                case 3: nameClassif = "scissors"; break;
                default:
                    WriteLine("Invalid selection..." +
                              " I assume you chose rock... :");
                    nameClassif = "rock - ???"; break;
            }
            return nameClassif;
        }

        public override string ToString()
        {
            return "\n\n\n\t\tRock-Paper-Scissors" +
                "\nThe computer chose\t" + PlayerChoice;
        }
    }
}
2
  • 1
    Answer based on your question title: string a = 5; Commented Mar 14, 2019 at 2:36
  • 1
    @John (OP john) Did any of the answers work for you? If so please mark one as the accepted answer using the checkmark below the down arrow. Commented May 24, 2019 at 15:25

3 Answers 3

2

The issue is that you declare classif as a string, but you compare it to int:

private string classif;
switch (classif)
{
    case 1: nameClassif = "rock"; break; // 1 is an int, not a string!
}

In short: "5" is not equal to 5, nor is it comparable to 5. To fix this, you should compare like with like instead:

switch (classif)
{
    case "1": nameClassif = "rock"; break;
    case "2": nameClassif = "paper"; break;
    case "3": nameClassif = "scissors"; break;
    default:
        WriteLine("Invalid selection..." +
                  " I assume you chose rock... :");
        nameClassif = "rock - ???"; break;
}

Now the type you're comparing is the same as the type you're comparing it to, so the code works.

Sign up to request clarification or add additional context in comments.

Comments

2

When you use a switch statement, the values in the case statement need to match the type of the variable you're switching on. You're switching on classif, which is a string, but your case statements are 1, 2, 3, which are int

Comments

0

This one:

case 1, 2, 3 means an integer.. that's why it returns an error.

 case "1": nameClassif = "rock"; break;
  case "2": nameClassif = "paper"; break;
  case "3": nameClassif = "scissors"; break;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.