我是Java的新手,我对此有疑问。我必须创建3种方法,老实说,您可以用不到3种方法做到这一点,但我正在尝试遵循它告诉我的方法。
这是它的意思:
“您可以使用String类的实例方法char CharAt(int i)获取位置i处的字符,并使用Character类的boolean isWhitespace(char ch)类方法来确定ch是否为空白。” >
我不知道如何处理CharAt和printWords方法。以及如何使这些方法相互参考。
public class printWords
{
public static void printWords(String s) {
String words = "quick brown fox jumps over the lazy dog";
System.out.println(words);
}
public void CharAt(int i) {
char[] c = new char[39];
for (i = 0; i < c.length; i++) {
}
}
public static boolean isWhitespace (char ch) {
if (Character.isWhitespace(ch)) {
return true;
}
else {
return false;
}
}
}
结果是为每个空格创建新行。就像这样:
快速
棕色
fox
跳跃
在
的
懒惰
狗
答案 0 :(得分:0)
从您的下一篇文章开始,请提供更多有关您的思考过程的信息,并实际尝试编码,而不是简单地说您“无所适从”(这就是为什么这个问题被拒绝的原因)。话虽如此,我将为您提供解决此问题的方法,但是您有责任了解我的思考过程以及代码各部分为何起作用。如果您是我,我不会使用本文中提供的解决方案,而是使用Java文档并完全计划(绘制或伪代码)适当的思考过程。归根结底,如果您接受了我给您的代码而又不理解(并且没有进行适当的编码尝试),那么您就在欺骗自己:
import java.util.*;
public class printWords {
//main method
public static void main(String[] args) {
String words = "quick brown fox jumps over the lazy dog";
printWords.printWords(words);
}
//This is the main printing method:
//If it sees a whitespace, a whitespace will be printed
//Otherwise, I will call a helper function (Which one is it?) that will process the component of the string
//without whitespaces (if you do not understand what I said, take your time and use print statements throughout the code)
public static void printWords(String s) {
String words = "quick brown fox jumps over the lazy dog";
int i = -1;
while (i < s.length()) {
i += 1;
char curr = s.charAt(i);
if (Character.isWhitespace(curr)) {
System.out.println();
} else {
//What does this return?
i = obtainNonWhiteSpace(s, i);
}
}
}
//This is the helper function
//What does it do? I will vaguely say that it parses a non white-space string chunk
//PLEASE PLEASE work through and understand what is going on?
public static int obtainNonWhiteSpace(String s, int start_index) {
String result = "";
int i = start_index;
//Consider the order of the two statements in the while clause (the ones connected by the "&&")
//What would happen if I changed the order?
while (i < s.length() && !Character.isWhitespace(s.charAt(i))) {
result = result + s.charAt(i);
i += 1;
}
//What is going on here?
System.out.println(result);
//Why do I need this condition?
if (i != s.length()) {
System.out.println();
}
//What value is this?
return i;
}
}
请,请花些时间回答我留下的问题。完成此操作后,请重新实现此功能或(希望!)使用另一种方法(并在需要时进行更改)。
答案 1 :(得分:0)
这是在空白处创建线条的方法。我不认为创建方法
public class printWords{
public static void main(String [] args) {
String words = "quick brown fox jumps over the lazy dog";
for (int i = 0; i < words.length(); i++) {
System.out.print(words.charAt(i));
if(Character.isWhitespace(words.charAt(i)))
//creates a line when there's white space
System.out.println();
}
}
}
输出:
快速
棕色
fox
跳转
在
懒惰
狗
答案 2 :(得分:0)
Java的 String 类已经包含方法 charAt(),而Java的 Character 类已经包含方法 isWhitespace()强>。根据我的阅读,您不需要创建那些方法。任务是实际上只是利用这些方法,因为它们是可用的。您可能要修改您的 printWords()方法:
public static void printWords(String strg) {
/* This variable will be used to hold each word in the
spplied string argument. */
String eachWord = "";
// Iterate through each character of the supplied String argument.
for (int i = 0; i < strg.length(); i++) {
/* Upon each FOR loop iteration, grab the character
located at the current index location specified
by the integer variable i and place it into the
variable c. */
char c = strg.charAt(i);
/* If the current character held in the variable c
is a White-Space AND the string variable eachWord
contains anything other than Null String ("") then
display the word held in eachWord to Console. Next,
clear the variable so as to prepare for the next
word. */
if (Character.isWhitespace(c) && !eachWord.equals("")) {
System.out.println(eachWord);
eachWord = "";
}
/* If the current character held in the variable c
is NOT a White-Space then concantenate that
character to the string variable eachWord so as
to build a word.
*/
else {
eachWord+= c;
}
}
/* Because the supplied String argument most likely doesn't
actually contain a white-space at the very end of the
string, the code within the FOR loop will not print the
final word to console. Therefore, after iterations are
done we check to see if a word was built and held in the
eachWord variable. If the variable actually contains
anything other than a Null String ("") then we print it
to the Console Window as well. */
if (!eachWord.equals("")) {
System.out.println(eachWord);
}
}
并通过您的 main()方法(或任意位置)
String words = "Quick brown fox jumps over the lazy dog.";
System.out.println(words); // Display the whole string to console.
printWords(words); // Display the words in string.