【问题标题】:How to take 0 decimal place "double" data type and output corresponding number of "*" automatically [duplicate]如何取0小数位“double”数据类型并自动输出对应的“*”个数[重复]
【发布时间】:2016-11-21 01:35:54
【问题描述】:

我已经编写了我的第一个 c# 文件。基本上,我所拥有的是有人对产品的口味、质地、外观等方面给出不同的评分(满分 10 分)(作为双打)。我创建了一个称重系统,并自动对整体评论进行评分,满分 10 分。 我使用Math.Round 将其四舍五入到最接近的整数。

我想将以下行显示为以下输出:

“总体评分为 5/10 : *****”

其中显示的“*”个数会自动显示为计算的总评分(作为一个整数)。

我不知道怎么做这最后一点。

到目前为止,我的最终输出是:

Console.WriteLine("Overall rating is " + ratingRounded + "/10: ");

如何将“*”显示为与ratingRounded 相同?

抱歉,我可能在解释这一切时做得很糟糕(我已经尽力了!)。

提前致谢。

【问题讨论】:

    标签: c#


    【解决方案1】:

    您可以使用(如果 ratingRounded 是数字数据类型而不是字符串)

    Console.WriteLine("Overall rating is " + ratingRounded + "/10: " + new String('*', ratingRounded));
    

    【讨论】:

      【解决方案2】:

      这个很短……

      string stars = new String('*', (int)ratingRounded);
      Console.WriteLine("Overall rating is " + ratingRounded + "/10: "+stars);
      

      【讨论】:

        【解决方案3】:

        你可以使用循环。

        Console.Write("Overall rating is " + ratingRounded + "/10: ");
        for(int i=0; i<ratingRounded; i++){
            Console.Write("*");
        }
        Console.WriteLine();
        

        【讨论】:

        • 感谢这工作完美。我从来没有想过在这样的 for 循环中编写代码。很简单。我也试过“Enumerable.Repeat”。你的方法很棒!
        • 如果它对您有用,您能否为答案投票? :)
        • @TajkiaRahmanToma 直到15 rep...
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-20
        • 1970-01-01
        • 2021-08-03
        • 2016-10-04
        • 1970-01-01
        • 2020-03-27
        • 1970-01-01
        相关资源
        最近更新 更多