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

为何我的代码无输出?基于switch语句实现两数加减遇问题

Fixing Switch-Based Addition/Subtraction in C#

Hey there! Let's get that switch-statement-driven calculator working properly. Since you're focused on sticking with switch for now (and not chasing fancier methods), let's break down a working implementation and cover the common pitfalls that might have broken your original code.

Working Example Code

Here's a complete, runnable version that handles input validation, switch logic, and proper result output:

using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get first number with validation
            Console.WriteLine("Enter the first number:");
            if (!double.TryParse(Console.ReadLine(), out double num1))
            {
                Console.WriteLine("That's not a valid number!");
                return;
            }

            // Get operator input
            Console.WriteLine("Enter operator (+ or -):");
            string op = Console.ReadLine()?.Trim();

            // Get second number with validation
            Console.WriteLine("Enter the second number:");
            if (!double.TryParse(Console.ReadLine(), out double num2))
            {
                Console.WriteLine("That's not a valid number!");
                return;
            }

            double result = 0;
            bool isOperatorValid = true;

            // Switch logic for operations
            switch (op)
            {
                case "+":
                    result = num1 + num2;
                    break;
                case "-":
                    result = num1 - num2;
                    break;
                default:
                    Console.WriteLine("Unsupported operator! Please use + or -.");
                    isOperatorValid = false;
                    break;
            }

            // Output result only if operator was valid
            if (isOperatorValid)
            {
                Console.WriteLine($"Result: {num1} {op} {num2} = {result}");
            }
        }
    }
}

Common Issues That Might Have Broken Your Original Code

Let's go over the most likely reasons your code wasn't outputting results correctly:

  • Missing input validation: If your original code didn't use TryParse, entering non-numeric values would crash the program instead of showing an error.
  • Forgotten break statements: Without break after each case, the switch would "fall through" to subsequent cases, leading to incorrect result calculations.
  • No default case: If the user entered an operator other than + or -, your code might have skipped all cases entirely, leaving the result variable uninitialized or set to a default value (like 0) without any feedback.
  • Misplaced result output: If you tried to output the result outside the switch without checking if the operator was valid, you might have printed an incorrect default value instead of handling the invalid input gracefully.

This implementation addresses all these points while sticking strictly to switch-based logic, which aligns with your current goal.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:25:44