繁体   English   中英

执行后... while循环未处理异常:System.FormatException:输入字符串格式不正确

[英]after do… while loop Unhandled Exception: System.FormatException: Input string was not in a correct format

所以我的实验室应该问你想要多少问题,然后给你随机生成的数学问题。 最后,它应该会询问您是否想要另一组问题或只是结束,您可以选择回答 y 或 n。 当我停止代码时,一切正常,但我还需要正确打印总金额,当我添加该代码时,我得到未处理的异常。 我的问题来自括号前的最后三行:

    using System;

class MainClass {
  public static void Main (string[] args) {
    var rnd = new Random();
    string exit = "";
    double score = 0;
    double total = 0; 
    double percent = 0;
    Console.WriteLine("How many problems do you want in a set?");
    double problem = Convert.ToDouble(Console.ReadLine());
    do{
    for(double i = 1; i<=problem; i++){
      double num1 = rnd.Next(1,9);
      double num2 = rnd.Next(1,9);

      Console.Write("Problem {0} : {1}*{2}=? ", i, num1, num2);
      double answer = Convert.ToDouble(Console.ReadLine());

      if(num1*num2 == answer){
        score++;
      }//end if

      total++;
      }
      Console.Write("Another set? <y/n>");
      exit = Console.ReadLine();
      Console.ReadLine();
    }while(exit!="n");
      percent = score/total;
      Console.WriteLine(" {} correct out of {} is a {}%",score,total,percent);
      Console.WriteLine("done");
  }//end main
}//end class

为参数 {0}、{1}、{2} 编号,或者您现在可以使用字符串插值,如下所示:

Console.WriteLine($"{score} correct out of {total} is a {percent}%");
      Console.WriteLine(" {} correct out of {} is a {}%",score,total,percent);

需要 0、1 和 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM