如何在控制台应用程序C#中增加数字的百分比?

时间:2018-12-04 15:39:58

标签: c#

如果整数“ a”的百分比小于100,我需要将其百分比提高20%,以此类推。我从未使用过百分比,因此我需要知道它在c#中的工作方式。这只是一个简单的代码示例。

static void Main(string[] args)
    {
        int a, b, c;
        a = Convert.ToInt32(Console.ReadLine());

        if (a < 100)
        {
            b = a * 20 %;
            Console.WriteLine(b);
        }
        else if (a > 200)
        {
            b = a * 30 %;
            Console.WriteLine(b);
        }
        else if (a == 300)
        {
            b = a * 40 %;
            Console.WriteLine(b);
        }
        else Console.WriteLine(a);

3 个答案:

答案 0 :(得分:2)

如果我理解,我认为您需要这个:

int a, c;
double b;
a = Convert.ToInt32(Console.ReadLine());

if (a < 100)
{
    b = a + a * 0.2;
    Console.WriteLine(b);
}
else if (a > 200)
{
    b = a + a * 0.3;
    Console.WriteLine(b);
}
else if (a == 300)
{
    b = a + a * 0.4;
    Console.WriteLine(b);
}
else 
    Console.WriteLine(a);

答案 1 :(得分:0)

如果您使用double而不是%,那将是一个更好的选择,正如我现在将在下面演示的那样。

int a, c;
double b;
a = Convert.ToInt32(Console.ReadLine());

if(a < 100)
{
   b = a * 1.2;
   Console.WriteLine(b);
}

答案 2 :(得分:0)

我认为他需要将其打印为字符串并将其附加百分号。在每种现代语言中,“%”都是余数运算符,它没有按照您认为的那样执行。在这种情况下,请使用以下代码:

cast_1_tup = castdf[['cast_0','cast_1']].apply(tuple, axis=1)
cast_2_tup = castdf[['cast_0','cast_2']].apply(tuple, axis=1)
cast_3_tup = castdf[['cast_0','cast_3']].apply(tuple, axis=1)
cast_4_tup = castdf[['cast_0','cast_4']].apply(tuple, axis=1)
cast_5_tup = castdf[['cast_1','cast_2']].apply(tuple, axis=1)
cast_6_tup = castdf[['cast_1','cast_3']].apply(tuple, axis=1)
cast_7_tup = castdf[['cast_1','cast_4']].apply(tuple, axis=1)
cast_8_tup = castdf[['cast_2','cast_3']].apply(tuple, axis=1)
cast_9_tup = castdf[['cast_2','cast_4']].apply(tuple, axis=1)
cast_10_tup = castdf[['cast_3','cast_4']].apply(tuple, axis=1)

G = nx.Graph()
G.add_edges_from(cast_1_tup)
G.add_edges_from(cast_2_tup)
G.add_edges_from(cast_3_tup)
G.add_edges_from(cast_4_tup)
G.add_edges_from(cast_5_tup)
G.add_edges_from(cast_6_tup)
G.add_edges_from(cast_7_tup)
G.add_edges_from(cast_8_tup)
G.add_edges_from(cast_9_tup)
G.add_edges_from(cast_10_tup)

# write in UTF-8 encoding
fh = open('edgelist.utf-8', 'wb')
fh.write('# -*- coding: utf-8 -*-\n'.encode('utf-8'))  # encoding hint for emacs
nx.write_multiline_adjlist(G, fh, delimiter=',', encoding='utf-8')

# read and store in UTF-8
fh = open('edgelist.utf-8', 'rb')
H = nx.read_multiline_adjlist(fh, delimiter=',', encoding='utf-8')

plt.figure(figsize=(40,40))
plt.axis('off')
pos = nx.spring_layout(G, scale =2)
nx.draw_networkx(G, pos, cmap = plt.get_cmap('jet'), node_colour = values , node_size=80, with_labels=False)
for p in pos:  # raise text positions
    pos[p][1] += 0.04
nx.draw_networkx_labels(G, pos)
plt.show()