空白位置查找
Whitespace Location Finding
所以我的指令如下:
Write code to print the location of any space in the 2-character string passCode. Each space detected should print a separate statement followed by a newline. Sample output for the given program:
Space at 1
我目前写的代码是:
import java.util.Scanner;
public class FindSpaces {
public static void main (String [] args) {
String passCode = "";
passCode = "A ";
if (Character.isWhitespace(passCode.charAt(0))){
System.out.println("Space at " +passCode.indexOf(" "));
}
else if (Character.isWhitespace(passCode.charAt(1))){
System.out.println("Space at " +passCode.indexOf(" ", 1));
}
else{
}
return;
}
}
现在这有时有效,但如果我的输入中有多个 space,它只会打印一行。如果它对任何人有帮助,它来自 zyBooks,我不知道如何让它显示第二行显示第二个白色space。
虽然长度可能限制为两个,但我仍然认为 for 循环更适合:
String passCode = "";
passCode = " ";
for (int i = 0; i < passCode.length(); i++) {
if (passCode.charAt(i) == ' ') {
System.out.println("Whitespace at index " + (i + 1));
}
}
查看您的解决方案:您的代码不能多次输入任何输出。你有 if() else() - 只需删除 else,你应该没问题:
String passCode = "";
passCode = " ";
if (Character.isWhitespace(passCode.charAt(0))) {
System.out.println("Space at " + passCode.indexOf(" "));
}
if (Character.isWhitespace(passCode.charAt(1))) {
System.out.println("Space at " + passCode.indexOf(" ", 1));
}
当你这样做时
if ( condition1 ) {
doSomething();
}
else if ( condition2 ) {
doSomethingElse();
}
else
意味着如果 condition1
为假,您将只测试 condition2
(因此只有机会调用 doSomethingElse()
)。您只需要从代码中删除 else
。
要查找并打印字符串中每个空格的位置,您只需遍历字符串并检查当前字符是否为空格:
String str = "Find the space location";
for(int i = 0; i < str.length(); i++) {
if(Character.isWhitespace(str.charAt(i)) {
System.out.println("Space at " + i);
}
}
所以我的指令如下:
Write code to print the location of any space in the 2-character string passCode. Each space detected should print a separate statement followed by a newline. Sample output for the given program: Space at 1
我目前写的代码是:
import java.util.Scanner;
public class FindSpaces {
public static void main (String [] args) {
String passCode = "";
passCode = "A ";
if (Character.isWhitespace(passCode.charAt(0))){
System.out.println("Space at " +passCode.indexOf(" "));
}
else if (Character.isWhitespace(passCode.charAt(1))){
System.out.println("Space at " +passCode.indexOf(" ", 1));
}
else{
}
return;
}
}
现在这有时有效,但如果我的输入中有多个 space,它只会打印一行。如果它对任何人有帮助,它来自 zyBooks,我不知道如何让它显示第二行显示第二个白色space。
虽然长度可能限制为两个,但我仍然认为 for 循环更适合:
String passCode = "";
passCode = " ";
for (int i = 0; i < passCode.length(); i++) {
if (passCode.charAt(i) == ' ') {
System.out.println("Whitespace at index " + (i + 1));
}
}
查看您的解决方案:您的代码不能多次输入任何输出。你有 if() else() - 只需删除 else,你应该没问题:
String passCode = "";
passCode = " ";
if (Character.isWhitespace(passCode.charAt(0))) {
System.out.println("Space at " + passCode.indexOf(" "));
}
if (Character.isWhitespace(passCode.charAt(1))) {
System.out.println("Space at " + passCode.indexOf(" ", 1));
}
当你这样做时
if ( condition1 ) {
doSomething();
}
else if ( condition2 ) {
doSomethingElse();
}
else
意味着如果 condition1
为假,您将只测试 condition2
(因此只有机会调用 doSomethingElse()
)。您只需要从代码中删除 else
。
要查找并打印字符串中每个空格的位置,您只需遍历字符串并检查当前字符是否为空格:
String str = "Find the space location";
for(int i = 0; i < str.length(); i++) {
if(Character.isWhitespace(str.charAt(i)) {
System.out.println("Space at " + i);
}
}