You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#初学者switch功能调用异常 出现Console/QuestionRectangle不存在报错

报错解决方法

1. "ProgramBase.Menu()"相关报错

  • 问题原因:你的项目中存在另一个命名为ProgramBase的类,该类内也定义了Menu()方法,或是你的Program类继承了ProgramBase类,导致方法签名冲突。
  • 解决方案:
    • 重命名当前你写的Menu()方法为其他名称,比如MainMenu()
    • 或给当前Menu()方法添加new修饰符,修改为private new static bool Menu()
    • 或删除项目中多余的ProgramBase类文件

2. "当前上下文中不存在名称“Console”"报错

  • 问题原因:你创建的项目类型不是控制台应用,可能误选了类库、WinForm、Web应用等不需要控制台交互的项目类型,或是System命名空间引用异常。
  • 解决方案:
    • 重新创建**控制台应用(.NET/.NET Framework)**类型的C#项目,将现有代码复制到新项目中即可
    • 确认代码第一行using System;没有被注释、拼写错误
    • 若上述操作无效,右键项目名称选择「清理」,再选择「重新生成」

3. "当前上下文中不存在名称“QuestionRectangle”"报错

  • 问题原因:这是前两个编译错误引发的连带报错,你的代码中QuestionRectangle()方法的定义和调用逻辑均无语法问题。
  • 解决方案:完成前两个报错的修复后,该报错会自动消失。
优化后可直接运行的完整代码
using System;

namespace ProgLab3
{
    class Program
    {
        static void Main(string[] args)
        {
            bool displayMenu = true;
            while (displayMenu)
            {
                displayMenu = Menu();
            }
        }

        // 存在ProgramBase冲突时加new修饰符,无冲突可省略
        private static bool Menu()
        {
            Console.Clear();
            Console.WriteLine("Welcome to the menu! Below are your choices of options.");
            Console.WriteLine("Type 1 to open Areas of Rectangles\n");
            Console.WriteLine("Type 2 to open Biggest Number\n");
            Console.WriteLine("Type 3 to open Valid Points\n");
            Console.WriteLine("Type 4 to open Dollar Game\n");
            Console.WriteLine("Type 5 to open Oldest Person\n");
            Console.WriteLine("Type 6 to open Hi Lo Game\n");
            Console.WriteLine("Type 7 to quit\n");

            switch (Console.ReadLine())
            {
                case "1":
                    QuestionRectangle();
                    // 新增暂停逻辑,方便用户查看结果后再返回菜单
                    Console.WriteLine("\n按任意键返回菜单...");
                    Console.ReadKey();
                    return true;
                case "2":
                    return true;
                case "3":
                    return true;
                case "4":
                    return true;
                case "5":
                    return true;
                case "6":
                    return true;
                case "7":
                    // 修正原逻辑,选择7才退出程序
                    return false;
                default:
                    Console.WriteLine("输入无效,请重新选择!");
                    Console.ReadKey();
                    return true;
            }
        }


        private static void QuestionRectangle()
        {
            double width1, length1, area1;
            double width2, length2, area2;

            Console.WriteLine("Welcome to the Areas of Rectangles choice.\nPlease start off by inputting the width of your first rectangle, do not include the metric unit of measurement.");
            width1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Now please input the length of the first rectangle.");
            length1 = Convert.ToDouble(Console.ReadLine());
            area1 = width1 * length1;

            Console.WriteLine("Alright great, now please input the width of your second rectangle.");
            width2 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Now what is the length of your second rectangle?");
            length2 = Convert.ToDouble(Console.ReadLine());
            area2 = width2 * length2;

            if (area1 > area2)
            {
                Console.WriteLine("Your first rectangle has a greater area, that area being " + area1);
            }

            if (area1 < area2)
            {
                Console.WriteLine("Your second rectangle has a greater area, that area being " + area2);
            }

            if (area1 == area2)
            {
                Console.WriteLine("Your rectangles have the same area, that area being " + area1);
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者carsonSgit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 16:15:02