數據我把這個班從一本書中,我學習。但是這本書沒有精確地解釋如何在下面的translateWord
方法中使用translate
方法的返回值,等等。這本書說的唯一的東西是:如何一個類的方法,可以通過存儲到另一種方法在同一類
翻譯方法將每個單詞傳遞給專用支持方法translateWord。
是的,但我想知道這樣的情況發生。我問,因爲我已經結束了JAVA CLASSES的章節,我還不瞭解這個過程。
import java.util.Scanner;
public class PigLatinTranslator {
//-------------------------------------------------------------
//Translates a sentence of words into Pig Latin.
//-------------------------------------------------------------
public static String translate(String sentence)
{
String result = "";
sentence = sentence.toLowerCase();
Scanner scan = new Scanner(sentence);
while(scan.hasNext())
{
result += translateWord(scan.next());
result += " ";
}
return result;
}
//-----------------------------------------------------------------------
//Translates one word into Pig Latin. If the word begins with a vowel,
//the suffix 'yay' is appended to the word. Otherwise, the first letter
//or two are moved to the end of the word, and 'ay' is appended.
//-----------------------------------------------------------------------
private static String translateWord(String word)
{
String result = "";
if(beginsWithVowel(word))
result = word + "yay";
else
if(beginsWithBlend(word))
result = word.substring(2) + word.substring(0,2) + "ay";
else
result = word.substring(1) + word.charAt(0) + "ay";
return result;
}
//--------------------------------------------------------------------
//Determines if the specified word begins with a vowel.
//--------------------------------------------------------------------
private static boolean beginsWithVowel(String word)
{
String vowels = "aeiou";
char letter = word.charAt(0);
return(vowels.indexOf(letter) != -1);
}
//------------------------------------------------------------------------
//Determines if the specified word begins with a particular two-character
//consonant blend.
//------------------------------------------------------------------------
private static boolean beginsWithBlend(String word)
{
return(word.startsWith("bl") || word.startsWith("sc") ||
word.startsWith("br") || word.startsWith("sh") ||
word.startsWith("ch") || word.startsWith("sk") ||
word.startsWith("cl") || word.startsWith("sl") ||
word.startsWith("cr") || word.startsWith("sn") ||
word.startsWith("dr") || word.startsWith("sm") ||
word.startsWith("dw") || word.startsWith("sp") ||
word.startsWith("fl") || word.startsWith("sq") ||
word.startsWith("fr") || word.startsWith("st") ||
word.startsWith("gl") || word.startsWith("sw") ||
word.startsWith("gr") || word.startsWith("th") ||
word.startsWith("kl") || word.startsWith("tr") ||
word.startsWith("ph") || word.startsWith("tw") ||
word.startsWith("pl") || word.startsWith("wh") ||
word.startsWith("pr") || word.startsWith("wr"));
}
}
什麼是你的問題? – 2015-02-06 16:05:29
參數:後像'translateWord( 「foobar的」)',** **中的方法'translateWord','word'具有值 「foobar的」 呼叫。一些閱讀:http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html – 2015-02-06 16:07:10