2017-01-27 7 views
0

したがって、学校では、基本的なことを実際に説明することなく、Javaに移行しました。ブールメソッドの結果をメインメソッドで呼び出す方法

今私は、2つの名前が同じかどうかをテストするequalsTestメソッドを持つクラスがあるという問題があります。私の主な方法では、equalsTestの結果を呼び出す必要があります。名前が同じであれば同じ友達(equalsTest = true)、名前が異なる場合は(samealsTest = 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("[email protected]"); 

    // 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. 

また、ラボの要件の1つは、equalsTestメソッドをCANTで編集できることです。メインメソッドで呼び出す必要があります。私はそれを分析するメインの方法でセクションを作るのは簡単かもしれないが、それを行うことはできません。

+0

何を探してるんですか? 'friend2.equalsTest(friend3)'? – luk2302

+0

説明したように、friend1がfriend2と等しい場合はtrueまたはfalseを返すメソッドがあります。最初のメソッドの結果を呼び出すために、別のメソッドのコードが必要です。結果がtrueの場合は、同じ友達 "になり、結果が偽であれば"同じではない友人 "をプリントアウト – MezyMinzy

答えて

0

この関数を呼び出して、メソッド呼び出しの真の戻り値または偽の戻り値で何かをしたいと思うでしょう。単純なif/elseを使うことができます。 Ternary Operatorを使用することもできます。これは、2つの結果が気になるときに非常に便利です。

(条件)? (真がここに入ります):(偽をここに)自分のメインに基づいて

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); 

出力は、あなたがあなたの主な

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

それは働いているようですが、まだテストしています。投稿する前に私はやってみた 結果= friend2.equalsTest(friends1); trueまたはfalseのif文。エラーが発生しても、私はあなたがそれを含めることができるか分からなかったのですか? 「友だちは同じです」など... これを説明する記事がありますか?初めてそれについて聞いたことがあります – MezyMinzy

+0

あなたはターナーリの説明へのリンクをクリックしましたか?それは3進法がどのように働くかを説明します –

+0

はリンクを見ませんでした、ありがとう! – MezyMinzy

0
if(friend1.equalsTest(friend2)) { 
    System.out.print("Same"); 
} 
else { 
    System.out.print("Not same"); 
} 
+0

これはPythonのように見えますが、私はこれをJavaで試しています。同様に、名前が同じであれば、私はすでに真または偽を返すメソッドを持っています。この方法を変更して印刷物を含めることはできません。最初のメソッドから結果を取り出し、その結果に基づいて印刷する、メインメソッドで別のセクションを作成する必要があります。 – MezyMinzy

+0

これはjavaではありません。 –

+0

純粋なjava構文に対する答えを編集しました。 mainメソッドからこのコードを呼び出します。 – user5262448

0

だろうそれ以外の場合は常に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"); 
    } 
} 

と比べ

がメインでそれを呼び出す:

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

あなたはその質問をお読みになりましたか? - "また、ラボの要件の一部は、私がCANTのequalsTestメソッドを編集できない" –

+0

私の答えを編集する – Kryze

0

でこれを行うことで、私が追加することになりますことができます

Friends are the same 
Friends are NOT the same 
関連する問題