Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine("What is the correct way to declare a variable to store an integer value in C#?");
		Console.WriteLine("a. int 1x=10;");
		Console.WriteLine("b. int x=10;");
		Console.WriteLine("c. float x=10.0f;");
		Console.WriteLine("d. string x=10");
		
		Console.WriteLine(Getanswer('a'));					  
		Console.ReadLine();
	}
	
	static int Getanswer(char answer)
	{
		string result;  
		switch (answer)
		{
			case 'a': 
				result = "Incorrect Answer!";
				break;
			case 'b': 
				result = "Correct!";
				break;	
			case 'c': 
				result = "Incorrect Answer!";
				break;
			case 'd': 
				result = "Incorrect Answer!";
				break;
			default:
				result = "Invalid Answer";
				break;
		} 
			
		return result;
	}
}


What I have tried:

Hi, I just wanted to ask for some help. I am a beginner on C# and I am practicing with the switch statement. Thank you!
Posted
Updated 22-Aug-20 20:52pm

1 solution

Look at your code:
C#
static int Getanswer(char answer)
    {
    string result;  
    ...
    return result;
    }
Your method is defined as returning an integer, but you are explicitly telling it to return a string. You can't do that: change the definition of the method to return a string and that'll fix it:
C#
static string Getanswer(char answer)
    {
    string result;  
    ...
    return result;
    }
 
Share this answer
 
Comments
JJTY 23-Aug-20 6:29am    
@OriginalGriff Wow, thanks! I did not notice that.
OriginalGriff 23-Aug-20 7:01am    
Easy mistake to make!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900