簡體   English   中英

如果用戶沒有輸入局部變量,我如何使用其他變量定義它? C#

[英]If an user does not input a local variable how do i define it using other variables? C#

讓我解釋。 我是一名高中生,在 C# 中沒有很多經驗編程,我們有一個任務來制作幾何計算器,我得到了一個三角金字塔,但這不是重點。 計算器應該從用戶那里得到一個輸入,然后用給定的數據計算表面和體積。

               double a = Convert.ToDouble(Console.ReadLine());
               double h = Convert.ToDouble(Console.ReadLine());
               double H = Convert.ToDouble(Console.ReadLine());
               double area = Math.Pow(a, 2) * Math.Sqrt(3) / 4 + 3 * a * h / 2;
                double volume = Math.Pow(a, 2) * Math.Sqrt(3) * H / 12;
                Console.WriteLine(volume);
               Console.WriteLine(area);
               Console.ReadLine();

現在這對我來說很容易,但是當用戶不知道其中一個變量的值時會出現問題,例如高。在這種情況下,計算器應該使用其他兩個局部變量來計算它。

double h = Convert.ToDouble(Console.ReadLine());
double h = Math.Sqrt(Math.Pow(a * Math.Sqrt(3) / 3, 2) + Math.Pow(H));

我知道我知道你不能這樣做,但我在互聯網上找不到任何東西,所以我請求你的幫助,因為這是我成績的 20%。 如果這無法完成,您還有其他建議嗎?

Ps 對不起,如果我的英語不好。

這很容易實現。 沒有變量意味着您將根據缺失的變量使用不同的公式/計算,您可以使用if條件來執行此操作。

   //Read them in as strings if you want to check if they're "_blank_", convert them 
     later.
        string a = Console.ReadLine();
        string h = Console.ReadLine();
        string H = Console.ReadLine();

        double area = 0;
        double volume = 0;
    
        if(a == "") //If it's blank, no entry do this code. 
        {
                  //This is how I'd convert it, just a little less pretty for the sake
                  //of understanding for you. You'd need to do this in every if block.
                  double returnedDoubleh = ConvertToDouble(h);
                  double returnedDoubleH = ConvertToDouble(H);
                  //Have your formula if `a` is blank.
        }
        else if (h == "")
        {
                  double returnedDoubleA = ConvertToDouble(a);
                  double returnedDoubleH = ConvertToDouble(H);
                  //Have your formula if `h` is blank.
        }
        else if (H == "")
        {
                  double returnedDoubleA = ConvertToDouble(a);
                  double returnedDoubleh = ConvertToDouble(h);
                  //Have your formula if `H` is blank.
        }
        else //This is if none are blank OR More than one is blank which would crash if 
                more than one is blank.. 
        {
                        area = Math.Pow(a, 2) * Math.Sqrt(3) / 4 + 3 * a * h / 2;
                        volume = Math.Pow(a, 2) * Math.Sqrt(3) * H / 12;        
        }

                       Console.WriteLine(volume);
                       Console.WriteLine(area);
                       Console.ReadLine();

用於轉換字符串值的示例函數。

public static double ConvertToDouble(string nonConverted)
        {
            double converted;
            while (!double.TryParse(nonConverted, out converted) || String.IsNullOrWhiteSpace(nonConverted))
            {
                Console.Clear();
                Console.WriteLine($"INVALID RESPONSE\n\r" +
                    $"\n\rTry Again");
                nonConverted = Console.ReadLine();
            }

            return converted;
        }

在您的條件中,您還可以使用“變量”,因此您可以執行類似if(a == "x") ,而不是說if(a == "") if(a == "x")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM