我的代码:
if (numavg <=3 ){
Console.WriteLine("Congratz, we can start! but some note before :");
Console.WriteLine("When you put the last number, type 'c' in the next line for calculate the avg");
Console.WriteLine("So lets start! type the first number.");
avg1 = int.Parse(Console.ReadLine());
Console.WriteLine("Ok, so you choose " + avg1 + ",who is the next number?");
avg2 = int.Parse(Console.ReadLine());
Console.WriteLine("Wonderful! put the next number");
avg3 = int.Parse(Console.ReadLine());
if (avg3 = "c"){
Console.WriteLine("Ok, Lets calculate!");
average = (avg1 + avg2)/numavg;
Console.WriteLine("The average is " + average + ".");
}
else {
Console.WriteLine("Perfect! please type the next number,or 'c' for avg to the last 3 numbers.");
avg4 = int.Parse(Console.ReadLine());
if (avg4 = "c" ){
Console.WriteLine("Ok, Lets calculate!");
average1 = (avg1 + avg2 + avg3)/numavg;
Console.WriteLine("The average is " + average1 + ".");
}
}
问题在于类型“c”(用于计算平均值)。 我不明白如何将avg3和avg4保持为字符串和整数。
帮助?
更纯粹的问题 - 如果我希望程序结束并自动重启,我需要键入哪些代码? (如果无法重启,我怎样才能完成自动程序关闭的第一件事?)
谢谢你们!
注意: 错误是“无法将类型'字符串'隐式转换为'int'”。
答案 0 :(得分:-1)
您的意思是if(avg3 == "c")
而不是if (avg3 = "c")
(avg3 = "c")
您要将"c"
分配给avg3
int
,其类型为" Cannot implicitly convert type 'string' to 'int'".
String avg3Str = Console.ReadLine();
if (avg3Str.equals("c")){
Console.WriteLine("Ok, Lets calculate!");
average = (avg1 + avg2)/numavg;
Console.WriteLine("The average is " + average + ".");
}
else {
avg3 = int.Parse(avg3Str);
Console.WriteLine("Perfect! please type the next number,or 'c' for avg to the last 3 numbers.");
String avg4Str = Console.ReadLine();
if (avg4Str.equals("c") ){
Console.WriteLine("Ok, Lets calculate!");
average1 = (avg1 + avg2 + avg3)/numavg;
Console.WriteLine("The average is " + average1 + ".");
}
else
{
avg4 = int.Parse(avg4Str);
Console.WriteLine("Ok, Lets calculate!");
average1 = (avg1 + avg2 + avg3 + avg4)/numavg;
Console.WriteLine("The average is " + average1 + ".");
}
}