簡體   English   中英

如何在並行數組 C# 的 bool 方法中返回 true 或 false 消息?

[英]How to return true or false with message inside the bool method for parallel array C#?

注:我正在學習使用arrays。

我的並行數組的 bool 方法的問題是我想返回 true 或 false,並顯示余額為空(等於 double 數據類型中的 0.00)或不為空(返回 false)的消息。 運行這段代碼后我得到了結果:

  • 余額不為空 False
  • 余額不為空 False
  • 余額不為空 False

簡而言之,一旦我運行這些代碼,它就會循環或者不會讀取用戶的余額是否為空,因為它不會檢查它是零還是小於零。 相反,他們只是忽略它然后返回一個錯誤。

這是我的代碼:

// declaring three parallel arrays that store name, account number, and balance with data types
string [] customerName = new string [2];
int [] bankAccount = new int [2];
double [] balance = new double [2];

customerName = new string [] { "John Doe", "Jane Doe", "John Smith" };
bankAccount = new int [] { 123456 , 654321 , 987654 };
balance = new double [] {100.00, 200.00, 300.00};

for ( int i = 0; i < customerName.Length; i++)
{
    Console.WriteLine(customerName[i] + "\t" + "Account Number: " + bankAccount[i] + "\t" + "Balance: $" + balance[i]);
}

// declared three different balances for the deposit method since context is necessary for local variables
double balance1 = balance[0];
double balance2 = balance[1];
double balance3 = balance[2];

Console.WriteLine(isEmpty(balance1));
Console.WriteLine(isEmpty(balance2));
Console.WriteLine(isEmpty(balance3));

 bool isEmpty (double balance)
{
    bool balance1 = balance == 0;
    bool balance2 = balance == 0;
    bool balance3 = balance == 0;
    if (balance == 0)
    {
        return true;
    }
    else
    {
        Console.WriteLine("The balance is not empty");
    }
    return balance1 || balance2 || balance3;
}
// hardcoded values for three customers' balances to be updated
double deposit = 200.00;
double deposit1 = 300.00;
double deposit2 = 400.00;

// declared three different customers' names for return with string data type
string firstCustomer = "John Doe";
string secondCustomer = "Jane Doe";
string thirdCustomer = "John Smith";

Console.WriteLine(Deposit(firstCustomer, balance1, deposit));
Console.WriteLine(Deposit(secondCustomer, balance2, deposit1));
Console.WriteLine(Deposit(thirdCustomer, balance3, deposit2));

// string return that will return the three customers with updated balance after the deposit which is string
string Deposit (string customerName, double balance, double deposit)
{
    double newBalance = balance + deposit;
    return customerName + " deposited $" + deposit + " and the new balance is " + newBalance;
}

因此,我需要簡單地檢查並在確定余額是否為空后返回 false 或 true。 沒有錯誤,只是顯示一些重復並忽略返回 false 的任何錯誤。 相反,無論它是什么,它們都將始終返回 true。

在我看來,如果你用一個double調用IsEmpty ,你只需要這個:

bool IsEmpty(double balance)
{
    if (balance == 0)
    {
        return true;
    }
    else
    {
        Console.WriteLine("The balance is not empty");
        return false;
    }
}

如果您使用數組調用它:

bool IsEmpty(params double[] balances)
{
    if (balances.Any(b => b == 0))
    {
        return true;
    }
    else
    {
        Console.WriteLine("No balance is empty");
        return false;
    }
}

params關鍵字允許您將數組作為單獨的變量傳遞,例如IsEmpty(balance1, balance2, balance3)

請注意,我將方法的名稱從isEmpty更改為IsEmpty以遵循標准的 C# 命名約定。


您還最好創建一個數組並使用自定義類型:

Account[] accounts = new Account[]
{
    new Account() { Customer = "John Doe", Number = 123456, Balance = 100m, },
    new Account() { Customer = "Jane Doe", Number = 654321, Balance = 200m, },
    new Account() { Customer = "John Smith", Number = 987654, Balance = 300m, },
};

public class Account
{
    public string Customer { get; set; }
    public int Number { get; set; }
    public decimal Balance { get; set; }
}

暫無
暫無

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

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