【问题标题】:How to properly manipulate a list?如何正确操作列表?
【发布时间】:2020-05-16 21:52:07
【问题描述】:

大家下午好,

逻辑菜鸟在这里再次提出另一个问题。我错误地使用了我的逻辑来处理以下列表中的数据。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

public static List<string> itemDescription = new List<string>();
    public static List<double> itemPrice = new List<double>();
    public static List<int> itemQoh = new List<int>();

    public static List<string> salesCustomerName = new List<string>();
    public static List<string> salesItemDescription = new List<string>();
    public static List<double> salesItemPrice = new List<double>();
    public static List<int> salesQuantity = new List<int>();

考虑到这个逻辑,我使用这两种方法:

GetPrice()

  • 提示用户输入价格
  • 验证值是否为双精度且大于 0
  • 返回输入的价格 我的问题是理解 tryparse 方法
public static double GetPrice() {
      // prompt the user to enter a price
      Console.WriteLine("\nEnter a new price: ");
      // validate value is double and greater than 0
      double price;
      string newPrice = Console.ReadLine();
      bool isDouble = Double.TryParse(newPrice, out price);
      if(isDouble | price > 0) {
        Console.WriteLine("Double data type has been detected.");
      }
      // return the price entered
      itemPrice.Add(price);
      return price;
    }

获取数量 - 提示用户输入价格 - 验证值是否为 int 且大于 0 - 返回输入的价格

public static int GetQuantity() {
      // prompt the user to enter a quantity
       Console.WriteLine("\nEnter a quantity: ");
      // validate value is integer and greater than or equal to 0
      int quantity;
      string newQuantity = Console.ReadLine();
      bool isInt = Int32.TryParse(newQuantity, out quantity);
      if(isInt | quantity > 0) {
        Console.WriteLine("Int data type has been detected.");
      }
      // return the quantity entered
      itemQoh.Add(quantity);
      return quantity;
    }

我使用正确吗?什么都有帮助!谢谢!

【问题讨论】:

    标签: c#


    【解决方案1】:

    如果我理解正确的话,

    if(isInt | quantity > 0) {
        Console.WriteLine("Double data type has been detected.");
      }
    

    if(isDouble | price > 0) {
        Console.WriteLine("Double data type has been detected.");
      }
    

    应仅在 both 价格/数量大于 0 且输入值为双精度/整数时运行。

    您正在使用按位或运算符;对于布尔条件,您将运算符加倍(所以 OR 是 || 并且 AND 是 &&)。

    有了这个,既然你似乎对 AND 感兴趣,它应该是(对于双重情况​​,留 int 作为你的练习):

      if(isDouble && price > 0) {
        Console.WriteLine("Double data type has been detected and value greater than zero.");
      }
    

    【讨论】:

    • 感谢您的反馈!我没有注意到我也两次要求双倍,抱歉造成混乱。一个是验证它是一个 int,另一个是验证它是一个 double。那么我的tryparse是否正确完成?如果我正确理解这一点。我将使用 AND,那是我的错误。
    • 是的,tryparse 似乎是正确的——切换到 AND 应该会为您解决它!
    【解决方案2】:

    我相信你的意思是做一个逻辑或(||)而不是按位

      if(isDouble || price > 0) {
        Console.WriteLine("Double data type has been detected.");
      }
    

    但你说验证值是双倍且大于 0,因此我会将其设为&amp;&amp; AND 条件

      if(isDouble && price > 0) {
        Console.WriteLine("Double data type has been detected.");
      }
    

    【讨论】:

    • 谢谢!是的,我需要 AND 条件!啊!
    猜你喜欢
    • 1970-01-01
    • 2012-03-17
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多