简体   繁体   English

如何在并行数组 C# 的 bool 方法中返回 true 或 false 消息?

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

Note: I am learning to use the arrays.注:我正在学习使用arrays。

The issue with my bool method for the parallel array is that I want to return true or false with a message for balance being empty (equal to 0.00 in double data type) or not (return false).我的并行数组的 bool 方法的问题是我想返回 true 或 false,并显示余额为空(等于 double 数据类型中的 0.00)或不为空(返回 false)的消息。 I got the result after running this code:运行这段代码后我得到了结果:

  • The balance is not empty False余额不为空 False
  • The balance is not empty False余额不为空 False
  • The balance is not empty False余额不为空 False

In short, once I run those codes then it will be looping or will not read whether the user's balance is empty or not because it doesn't check if it is zero or less than a zero.简而言之,一旦我运行这些代码,它就会循环或者不会读取用户的余额是否为空,因为它不会检查它是零还是小于零。 Instead, they simply ignore it and then return a false.相反,他们只是忽略它然后返回一个错误。

Here is my code:这是我的代码:

// 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;
}

Therefore, I need to simply check and then return false or true after deciding if the balance is empty or not.因此,我需要简单地检查并在确定余额是否为空后返回 false 或 true。 There is no error but simply shows some repeating and ignoring any that is return false.没有错误,只是显示一些重复并忽略返回 false 的任何错误。 Instead, they will always return true no matter what it is.相反,无论它是什么,它们都将始终返回 true。

It seems to me, if you are calling IsEmpty with one double , you simply need this:在我看来,如果你用一个double调用IsEmpty ,你只需要这个:

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

If you are calling it with an array:如果您使用数组调用它:

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

The params keyword allows you to pass the array as separate variables, like IsEmpty(balance1, balance2, balance3) . params关键字允许您将数组作为单独的变量传递,例如IsEmpty(balance1, balance2, balance3)

Please note that I changed the name of the method from isEmpty to IsEmpty to follow standard C# naming conventions.请注意,我将方法的名称从isEmpty更改为IsEmpty以遵循标准的 C# 命名约定。


You're also better off creating one array and using a custom type:您还最好创建一个数组并使用自定义类型:

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