【问题标题】:i cant understand why there is error in the while loop我不明白为什么 while 循环中有错误
【发布时间】:2022-12-10 21:35:36
【问题描述】:

试图从学生的用户和他的成绩中获取输入,当用户输入的名称为“FINISH”时,循环应该停止

错误 CS0165:使用未分配的局部变量“名称”

试图在循环外获取姓名输入,先看看用户是否想完成,然后在循环内获取成绩,

using System;

namespace whileLoops
{
    class Program
    {
        static void Main(string[] args)
        {
            string name;
            int grade, aboveNf;

            Console.WriteLine("Name of the student: ");
            mame = Console.ReadLine();

            aboveNf = 0;
            grade = 0;

            while(name != "FINISH")
            {
                Console.WriteLine("Grade: ");
                grade = int.Parse(Console.ReadLine());

                if (grade>95)
                    aboveNf++;
                
                Console.WriteLine("Name of the student: ");
                name = Console.ReadLine();
            }
            Console.WriteLine(name + " " + grade);
            Console.WriteLine(aboveNf + " " + "got above 95");
        }
    }
}

【问题讨论】:

  • 查看您分配给此处的变量:mame = Console.ReadLine();。那是错字吗?如果是这样,你为什么要在这里输入代码而不是复制/粘贴它?
  • 你能更简单地解释一下吗,我不确定我是否理解你的意思

标签: c# loops while-loop


【解决方案1】:

问题是您正在定义变量name

string name;

然后你设置一个值给一个不存在的变量mame

mame = Console.ReadLine();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-10
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    相关资源
    最近更新 更多