【发布时间】: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#