2015-11-17 51 views
-2

我剛剛嘗試註冊,目前正在練習二,試圖找出答案。在這裏它的代碼。註冊Codewars Java練習2

public class Person { 
    String name; 

    public Person(String personName) { 
     name = personName; 
    } 

    public String greet(String yourName) { 
     return String.format("Hi %s, my name is %s", name, yourName); 
    } 
} 

這些都是在當時看來是有道理的我所做的更改,但我仍然會通過它來尋找適合自己的

public class Person { 
    String name; 

    public Person(String personName) 
    { 
     name = "John"; 
    } 

    public String greet(String "Tommy"); 
    { 
     return String.format("Hi %s, my name is %s", name, yourName); 
    } 
} 

如果我粘貼到Eclipse和從那裏運行它有作弊?

我希望這已經夠清楚了,如果有什麼方法可以改進這篇文章或者讓任何東西更清楚,請讓我知道:)謝謝!

+1

複製/粘貼和不理解是一種欺騙行爲。代碼中存在大量錯誤,我建議您閱讀Java的基礎知識。我可以用更正來回答你,但是,不知道如何才能幫助你,除非你從Java的基礎開始。 – Raf

+0

這個問題不是關於編程。 OP在另一個網站上參加了一個編碼遊戲,並且詢問了另一個網站上的遊戲規則 –

+0

我認爲你的權利,我真的真的生鏽了,可能最好關注基礎知識謝謝你們 –

回答

0

更正和在下面的代碼中的意見。如果你學習了一些語言的基礎知識,你會明白一切,否則它會很有趣。

public class Person { 
    String name; 

    //This is a constructor and invoked during creation of object 
    public Person(String personName) 
    { 
      //makes no sense to pass personName to construct but, not use it 
      //name = "John"; 
      name = personName; 
    } 

    //A method signature does not end with semi-colon 
    //You cannot pass string literal in function as argument, "Tommy" incorrect arg 
    //public String greet(String "Tommy"); 
    public String greet(String greeting) 
    { 
      //not sure where you come up with yourName but, i replaced with greeting arg 
      return String.format("Hi %s, my name is %s", name, greeting); 
    } 

    //add main to show you how this works, when run execution enters main first 
    public static void main(String[] args) { 

     //an object of Person class is created passing "John" to its constructor  
     Person p = new Person("John"); 

     //p.greet() invokes the greet method of Person object 
     System.out.println(p.greet("Oops!")); 
    } 
} 
1

,你只需要在的String.format交換姓名和YOURNAME()函數,見下面的代碼

return String.format("Hi %s, my name is %s", yourName, name); 
public class Person { 

    String name; 

    public Person(String personName) { 
     name = personName; 
    } 

    public String greet(String yourName) { 
     return String.format("Hi %s, my name is %s", yourName, name); 
    } 
}