Pigltin,难以理解如何移动“。”在一个字符串中
Piglatin, difficulty understanding how to move a '.' in a String
我为我的计算机科学编写的这个程序遇到了很大的困难 class。我浏览并查看了学生在这里和其他网站上发布的其他程序,但我一直无法理解我做错了什么。我已经查看了 Java oracle 文档,它就是不合我意。
我的主要问题是应该移动“.”的嵌套部分 if 语句在单词后添加 "ay" 到句子的末尾。申请不完整。在我学会如何解决这一步之后,我将继续处理句子中第一个单词的大写。我只是不明白为什么我在使用 .substring 和 .charAt 时遇到困难。
请解释答案。
File input = new File ("elab.txt");
File output = new File ("elab.out");
//create Scanner
Scanner in = new Scanner(input);
//create writer
PrintWriter out = new PrintWriter(output);
//VariParty, Part 1
int count = 0;//Simple counter
String word;//Scanned word from text file, will be edited
String original;//Original word, backed up from String "word" before edit
String original1;//Used in consonants
String sentence = "";//Declare full sentence
/* What I will do is scan each word in the text document. The application will display
* a number for each word for my visual learning pleasure. The counter will be //commented
* out as to not cause any visual action for the complete application.
*/
while(in.hasNext()){//Star while
word = in.next();//Scans individual word
word = word.toLowerCase();
count++;//adds to counter
original = word.toLowerCase();
original1 = word.toLowerCase();
//VariParty, Part 2
char fl = word.charAt(0);
char fL = Character.toLowerCase(word.charAt(0));
char endChar = word.charAt(word.length()-1);
/* This vowel portion of the application will check each "word" and deduce if "ay"
* should be added to the end of the word beginning with a vowel. Finally, the code
* will build onto the sentence string to produce a sentence.
*/
if (fl == 'a' || fl == 'e' || fl == 'i'|| fl == 'o' || fl == 'u'){//Start if
word = word + "ay";
System.out.println(count + " VOW) " + "\"" + original + "\"" + " to " + "\"" + word + "\"" + " ");
out.print(word + " ");
sentence = sentence + " " + word;
}//End if
/* We will now work towards building a better sentence. Our sentence will not have the
* audacity to place a period in the middle of the word! Our sentences shall forever be
* properly defined with a punctuation mark at the end, unless it's an apostrophe.
*/
if(word.contains(".")){//Start nested if
word = word.substring(0,word.length()-1);
word = word + ".";
System.out.println(count + " NES VOW) " + "\"" + original + "\"" + " to " + "\"" + word + "\"" + " ");
}//End nested if
/* Now we will move the word around and deal with the consonants
* The else statement will build the proper word by moving the first
* letter to the end of the sentence, then adding "ay" to the end.
* The final line will be building onto the sentence string.
*/
else{//Start else
word = word.substring(1,word.length());
original = word;
word = word + fl + "ay";
System.out.println(count + " CON) " + "\"" + original1 + "\"" + " to " + "\"" + word + "\"" + " ");
out.print(word + " ");
sentence = sentence + " " + word;
}//End else
}//End While
System.out.println(sentence);
in.close();//Close Scanner
out.close();//Close PrintWriter
在下一行中,您的结束索引是词尾。因此,我不确定您在此步骤中要做什么。
word = word.substring(0,word.length()-1);
根据文档
public String substring(int beginIndex,
int endIndex)
所以你的步骤应该保持原样。那是你的问题吗。我无法发表评论,所以将其添加为答案...
此外,如果您在 while 循环中修改字符串,请尝试使用 Stringbuffer。
您应该做的第一件事是让您的程序可测试。
主要功能是将单词转换成 piglatin。因此,您应该定义一个方法来完全做到这一点:
public static String translateWordToPigLatin(String word) {
....
return ...;
}
然后,您应该编写另一个名为 PigLatinTest
的 class,在其中测试此方法:
public class PigLatinTest {
public static void main(String[] args) {
testPigLatin("hello", "ellohay");
testPigLatin("welcome", "ellcomeway");
// Add more test cases here.
}
private static void testPigLatin(String english, String expectedPigLatin) {
String actual = translateWordToPigLatin(english);
if (!expectedPigLatin.equals(actual)) {
throw new AssertionError("Expected \"" + expectedPigLatin + "\" for \"" + english + "\", got \"" + actual "\".");
}
}
}
在translateWordToPigLatin
方法中,我调用了参数word
。这是“.”的强烈指示。不属于那里。所以你真正应该做的是从一行中提取所有正确的单词,其中一个单词只包含字母。
应该调用下一个更大的方法 translateLineToPigLatin(String line)
,在这个方法中,您应该:
- 从英文行的开头开始。
- 将字符复制到猪拉丁文行,直到找到一个字母。
- 在英文行中进一步扫描,直到找不到字母为止。
- 第 2 步和第 3 步之间的文本是一个单词。
- 将该词转换为 pig latin。
- 将转换后的单词添加到猪拉丁文行中。
- 继续浏览您离开的英文行。
我为我的计算机科学编写的这个程序遇到了很大的困难 class。我浏览并查看了学生在这里和其他网站上发布的其他程序,但我一直无法理解我做错了什么。我已经查看了 Java oracle 文档,它就是不合我意。
我的主要问题是应该移动“.”的嵌套部分 if 语句在单词后添加 "ay" 到句子的末尾。申请不完整。在我学会如何解决这一步之后,我将继续处理句子中第一个单词的大写。我只是不明白为什么我在使用 .substring 和 .charAt 时遇到困难。
请解释答案。
File input = new File ("elab.txt");
File output = new File ("elab.out");
//create Scanner
Scanner in = new Scanner(input);
//create writer
PrintWriter out = new PrintWriter(output);
//VariParty, Part 1
int count = 0;//Simple counter
String word;//Scanned word from text file, will be edited
String original;//Original word, backed up from String "word" before edit
String original1;//Used in consonants
String sentence = "";//Declare full sentence
/* What I will do is scan each word in the text document. The application will display
* a number for each word for my visual learning pleasure. The counter will be //commented
* out as to not cause any visual action for the complete application.
*/
while(in.hasNext()){//Star while
word = in.next();//Scans individual word
word = word.toLowerCase();
count++;//adds to counter
original = word.toLowerCase();
original1 = word.toLowerCase();
//VariParty, Part 2
char fl = word.charAt(0);
char fL = Character.toLowerCase(word.charAt(0));
char endChar = word.charAt(word.length()-1);
/* This vowel portion of the application will check each "word" and deduce if "ay"
* should be added to the end of the word beginning with a vowel. Finally, the code
* will build onto the sentence string to produce a sentence.
*/
if (fl == 'a' || fl == 'e' || fl == 'i'|| fl == 'o' || fl == 'u'){//Start if
word = word + "ay";
System.out.println(count + " VOW) " + "\"" + original + "\"" + " to " + "\"" + word + "\"" + " ");
out.print(word + " ");
sentence = sentence + " " + word;
}//End if
/* We will now work towards building a better sentence. Our sentence will not have the
* audacity to place a period in the middle of the word! Our sentences shall forever be
* properly defined with a punctuation mark at the end, unless it's an apostrophe.
*/
if(word.contains(".")){//Start nested if
word = word.substring(0,word.length()-1);
word = word + ".";
System.out.println(count + " NES VOW) " + "\"" + original + "\"" + " to " + "\"" + word + "\"" + " ");
}//End nested if
/* Now we will move the word around and deal with the consonants
* The else statement will build the proper word by moving the first
* letter to the end of the sentence, then adding "ay" to the end.
* The final line will be building onto the sentence string.
*/
else{//Start else
word = word.substring(1,word.length());
original = word;
word = word + fl + "ay";
System.out.println(count + " CON) " + "\"" + original1 + "\"" + " to " + "\"" + word + "\"" + " ");
out.print(word + " ");
sentence = sentence + " " + word;
}//End else
}//End While
System.out.println(sentence);
in.close();//Close Scanner
out.close();//Close PrintWriter
在下一行中,您的结束索引是词尾。因此,我不确定您在此步骤中要做什么。
word = word.substring(0,word.length()-1);
根据文档
public String substring(int beginIndex,
int endIndex)
所以你的步骤应该保持原样。那是你的问题吗。我无法发表评论,所以将其添加为答案...
此外,如果您在 while 循环中修改字符串,请尝试使用 Stringbuffer。
您应该做的第一件事是让您的程序可测试。
主要功能是将单词转换成 piglatin。因此,您应该定义一个方法来完全做到这一点:
public static String translateWordToPigLatin(String word) {
....
return ...;
}
然后,您应该编写另一个名为 PigLatinTest
的 class,在其中测试此方法:
public class PigLatinTest {
public static void main(String[] args) {
testPigLatin("hello", "ellohay");
testPigLatin("welcome", "ellcomeway");
// Add more test cases here.
}
private static void testPigLatin(String english, String expectedPigLatin) {
String actual = translateWordToPigLatin(english);
if (!expectedPigLatin.equals(actual)) {
throw new AssertionError("Expected \"" + expectedPigLatin + "\" for \"" + english + "\", got \"" + actual "\".");
}
}
}
在translateWordToPigLatin
方法中,我调用了参数word
。这是“.”的强烈指示。不属于那里。所以你真正应该做的是从一行中提取所有正确的单词,其中一个单词只包含字母。
应该调用下一个更大的方法 translateLineToPigLatin(String line)
,在这个方法中,您应该:
- 从英文行的开头开始。
- 将字符复制到猪拉丁文行,直到找到一个字母。
- 在英文行中进一步扫描,直到找不到字母为止。
- 第 2 步和第 3 步之间的文本是一个单词。
- 将该词转换为 pig latin。
- 将转换后的单词添加到猪拉丁文行中。
- 继续浏览您离开的英文行。