using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace CSharpControl02 { class Program { static void Main(string[] args) { new Program().deep(); } public void deep() { do { int o, a, b; Console.Write(" 请选择难度: "); o = Convert.ToInt32(Console.ReadLine()); switch (o) { case 1: { Console.Write(" 请输入行: "); a = ReadInt(); Console.Write(" 请输入列: "); b = ReadInt(); Easy(a, b); break; } case 2: { Console.Write(" 请输入行: "); a = ReadInt(); Console.Write(" 请输入列: "); b = ReadInt(); Medium(a, b); break; } case 3: { Console.Write(" 请输入行: "); a = ReadInt(); Console.Write(" 请输入列: "); b = ReadInt(); Hard(a, b); break; } case 4: Help(); break; default: Console.WriteLine("\n输入错误!!!\n"); continue; } } while (true); } private int ReadInt() { int i; while (!int.TryParse(Console.ReadLine(), out i)) ; return i; } private void Help() { Console.WriteLine("help"); } private void Hard(int a, int b) { Console.WriteLine("hard"); } private void Medium(int a, int b) { Console.WriteLine("medium"); } private void Easy(int a, int b) { Console.WriteLine("easy"); } } }
Convert.ToInt32 你输入完了再转就不会报错了
public void deep() { do { int o, a, b; Console.Write(" 请选择难度: "); o = Convert.ToInt32(Console.ReadLine()); if(o>0 && 0<4) { Console.Write(" 请输入行: "); a = Convert.ToInt32(Console.ReadLine()); Console.Write(" 请输入列: "); b = Convert.ToInt32(Console.ReadLine()); } switch (o) { case 1: { Easy(a, b); break; } case 2: { Medium(a, b); break; } case 3: { Hard(a, b); break; } case 4: Help(); break; default: Console.WriteLine("\n输入错误!!!\n"); continue; } } while (true); }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace CSharpControl02
{
class Program
{
static void Main(string[] args)
{
new Program().deep();
}
public void deep()
{
do
{
int o, a, b;
Console.Write(" 请选择难度: ");
o = Convert.ToInt32(Console.ReadLine());
switch (o)
{
case 1:
{
Console.Write(" 请输入行: ");
a = ReadInt();
Console.Write(" 请输入列: ");
b = ReadInt();
Easy(a, b); break;
}
case 2:
{
Console.Write(" 请输入行: ");
a = ReadInt();
Console.Write(" 请输入列: ");
b = ReadInt();
Medium(a, b); break;
}
case 3:
{
Console.Write(" 请输入行: ");
a = ReadInt();
Console.Write(" 请输入列: ");
b = ReadInt();
Hard(a, b); break;
}
case 4:
Help(); break;
default:
Console.WriteLine("\n输入错误!!!\n"); continue;
}
} while (true);
} private int ReadInt()
{
int i;
while (!int.TryParse(Console.ReadLine(), out i)) ;
return i;
} private void Help()
{
Console.WriteLine("help");
} private void Hard(int a, int b)
{
Console.WriteLine("hard");
} private void Medium(int a, int b)
{
Console.WriteLine("medium");
} private void Easy(int a, int b)
{
Console.WriteLine("easy");
} }
}
你输入完了再转就不会报错了
{
do
{
int o, a, b;
Console.Write(" 请选择难度: ");
o = Convert.ToInt32(Console.ReadLine());
if(o>0 && 0<4)
{
Console.Write(" 请输入行: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write(" 请输入列: ");
b = Convert.ToInt32(Console.ReadLine());
}
switch (o)
{
case 1:
{
Easy(a, b); break;
}
case 2:
{
Medium(a, b); break;
}
case 3:
{
Hard(a, b); break;
}
case 4:
Help(); break;
default:
Console.WriteLine("\n输入错误!!!\n"); continue;
}
} while (true);
}
看不懂额,貌似调试的时候貌似那个私有的ReadInt没有用
结果还是一样的额,直接按enter会抛出异常
//不是任何东西都可以Convert.ToInt32的.想使用Convert.ToInt32之前处理判断下字符.另外 C# 里面switch 是可以支持 string ,直接用成string 型就可以啊. / C,C++中SWITCH不支持
public void deep()
{
do
{
int o, a, b;
Console.Write(" 请选择难度: ");
string input = Console.ReadLine();
o = Convert.ToInt32(input);
switch (o)
{
case 1:
{
Console.Write(" 请输入行: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write(" 请输入列: ");
b = Convert.ToInt32(Console.ReadLine());
Easy(a, b); break;
}
case 2:
{
Console.Write(" 请输入行: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write(" 请输入列: ");
b = Convert.ToInt32(Console.ReadLine());
Medium(a, b); break;
}
case 3:
{
Console.Write(" 请输入行: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write(" 请输入列: ");
b = Convert.ToInt32(Console.ReadLine());
Hard(a, b); break;
}
case 4:
Help(); break;
default:
Console.WriteLine("\n输入错误!!!\n"); continue;
}
} while (true);
}看这个行不行?
public void deep()
{
do
{
int o, a, b;
Console.Write(" 请选择难度: ");
string input = Console.ReadLine();
o = Convert.ToInt32(input);
switch (o)
{
case 1:
{
Console.Write(" 请输入行: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write(" 请输入列: ");
b = Convert.ToInt32(Console.ReadLine());
Easy(a, b); break;
}
case 2:
{
Console.Write(" 请输入行: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write(" 请输入列: ");
b = Convert.ToInt32(Console.ReadLine());
Medium(a, b); break;
}
case 3:
{
Console.Write(" 请输入行: ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write(" 请输入列: ");
b = Convert.ToInt32(Console.ReadLine());
Hard(a, b); break;
}
case 4:
Help(); break;
default:
Console.WriteLine("\n输入错误!!!\n"); continue;
}
} while (true);
}