24661人加入学习
(72人评价)
C#编程-第一季-编程基础-宇宙最简单2021最新版

制作完成于2021年10月10日,使用Visual Studio 2019

价格 免费
using System;

namespace _015_排序_编程题
{
    class Program
    {
        static void Main(string[] args)
        {
            //int a = Convert.ToInt32(Console.ReadLine());
            //int b = Convert.ToInt32(Console.ReadLine());
            //int c = Convert.ToInt32(Console.ReadLine());

            //// a b    b c    a b
            //if (a > b)
            //{
            //    int temp = a;
            //    a = b;
            //    b = temp;
            //}
            //if (b > c)
            //{
            //    int temp = b;
            //    b = c;
            //    c = temp;
            //}
            //if (a > b)
            //{
            //    int temp = a;
            //    a = b;
            //    b = temp;
            //}
            //Console.WriteLine(a + " " + b + " " + c);


            double m = Convert.ToDouble(Console.ReadLine());
            int k = Convert.ToInt32(Console.ReadLine());
            if (k == 0)
            {
                int temp = (int)m;
                Console.WriteLine(temp);
            }
            else
            {
                // 3.2343 +0.05 = 3.3043 *10 = 33.043 /10 = 3.3
                double temp = ((int)((m + 0.05) * 10)) / 10.0;
                Console.WriteLine(temp);
            }
        }
    }
}

 

[展开全文]

如何去除小数部分,用强塞的办法

int temp = (int)m

想要得到小数,需要用小数运算

 

[展开全文]

double t=((int)((m+0,05)*10))/10.0;

[展开全文]

 double m = Convert.ToDouble(Console.ReadLine());
            int k = Convert.ToInt32(Console.ReadLine());
            if (k == 0)
            {
                int a = (int)m;
                Console.WriteLine(a);

            }
            else
            {
                int b = (int)(m * 100);
                if(b%10>=5)
                {
                    int c = (b / 10 + 1);
                    double d = (double)c;
                    Console.WriteLine(d/10);
                }
                else
                {
                    int c = (b / 10);
                    double d = (double)c;
                    Console.WriteLine(d/10);
                }
                
            }

[展开全文]

整数转变成小数需要小数点,不然转变过后还是整数

[展开全文]

double x=Convert.ToDouble(Console.ReadLine());

取小数。

四舍五入的思路,以及小数变整数,整数变小数

[展开全文]
bininb · 2022-11-06 · 041-编程题 0

  double temp;
    temp =((int)((m+0.05)*10))/10.0;
    Console.WriteLine (temp);

[展开全文]

(int)x     把X强塞为整数

(char)x    把X强塞为字符串

括号里字符类型是强塞

[展开全文]
潇枭 · 2022-08-13 · 041-编程题 0

数字不用计算求和的输出,用+双引号连接前后

[展开全文]
Kismy · 2022-05-31 · 041-编程题 0

我原本用的:

int temp = (int)((m + 0.05) * 10); double temp2=(double)temp/10;

为了少个变量,后来我直接把(int)((m+0.05)*10)替换掉temp了,改成了:

double temp = (double)((int)((m + 0.05) * 10)) / 10;

这样就不用temp2了。

[展开全文]

小数强赛到整数容器,那么该数的小数部分会丢失

四舍五入可以通过加0.05 

[展开全文]

第四十二课  编程题

1. 输入三个整数,按照从小到大顺序输出。

int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
            int c = Convert.ToInt32(Console.ReadLine());
            if(a !=b && b!=c && a!= c) { 
            if (a > b)
            {
                int temp = a;
                a = b;
                b = temp;
            }
            if (b > c)
            {
                int temp = b;
                b = c;
                c = temp;
            }
            if (a > b)
            {
                int temp = a;
                a = b;
                b = temp;
            }
            Console.WriteLine(a + " " + b + " " + c);
            }

解析:总if语句中表示三个数不能相等。

 例如,我们给出个字母和数字对应的表格。

        a           b           c

        4           3           1

第一个if语句以后变为

        a           b           c

        3           4           1

  第二个if语句以后变为     

        a           b           c

        3           1           4

第三个if语句以后变为      

        a           b           c

        1          3            4

比较以后,将大的数字还给后面的字母容器承装。

2. 输入一个小数m和整数k(k为0或1);

如果k为0,则输出m保留整数部分;

如果k为1,则输出m,四舍五入保留1位小数。

Console.WriteLine("先输入一位小数");
            double m = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("再输入0或1");
            int k = Convert.ToInt32(Console.ReadLine());
            if(k == 0)
            {
                int temp = (int)m;
                Console.WriteLine(temp);
            }else if(k == 1)
            {
                double sep = ((int)((m + 0.05) * 10))/10.0;
                Console.WriteLine(sep);
            }

注:

比如输入的小数为:3.4567

①取小数部分方法:

取一位小数时,需要百分位上的数四舍五入,那么让这个小数+0.05,如果百分位上的数不大于5,那么+0.05也不能进一位,所以相当于被四舍;百分位上的数大于5,那么+0.05就会被进上去一位,相当于被五入了。

②在除以一个数,并且想得到一位小数部分,那么就要写/10.0,将除数带上一位小数,这样结果才会带上一位小数。(别像我傻傻地写了个10上去给自己弄的怀疑人生了,看了老师继续讲才知道的。)

 

 

 

 

 

 

[展开全文]

授课教师

SiKi学院老师

课程特色

下载资料(1)
视频(118)
图文(1)