【问题标题】:How to print result depends on condition ?如何打印结果取决于条件?
【发布时间】:2016-09-22 08:05:45
【问题描述】:
  1. 我的代码:

        public class Triangle 
       {
          private PointClass v1 = new PointClass();
          private PointClass v2 = new PointClass();
          private PointClass v3 = new PointClass();
    
    
    
          public Triangle (PointClass v1 , PointClass v2 , PointClass v3)
    
        {
             this.v1 = v1;
             this.v2 = v2;
             this.v3 = v3;
        }
    
        public void printtype()
     {
    
           if (v1 != v2 && v2 !=v3 && v1 !=v3)
    
         {
    
           System.out.println(" The Triangle is scalene"); 
    
         }
    
           if (v1 != v2 || v3 !=v2 )
        {
    
           System.out.println(" The Triangle is isosceles"); 
    
        }
    
    
          if (v1 == v2 && v2== v3 && v1 == v3)
    
       {
           System.out.println(" The Triangle is equilateral");
       }
    
    
     }
    
    
    }
    
  2. 主要方法:

      public static void main(String[] args) 
    {
       PointClass v1 = new PointClass(1, 2);
       PointClass v2 = new PointClass(1, 2);
       PointClass v3 = new PointClass(1, 2);
       Triangle tr = new Triangle(v1, v2, v3);
       tr.printtype();
    }
    
  3. 输出是:

    三角形是不等边的

    三角形是等腰三角形

我想打印条件的结果取决于 PointClass 的输入,例如 V1 , V2 和 V3 并且是一个结果而不是其中两个

我该怎么做?谁能帮帮我?

【问题讨论】:

  • v1v2v3真的边长吗?
  • 另外,您应该使用equals 比较PointClass 实例,而不是==
  • 首先,不要使用 == 来比较两个对象,你应该重写 equals 来检查相等性。然后只需对您的条件进行排序(第 3 次然后第 1 次然后第 2 次)并使用 else if 而不是 if
  • -1,通过提供一致的格式并最好遵循 Java 约定,为您发布的代码投入更多精力。

标签: java oop


【解决方案1】:

我认为您正在寻找else if

if (condition1) {
  System.out.println("1");
} else if (condition2) {
  System.out.println("2");
} else {
  System.out.println("other");
}

将只执行这些System.out.println 语句中的一个:如果condition1condition2 都为真,则只打印1,因为首先匹配condition1

【讨论】:

  • 我尝试过 else if 但它得到了相同的结果@Andy_Turner
  • 由于您显示的代码不会给出您显示的输出,因此您显然做错了。
【解决方案2】:

== 比较对象引用,它检查两者是否指向同一个对象(它们不是)。这就是为什么你有 2 个输出。

你应该使用.equals

if (!v1.equals(v2) && !v2.equals(v3) && !v1.equals(v3))
{
 ...
}
else if(...)
{
...
}

// use this custom equals for your case
@Override
public boolean equals(Object obj) {
    if (!(obj instanceof PointClass)) {
        return false;
    }

    PointClass temp = (PointClass) obj;

    return this.x.equals(obj.x)
        && this.y.equals(obj.y);
}

【讨论】:

  • 我如何使用它?? @deviantxdes
  • @MadJlzz 不,我读了很多书,但练习与阅读不同
  • @deviantxdes public int x = 0;公共 int y = 0;公共 PointClass() { } 公共 PointClass(int x , int y) { this.x = x;这个.y = y; }
  • @AbdelrahmanAli,不要在 cmets 中显示代码。在您的帖子中显示它。当您使用它时,请正确格式化整个帖子。
  • @AbdelrahmanAli 你当时什么都不懂。如果你不想进步,这是基本的知识,否则你将无法进步。
【解决方案3】:

听着,我不是想让你失望。我只是在观察并给你一些建议。

正如其他人所说,当您在 Objects 上使用 == 时,它会比较引用。

引用是在Stack 中创建的包含内存地址的块。当你调用new 操作符时,Java 实际上是在Stack 中为你保留一个新的地址内存。

此引用指向 Heap 中的实际 Object,您可以在其中找到构成对象的所有内容。

因此,当您执行以下操作时: v1 == v2,您实际上是在查看 Object v1 的内存地址是否实际上与 Object v2 的地址相同,这不是因为您调用了两次 new 运算符。

这是我的意思的图纸:

你可以通过使用来证明它:

System.out.println("V1 object : " + v1);
System.out.println("V2 object : " + v2);

您会发现地址不同。

最后,如果您不想比较对象,这是可能的,但您需要告诉 Java 如何比较它们。这是通过@Overrideequals(Object obj) 方法在Object 类中定义的。

在您的示例中,它可能如下所示(您需要在 PointClass 中覆盖此方法):

 @Override
 public boolean equals(Pointclass p) {
    if (this.x == p.x && this.y == p.y)
      return true;
    return false;
 }

现在如果你使用v1.equals(v2),这将检查点是否位于同一位置。

希望现在对 Java 对象的实例化和引用更加清楚。

【讨论】:

  • @MadJIzz 谢谢你的朋友 :) 我昨天做了另一种方法,但谢谢你的工作,这很好(y)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
相关资源
最近更新 更多