簡體   English   中英

C#僅使用If / else的5個數字最大

[英]C# biggest of 5 numbers only with If/else

在進行一些C#練習時,此練習卡住了。

//僅使用五個 if語句查找五個數字中的最大值。

我不知道該怎么做,所以我上網查找並進行了設置。

    Console.Write("Type number 1:");    double a = double.Parse(Console.ReadLine());
    Console.Write("Type number 2:");    double b = double.Parse(Console.ReadLine());
    Console.Write("Type number 3:");    double c = double.Parse(Console.ReadLine());
    Console.Write("Type number 4:");    double d = double.Parse(Console.ReadLine());
    Console.Write("Type number 5:");    double e = double.Parse(Console.ReadLine());

    double max;
    if (a > b && a > c && a > d && a > e)
    {
        max = a;
        Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
    }
    else if (b > a && b > c && b > d && b > e)
    {
        max = b;
        Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
    }
    else if (c > a && c > b && c > d && c > e)
    {
        max = c;
        Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
    }
    else if (d > a && d > b && d > c && d > e)
    {
        max = d;
        Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
    }
    else
    {
        max = e;
        Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
    }

使用練習示例,它返回正確的數據。 例如

5   2   2   4   1   return: 5

但是經過進一步測試,我最終發現這是錯誤的:

-1  -2  -2  -1  -15 return: -15

我的一年級數學技能是錯誤的,還是-15不大於-1。 我知道錯誤在哪里,但我不知道如何解決。

即使只有4個if,也相當容易。

double max = a;
if (b > max) max = b;
if (c > max) max = c;
if (d > max) max = d;
if (e > max) max = e;

Console.WriteLine("max is " + max);

您還應該處理相等的情況,否則沒有'if'語句將匹配並且您最終到達設置了max=e的錯誤的'else'塊。 做:

              if (a >= b && a >= c && a >= d && a >= e)
        {
            max = a;

        }
        else if (b >= a && b >= c && b >= d && b >= e)
        {
            max = b;

        }
        else if (c >= a && c >= b && c >= d && c >= e)
        {
            max = c;

        }
        else if (d >= a && d >= b && d >= c && d >= e)
        {
            max = d;

        }
        else
        {
            max = e;

        }
        Console.WriteLine(max);

首先,有可能作弊 ,即

  if (a > b)
    max = a;
  else
    max = b;

等於( 三元運算符

  max = a > b ? a : b;  

while循環仿真

  max = b;

  while (a > b) {
    max = a;

    break;
  }

可能的誠實解決方案很簡單,並且僅if s時要求4:

  ...
  Double max = a;

  if (max < b)
    max = b;

  if (max < c)
    max = c;

  if (max < d)
    max = d;

  if (max < e)
    max = e;

  Console.WriteLine("The biggest number from {0}, {1}, {2}, {3}, {4} is {5}.", a, b, c, d, e, max);

我找到了一個更聰明的解決方案

    Console.Write("Type number 1:");    float a = float.Parse(Console.ReadLine());
    Console.Write("Type number 2:");    float b = float.Parse(Console.ReadLine());
    Console.Write("Type number 3:");    float c = float.Parse(Console.ReadLine());
    Console.Write("Type number 4:");    float d = float.Parse(Console.ReadLine());
    Console.Write("Type number 5:");    float e = float.Parse(Console.ReadLine());
    float max = a;

    if (a < b)
    {
        max = b;
    }
    if (max < c)
    {
        max = c;
    }
    if (max < d)
    {
        max = d;
    }
    if (max < e)
    {
        max = e;
    }
    Console.WriteLine(max);

工作正常。

我知道這是一個舊帖子,但我只想向這些內容添加自己的解決方案。

int large = 0;
        for (int i = 0; i < 5; i++)
        {
            Console.Write("Enter interger :");
            int num = int.Parse(Console.ReadLine());
            if (num > large)
            {
                large = num;
            }

        }
        Console.WriteLine(large);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM