Hi,
I am new to learning c# and I am currently trying to unpick the errors in a piece of code as practice. I feel clear on what a switch statement is, but I can't see how to correct it to make it work.
I have two questions, the first is can the case value be a situation/scenario, eg. 'when a value is lower than....' or does it have to be an absolute value?
Secondly, does the output of a switch statement have to be a text string or can it be an instruction?
Below is the code I am trying to unravel. Any help, much appreciated.
int
high, low, number = 0, count = 0, values = 0, total; float average;high = -32768;
low = 32767;
number = 0 = count = total = 0;
while(number < 1){
Console.WriteLine("\nEnter a Number for repetitions:");
number = Int32.Parse(Console.ReadLine());
if(number < 1)Console.WriteLine("\nNumber must be greater than 1");
}
for (count = 0; count < number; count++){
Console.WriteLine("Enter number {0} : ", count+1);
values = Console.ReadLine();
Switch(values);
{
case (values > high) : Console.WriteLine("high = values"); break; case (values < low) : low = values; break; default; break;}
total = total + values;
}
average = total/ number;
Console.WriteLine("\nAverage of {0} numbers is {1}", number, average);
Console.WriteLine("nHighest is {2}. Lowest is {3}", high, low);
//Console.ReadLine();
}
}
}
Tom GunnerPosted Jun 2, 2008, 5:02 AM
DavePosted May 31, 2008, 9:15 AM
Each case label can have its own block of code. It can even have a nested switch inside.
case 1:
{
do stuff...
break;
}
case ...
DavePosted May 31, 2008, 9:13 AM
If you need to evaluate a boolean expression, you need to use an if/else construct.