簡體   English   中英

如何使方法參數對於用戶輸入是可選的? (C#)

[英]How do I make a method parameter optional with user input? (C#)

我在C#中有一個名為“運算符”的對象,該對象具有一種從用戶獲取兩個數字輸入並將它們加在一起的方法。 但是,我想使第二個參數(第二個輸入)為可選,以便如果用戶未輸入第二個數字,則默認值為“ 4”。

我知道出了點問題,因為如果用戶僅輸入一個數字並在提示您輸入第二個內容時按回車鍵,它將僅結束程序而不使用默認值。

這個解決方案可能非常明顯,但它使我難以理解。 如果有人能看一下我的代碼並看到我所缺少的,我將不勝感激。

非常感謝!

程序代碼:

class Program
{
    static void Main(string[] args)
    {
        Operator operatorObject = new Operator();
        Console.WriteLine("Pick a number:");
        int userValue = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Pick another number--optional");
        int userValue2 = Convert.ToInt32(Console.ReadLine());

        int result = operatorObject.operate(userValue, userValue2);

        Console.WriteLine(result);
        Console.ReadLine();
    }
}

班級代碼:

public class Operator
{
    public int operate(int data, int input=4)
    {
        return data + input;
    }           
}

更新:謝謝大家的回答! 由於有多種建議,我認為我現在可以正常使用了。 非常感謝您的幫助!

如果您省略輸入值,則輸入Console.ReadLine將返回空字符串,該字符串肯定不能轉換為整數。

因此,為了使參數可以省略你需要指出,如果用戶在輸入的所有東西:

int userValue2, userValue2;
int result;
Console.WriteLine("Pick a number:");
if(!int.TryParse(Console.ReadLine(), out userValue))
    throw new ArgumentException("no valid number");

Console.WriteLine("Pick another number--optional");
if(int.TryParse(Console.ReadLine(), out userValue2)
    result = operatorObject.operate(userValue, userValue2);
else
    result = operator.operate(userValue);

int.TryParse嘗試解析用戶提供的輸入,如果解析失敗將返回false 因此,如果用戶鍵入完全不同的內容(例如"MyString"這也將起作用。

問題是您要同時使用兩個參數來調用方法。 您應該檢查是否傳遞第二個參數。 如下所示:

public static void Main()
{
    Operator operatorObject = new Operator();
    Console.WriteLine("Pick a number:");
    int userValue = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Pick another number--optional");

    int userValue2;
    int result;
    if(int.TryParse(Console.ReadLine(), out userValue2))
    {
         result = operatorObject.operate(userValue,userValue2);
    } 
    else 
    {
         result = operatorObject.operate(userValue);
    }

    Console.WriteLine(result);
    Console.ReadLine();
}

就像是:

    static void Main(string[] args)
{
    Operator operatorObject = new Operator();
    Console.WriteLine("Pick a number:");
    var val1 = Console.ReadLine();
    int userValue = 0;
    if (val1 != null && val1.Length > 0)
    {
        userValue = Convert.ToInt32(val1);
    }
    Console.WriteLine("Pick another number--optional");
    var val2 = Console.ReadLine();
    int userValue = 0;
    int userValue2 = 0;
    if (val2 != null && val2.Length > 0)
    {
        userValue2 = Convert.ToInt32(val2);
    }


    int result = operatorObject.operate(userValue, userValue2);

    Console.WriteLine(result);
    Console.ReadLine();
}
public class Operator
{
    public int operate(int data, int input = 4)
    {
        return data + input;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Operator operatorObject = new Operator();
        Console.WriteLine("Pick a number:");
        int userValue = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Pick another number--optional");
        var userValue2IsValid = int.TryParse(Console.ReadLine(), out int userValue2);

        int result = 0;
        if (userValue2IsValid) {
            result = operatorObject.operate(userValue, userValue2);
        }
        else {
            result = operatorObject.operate(userValue);
        }

        Console.WriteLine(result);
        Console.ReadLine();
    }
}

這個怎么樣:

class Program
{
    static void Main(string[] args)
    {
        Operator operatorObject = new Operator();
        Console.WriteLine("Pick a number:");

        int result = 0;

        int userValue;
        if (int.TryParse(Console.ReadLine(), out userValue))
        {
            Console.WriteLine("Pick another number--optional");
            int userValue2;
            if (int.TryParse(Console.ReadLine(), out userValue2))
            {
                result = operatorObject.operate(userValue, userValue2);
            }
            else
            {
                result = operatorObject.operate(userValue);
            }
        }
        else
        {
            Main(null);
        }

        Console.WriteLine(result);
        Console.ReadLine();
    }

  ...

}

暫無
暫無

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

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