gpt4 book ai didi

java - 类方法如何将存储的数据传递给同一类中的其他方法

转载 作者:行者123 更新时间:2023-12-01 11:57:04 25 4
gpt4 key购买 nike

我从我正在学习的书中学习了这门类(class)。但书中并没有准确解释 then 的 translate 方法的返回值如何在下面的 translateWord 方法中使用,等等。书上唯一说的是:

The translate method passes each word to the private support method 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"));

}







}

最佳答案

作为 Javadoc 显示,next()方法返回一个字符串。 translateWord(String word)接受字符串作为参数,因此到一天结束时任何内容都会解析为 String可以传递给这个方法。

因此,这一行:translateWord(scan.next());本质上是以下代码的简写:

String scannerInput = scan.next();
translateWord(scannerInput);

给定一个字符串,This is a test ,扫描仪将返回 This , is , a , test 。因此,这些字符串将被输入到您的 translateWord方法。

关于java - 类方法如何将存储的数据传递给同一类中的其他方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28369775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com