【问题标题】:C# false input returned after second try第二次尝试后返回 C# 错误输入
【发布时间】:2021-12-25 11:00:28
【问题描述】:

您好,我是 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;  

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

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

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

【问题讨论】:

    标签: c# c#-3.0


    【解决方案1】:

    您正在使用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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 2023-01-30
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      相关资源
      最近更新 更多