我不确定我的代码到底有什么问题,因为它以前曾在起作用,但现在它向我显示此错误。
main.cs(179,20):错误CS0161:`CarProgram.ChangeGears(int,string)':并非所有代码路径都返回值
该程序用于汽车功能,在某种程度上类似于模拟器,但我想先解决第一个错误,然后再继续进行其他操作。
这里是代码
using System;
class CarProgram {
public static int speed = 0;
public static string gear = "P";
static void Main(string[] args)
{
string selection;
string g;
const int MAX_SPEED =55;
const int ADD_SPEED =5;
const int DECELERATE =-5;
char signal_on ='X';
int temperature =0;
int choice = 1;
bool key_is_in_ignition = false;
bool car_is_started = false;
bool seatbelt_is_fastened = false;
bool wipers_are_on = false;
bool lights_are_on = false;
bool HiBeams_are_on = false;
bool radio_is_on = false;
bool hazard_is_on = false;
// Main Menu.
while (choice < 5 )
{
Console.WriteLine("Choose an option");
Console.WriteLine("1) Start The Car");
Console.WriteLine("2) Key in Ignition");
Console.WriteLine("3)Fasten Seat Belt");
Console.WriteLine("4)Stop The Car");
Console.WriteLine("5) Exit");
Console.Write("\r\nSelect an option: ");
selection = Console.ReadLine();
choice = Convert.ToInt32(selection);
while (choice < 1 || choice > 5 )
{
Console.WriteLine("Valid choices are 1 through 5");
Console.WriteLine("Please select an option");
Console.WriteLine("You typed a " + choice);
Console.WriteLine("\r\n select 1 through 5: ");
selection = Console.ReadLine();
choice = Convert.ToInt32(selection);
}
switch (choice)
{
case 1:
{
if (key_is_in_ignition)
{
while (choice < 10)
{
Console.WriteLine("Choose an option");
Console.WriteLine("1) Change Gears");
Console.WriteLine("2) Step On The Gas");
Console.WriteLine("3) Step On The Brake");
Console.WriteLine("4) Turn on/off The lights");
Console.WriteLine("5) Turn on/off The Signal");
Console.WriteLine("6) Turn on/off The Hazard lights");
Console.WriteLine("7) Turn on/off The Radio");
Console.WriteLine("8) Turn on/off The Wipers");
Console.WriteLine("9) Adjust The Temperature");
Console.WriteLine("10) Return to Main Menu");
Console.WriteLine("\r\n Select an option");
selection= Console.ReadLine();
choice = Convert.ToInt32(selection);
while (choice < 1 || choice > 10 )
{
Console.WriteLine("Valid choices are 1 through 10");
Console.WriteLine("Please select an option");
Console.WriteLine("You typed a " + choice);
Console.WriteLine("\r\n select 1 through 10: ");
selection = Console.ReadLine();
choice = Convert.ToInt32(selection);
}//close while
whichOption(choice);
}
}//close If
else
{
key_is_in_ignition = insertKey(key_is_in_ignition);
}
break;
}// close case 1
case 2:
{
break;
}
case 3:
{
break;
}
}// close switch
}// close while
}// close main
// CHECK FOR KEY
private static bool insertKey(bool key)
{
string haveKey;
Console.WriteLine("Do you have the key in the Ignition (Y/N)?");
haveKey = Console.ReadLine();
haveKey = haveKey.ToUpper();
Console.WriteLine("You Choose" + haveKey);
if (haveKey == "Y")
{
Console.WriteLine("Key is In The Ignition!! ");
return true;
}
else
{
Console.WriteLine("Key must be In The Ignition before car can start!! ");
return false;
}
}
private static int whichOption(int x)
{
switch(x)
{
case 1:
{
ChangeGears(speed, gear);
break;
}
case 2:
{
Gas(speed, gear);
break;
}
case 3:
{
Brake();
break;
}
case 4:
{
Lights();
break;
}
case 5:
{
Turn();
break;
}
case 6:
{
Hazard();
break;
}
case 7:
{
Radio();
break;
}
case 8:
{
Wipers();
break;
}
case 9:
{
Temperature();
break;
}
case 10:
{
Done();
break;
}
}//close switch
return 0;
}// close ChangeGears
private static int ChangeGears(int s, string g)
{
Console.WriteLine("Inside the function Change Gears");
if (s == 0)
{
string haveGear;
Console.WriteLine("What gear would you like to have?");
haveGear = Console.ReadLine();
haveGear = haveGear.ToUpper();
if (haveGear == "P" || haveGear == "R" || haveGear == "N" || haveGear == "D")
{
switch (haveGear)
{
case "P":
{
Console.WriteLine("You are in park");
break;
}
case "N":
{
Console.WriteLine("You are in neutral");
break;
}
case "D":
{
Console.WriteLine("You are in drive");
break;
}
case "R":
{
Console.WriteLine("You are in reverse");
break;
}
}//close
g = haveGear;
}
else
{
Console.WriteLine("The speed must be 0 to change gears.");
}
Console.WriteLine("Gear is "+ g);
return 0;
}
}// close ChangeGears
private static int Gas(int s, string g)
{
Console.WriteLine("Inside the function Gas");
switch (g)
{
case "P":
{
Console.WriteLine("You cannot drive while in park");
break;
}
case "N":
{
Console.WriteLine("Car is in neutral, the gas won't go up");
break;
}
case "D":
{
s += 5;
Console.WriteLine("Yo are going " + s + "MPH");
if (s > 55)
{
Console.WriteLine("You are speeding!!!!!");
}
break;
}
case "R":
{
s += 5;
Console.WriteLine("Yo are going " + s + "MPH");
if (s > 20)
{
Console.WriteLine("You are going too fast for reverse!!!!");
}
break;
}
}//close
return 0;
}// close Gas
private static int Brake()
{
Console.WriteLine("Inside the function Brake");
return 0;
}// close Brake
private static int Lights()
{
Console.WriteLine("Would you like to turn the lights on or off? |O-On| |F-Off|");
return 0;
}// close Lights
private static int Turn()
{
Console.WriteLine("Which turn signal do you need? |L=Left| |R=Right|");
return 0;
}// close Turn
private static int Hazard()
{
Console.WriteLine("Inside the function Hazard");
return 0;
}// close Hazard
private static int Radio()
{
Console.WriteLine("Inside the function Radio");
return 0;
}// close Radio
private static int Wipers()
{
Console.WriteLine("Inside the function Wipers");
return 0;
}// close Wipers
private static int Temperature()
{
Console.WriteLine("Inside the function Temperature");
return 0;
}// close Temperature
private static int Done()
{
Console.WriteLine("Returning to main menu");
return 0;
}// close Done
}
格式化您的例程,
private static int ChangeGears(int s, string g) {
Console.WriteLine("Inside the function Change Gears");
if (s == 0) {
...
/* Some logic here */
...
return 0;
}
//TODO: return value when s != 0
}// close ChangeGears
您显然看到ChangeGears
时,s != 0
不返回任何内容>