【问题标题】:C# Console Application that Outputs a category based on input基于输入输出类别的 C# 控制台应用程序
【发布时间】:2021-02-14 07:52:33
【问题描述】:

我的任务是开发一个收集员工信息的 Windows 应用程序: 名称和已售商品数量作为输入并输出信息摘要,以根据售出商品数量(低于 50 件商品, 50-99 项、100-199 项和 199 项以上的级别)见下文。你能帮我处理这段代码吗?以下是目前正在使用的代码:

using System;

namespace L0002B
{
    public class Salesperson
    {
        string Name { get; set; }
        int Quantity_Sold { get; set; }
        static void Main(string[] args)
        {
            System.Collections.Generic.List<Salesperson> Salesperson = new System.Collections.Generic.List<Salesperson>()
        {
             new Salesperson {Name="Donald Duck ",  Quantity_Sold=173},
             new Salesperson {Name="Mickey Mouse ",  Quantity_Sold=202},
             new Salesperson {Name="Snoopy  ",  Quantity_Sold=203},
        };
      
        }
        private static void Bonus(ref int Quantity_Sold)
        {
            if (Quantity_Sold >= 200)
            {
                Console.WriteLine("Above 199 items");
            }
            else if (Quantity_Sold >= 100)
            {
                Console.WriteLine("100-199 items");
            }
            else if (Quantity_Sold >= 50)
            {
                Console.WriteLine("50-99 items");
            }
            else
            {
                Console.WriteLine("Below 50 items");
            }
            Console.WriteLine("Bonus Report: " Bonus);
        }
    }
}

我想要一个提供类似这样的报告的输出,但我得到一个空白输出。 enter image description here

【问题讨论】:

  • 您遇到了什么问题?
  • 那么如果值是 120 呢?大于 50 触发分支,忽略其他分支
  • 是的@HansKesting
  • 您的问题仍然没有说明您面临的问题。 if 和 else 的逻辑可以通过 ZoharPeled 发布的答案来修复。您还面临什么其他问题?
  • 我已经添加了我遇到的问题的错误消息。主要是系统不识别List Salesperson

标签: c# list if-statement arraylist


【解决方案1】:

你的 if 条件有两个问题

  1. 你有两个条件

    if (Quantity_Sold < 50)
       ...
    else if (Quantity_Sold > 50)
       ...
    

    如果Quantity_Sold == 50,任何分支都不会被击中

    因此,您必须将&lt;= 与这些条件之一一起使用,或者创建一个额外的

    else if (Quatity_Sold == 50)
    
  1. 如果您使用if .. else if 将多个阈值与&gt; 进行比较,则必须从最大值开始。如果您要与&lt; 进行比较,您必须从最小值开始。不要在同一个 if .. else if .. else

    中混用这些比较器
    • 你的方式:(假设Quantity_Sold == 123
    if (Quantity_Sold > 50) 
       ...  //this will be hit because 123 > 50
    else if (Quantity_Sold > 100)
       ...  //this won't be hit anymore because first condition already is hit
    
    • 正确方法:
    if (Quantity_Sold >= 100) 
       ...  // 100 .. maxint
    else if (Quantity_Sold >= 50)
       ...  // 50 .. 99
    else 
       ...  // 0 .. 49
    

    这也将解决第一种情况的问题(即Quantity_Sold == 50时会发生什么)

【讨论】:

  • 我已经应用了这个更正,我的输出仍然是空白
  • @Hass Bonus() 是您的代码中创建任何输出的唯一方法。但我没有看到你在任何地方调用它......
  • 我已经编辑了问题中的代码,但后来我得到了这个错误,我称之为。 "语法错误,',' 应为 (CS1003) (L0002B)"
  • 您希望Console.WriteLine("Bonus Report: " Bonus); 做什么?首先,方法调用中的两个参数必须用,隔开。其次,您必须从Main 方法调用Bonus 方法,最好是为每个销售人员调用。 TBH:如果您缺乏基本的编程技能(例如,如何调用方法),我不确定是否可以帮助您。您可能应该先学习一些基本的编程教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2023-04-10
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多