第二次尝试后返回 C# 错误输入

C# false input returned after second try

您好,我是 C# 的新手,昨天开始学习它,掌握了一些 Java 和 Python 知识。我试图制作一个计算器,但它没有按预期工作。 代码是:

using System;

namespace LolCS{
    class Program{
        
        static void Main(){
            //variables and objects
            Program proj = new Program();
            double solution = 0;
            string operation;
            double input1 = 0;
            double input2 = 0;

            //take inputs for input 1,2 and which operation to use
            Console.WriteLine("Welcome to this calculator, let's get started\n"+
                              "Please input your operations\n");               

            double.TryParse(proj.get_input("in1"), out input1);
            operation = proj.get_input("op");
            double.TryParse(proj.get_input("in2"), out input2);

            //call function which does the math and assign result to variable solution
            solution = proj.domath(operation, input1, input2);

            //print solution
            Console.WriteLine(input1+operation+input2+" = "+solution);
            if (Console.ReadLine() == "r") { Main(); } 
        }

        //function to do the math according to the given operation
        double domath(string oper, double in1, double in2){
            double solu = 0;
            switch(oper){                                    
                case "+":
                    solu = in1 + in2;
                    break;
                case "-":
                    solu = in1-in2;
                    break;
                case "*":
                    solu = in1 * in2;
                    break;
                case "/":
                    solu = in1 / in2;
                    break;
            }
            return solu;
        }

        //gets values for the variables
        string get_input(string in_type){
            string gon_ret = "";
            switch(in_type){
                case "in1":
                    Console.WriteLine("Input 1: ");
                    gon_ret = Console.ReadLine();
                    break;
                case "in2":
                    Console.WriteLine("Input 2: ");
                    gon_ret = Console.ReadLine();
                    break;
                case "op":
                    Console.WriteLine("Operation: ");
                    gon_ret = Console.ReadLine();       
                    if (!(gon_ret == "+") && !(gon_ret == "-") && !(gon_ret == "*") && !(gon_ret == "/")){
                        Console.WriteLine(gon_ret+" Not Valid, try again");
                        get_input("op");
                    }
                    break;  
            }
            return gon_ret;

        }
    }
}

问题在于这行代码:

case "op":
                    Console.WriteLine("Operation: ");
                    gon_ret = Console.ReadLine();       
                    if (!(gon_ret == "+") && !(gon_ret == "-") && !(gon_ret == "*") && !(gon_ret == "/")){
                        Console.WriteLine(gon_ret+" Not Valid, try again");
                        get_input("op");
                    }
                    break;  

预计将检查操作输入是否不同于 +、-、* 或 /,如果是,则应打印“无效”并重复操作输入。如果第一次输入正确,那将按预期工作,但如果在第二次或以后尝试给出正确的输入,则会返回错误的输入。示例输出:

Welcome to this calculator, let's get started
Please input your operations

Input 1: 
2
Operation: 
p
p Not Valid, try again
Operation: 
+
Input 2: 
3
2p3 = 0

抱歉,如果这是一个愚蠢的错误,但我仍在学习。感谢您的帮助!

您正在使用 get_input("op") 执行递归操作 该函数工作正常,但您没有返回新的 get_input("op") 调用的结果。您正在返回原始调用产生的无效字符串。

            case "op":
                Console.WriteLine("Operation: ");
                gon_ret = Console.ReadLine();       
                if (!(gon_ret == "+") && !(gon_ret == "-") && !(gon_ret == "*") && !(gon_ret == "/")){
                    Console.WriteLine(gon_ret+" Not Valid, try again");
                    //Return here as the current call is invalid
                    return get_input("op");
                }
                break;