【问题标题】:How to call results of a Boolean method in a main method如何在主方法中调用布尔方法的结果
【发布时间】:2017-06-13 09:51:27
【问题描述】:

因此,学校已将我们转移到 Java,而没有真正解释基础知识。

现在我有一个问题,我有一个带有方法 equalsTest 的类,用于测试两个名称是否相同。然后在我的主要方法中,如果名称相同(equalsTest = true),我必须调用equalsTest的结果并打印“相同的朋友”,如果名称不同(equalsTest = false)则打印“不同的朋友”

public class Person {

/* Attribute declarations */
private String lastName;    // last name
private String firstName;   // first name
private String email;       // email address

public Person(String firstName, String lastName, String email) {
    this.firstName = firstName;
    this.lastName = lastName;       
    this.email = email;
}

//there are a few other methods in here but are not necessary for the issue explained

public boolean equalsTest(Person other){
    if (this.firstName.equals(other.firstName)&& this.lastName.equals(other.lastName))
        return true;
    else
        return false;

public static void main (String[] args) {
    // create a friend
    Person friend1 = new Person("Mickey", "Mouse", "");
    friend1.setEmail("mickey@uwo.ca");

    // create another friend
    Person friend3 = new Person ("Mickey", "Mouse", "");

    // create a friend without email
    Person friend2 = new Person("Minnie", "Mouse", "");

//Here I need another method that calls the "true" or "false" boolean result of the equalsTest method above and says "if equalsTest true, print "Same Friend", and if equalsTest false, print "Not Same Friend", etc.

另外,实验室的部分要求是我不能编辑 equalsTest 方法,我必须在 main 方法中调用它。我知道只在主要方法中创建一个分析它的部分会更容易,但不能这样做。

【问题讨论】:

  • 您在寻找什么? friend2.equalsTest(friend3)?
  • 正如解释的那样,我有一个方法,如果friend1等于friend2,则返回true或false,我需要的是另一种方法中的一段代码,以调用第一种方法的结果,如果结果为真,打印出“同一个朋友”,如果结果为假,则打印出“不是同一个朋友”

标签: java eclipse if-statement methods boolean


【解决方案1】:

让你的布尔方法静态... 现在您可以在条件语句中使用 if...else

public static boolean equalsTest(Person other){..}

private void myMethod(){
  // Person other = new Person ("Mickey", "Mouse", "");
if(equalsTest(Person other)) {...}
}

【讨论】:

    【解决方案2】:

    你可以在你的主目录中这样做

    if(friend1.equalsTest(friend2))
         System.out.println("same");
     else
         System.out.println("not the same);
    

    【讨论】:

    • 你读过这个问题吗? - “另外,实验室的部分要求是我不能编辑 equalsTest 方法”
    【解决方案3】:

    我会添加一个打印方法,否则你总是必须调用equalsTest 方法。

    public void printFriendsEqual(Person a) {
        if (this.equalsTest(b)) {
            System.out.println("Friends are the same");
        } else {
            System.out.println("Friends are NOT the same");
        }
    }
    

    然后在 main 中调用它:

    // create another friend
    Person friend3 = new Person ("Mickey", "Mouse", "");
    Person friend2 = new Person("Minnie", "Mouse", "");
    friend3.printFriendsEqual(friend2);
    

    【讨论】:

      【解决方案4】:
      if(friend1.equalsTest(friend2)) {
        System.out.print("Same");
      }
      else {
        System.out.print("Not same");
      }
      

      【讨论】:

      • 这看起来更像 Python,我正在 Java 中尝试这个。同样,如果名称相同,我已经有一个返回 true 或 false 的方法。我无法更改此方法以包含打印。我必须在 main 方法中创建另一个部分,该部分从第一个方法中获取结果,然后根据这些结果进行打印。
      • 那不是java。
      • 我编辑了纯 java 语法的答案。您从 main 方法调用这段代码。
      【解决方案5】:

      您将希望调用该函数并在方法调用的真返回或假返回时执行某些操作。您可以使用简单的 if/else。您也可以使用Ternary Operator,这对于您只关心两个结果时非常有用。

      (条件)? (此处为真):(此处为假)

      String testResultOutput = "";
      
      testResultOutput = friend1.equalsTest(friend3) ? "Friends are the same" : "Friends are NOT the same";
      System.out.println(testResultOutput);
      
      testResultOutput = friend2.equalsTest(friend3) ? "Friends are the same" : "Friends are NOT the same";
      System.out.println(testResultOutput);
      

      基于您的主要输出将是

      Friends are the same
      Friends are NOT the same
      

      【讨论】:

      • 这似乎有效,仍在测试中。在我发布之前,我尝试过 results =friend2.equalsTest(friends1);然后是一个 if 语句,如果为真或为假。不断收到错误,我不知道您可以包含 ? “朋友都是一样的”......等等。你有什么文章解释这个吗?第一次听说
      • 你点击了三元解释的链接吗?这解释了三元运算的工作原理
      猜你喜欢
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 2018-04-15
      相关资源
      最近更新 更多