簡體   English   中英

C#練習,輸出不正確

[英]C# exercise, output is not right

練習的目的是檢查一個點是否在2D矩形的邊上。 我得到了一些正確的輸出。 但是我不知道為什么最終的“ else”子句在打印時似乎會被忽略。

源代碼:

using System;

class pointOnBorder
{
    static void Main()
    {
        var x1 = decimal.Parse(Console.ReadLine());
        var y1 = decimal.Parse(Console.ReadLine());

        var x2 = decimal.Parse(Console.ReadLine());
        var y2 = decimal.Parse(Console.ReadLine());

        var x = decimal.Parse(Console.ReadLine());
        var y = decimal.Parse(Console.ReadLine());

        if (x1 < x2 && y1 < y2)
        {
            if (x == x1 || x == x2) 
            {
                if (y >= y1 && y <= y2)
                {
                    Console.WriteLine("Border");
                }
            }
            else if (y == y1 || y == y2)
            {
                if (x >= x1 && x <= x2)
                {
                    Console.WriteLine("Border"        );
                }
            }
            else
            {
                Console.Write("Inside  / Outside");
            }
        }
    }
}

注意:“邊界”輸出沒有問題。

我想打印“內部/外部”,但它似乎沒有解決(我得到空的輸出)。 我曾在其他地方問過,有人建議我的“ else”可能在第一個“ if”下,因為可以看到情況並非如此,因此我看不出我最后一個“ else”子句的原因被忽略。

提前致謝! :)

如果要涵蓋所有可能的情況,則每個if都應具有其對應的else if 5的if我只看到3個else的。

您可以通過關閉的支架嘗試if上述else if關鍵字或代替else ifelse

您的代碼過於復雜。

我將其簡化為:

// this is a conditional assignment -
// if the condition is true, pointLocation would be assigned with "Border".
// if it's false, it will be assigned with "Inside  / Outside". 
// It's a short way to write if...else statements - condition ? true : false ;
var pointLocation = (
   (IsValueEqualToStartOrToStop(x1, x2, x) && IsValueBetweenStartAndStop(y1, y2, y)) || 
   (IsValueEqualToStartOrToStop(y1, y2, y) && IsValueBetweenStartAndStop(x1, x2, x))) ? "Border" : "Inside  / Outside";

    Console.WriteLine(pointLocation);

// this method checks that the value is equal to start or to stop. 
bool IsValueEqualToStartOrToStop(decimal start, decimal stop, decimal value)
{
    return value == start || value == stop;
}

// this method checks if the value is between start and stop
bool IsValueBetweenStartAndStop(decimal start, decimal  stop, decimal  value)
{
    return (start < stop && start <= value && value <= stop) || 
           (start > stop && start >= value && value >= stop); value 
}

我要強調的是,這不是我編寫此功能的方式。 我同意您的代碼過於復雜,但是了解到您可能希望修復代碼,以便看到問題所在,而不是完全重寫,這是修復的代碼:

class pointOnBorder{
   static void Main(){
      var x1 = decimal.Parse(Console.ReadLine());
      var y1 = decimal.Parse(Console.ReadLine());

      var x2 = decimal.Parse(Console.ReadLine());
      var y2 = decimal.Parse(Console.ReadLine());

      var x = decimal.Parse(Console.ReadLine());
      var y = decimal.Parse(Console.ReadLine());

      if (x1 < x2 && y1 < y2)
      {
         if (x == x1 || x == x2)
         {
            if (y >= y1 && y <= y2)
            {
               Console.WriteLine("Border");
            }
            else
            {
               Console.Write("Inside  / Outside");
            }
         }
         else if (y == y1 || y == y2)
         {
            if (x >= x1 && x <= x2)
            {
               Console.WriteLine("Border");
            }
            else
            {
               Console.Write("Inside  / Outside");
            }
         }
         else
         {
            Console.Write("Inside  / Outside");
         }
      }
      else{
         Console.Write("Bad rectangle specification.");
      }
   }
}

暫無
暫無

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

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