一包茄子酱 2022-04-14 14:57 采纳率: 100%
浏览 40
已结题

C#判断语句中数据类型

在用C#学习写一个最简单的图书借阅管理系统,想要实现输入一本书的数量后,如果数量大于0,“借阅状态”能判断为“可借阅”,反之则不可借阅,但是Qty用String类型无法用>来判断,改成int也报错,求问怎么改

public class Book
{
    public string BookName { get; set; }
    public string BookAuthor { get; set; }
    public string BookQty { get; set; }
    public string BookLoc { get; set; }
    public string BookStatus { get; set; }

}
public class Library
{
    List<Book> Books = new List<Book>();
    List<Book> books = new List<Book>();
    
    #region 录入
    public void exe0()
    {

        int i = 1;
        string str = "";
        while (str != "ok")
        {
            Console.WriteLine("请输入第{0}本书的书名:", i);
            string name = Console.ReadLine();
            Console.WriteLine("请输入第{0}本书的作者:", i);
            string author = Console.ReadLine();
            Console.WriteLine("请输入第{0}本书的数量:", i);
            string qty = Console.ReadLine();
            Console.WriteLine("请输入第{0}本书的位置:", i);
            string loc = Console.ReadLine();
            if (qty > 0)
            {
                string status = "可借阅";
            }
            else 
            {
                string status = "不可借阅";
            }

            Books.Add(new Book { BookName = name, BookAuthor = author, BookQty = qty, BookLoc = loc, BookStatus = status});
            i = i + 1;
            Console.WriteLine("输入 ok 停止录入,按任意键继续");
            str = Console.ReadLine();

        }
        Console.WriteLine("");
        Console.WriteLine("");
        Console.WriteLine("书名\t作者\t数量\t位置\t借阅状态");
        foreach (var item in Books)
        {
            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", item.BookName, item.BookAuthor, item.BookQty, item.BookLoc, item.BookStatus);
        }

    }

运行报错
Operator '>' cannot be applied to operands of type 'string' and 'int'
The name 'status' does not exist in the current context

  • 写回答

2条回答 默认 最新

  • 依然如昨 2022-04-14 15:12
    关注

    直接转int型啊,int qty= int.Parse(console.ReadLine());//当然最好加上try catch 处理输入非数字的异常

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月26日
  • 已采纳回答 4月18日
  • 创建了问题 4月14日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度