Java 句子缩短程序无法正常工作
Java sentence shortening program doesn't work right
import java.util.Scanner;
public class SentenceShortener {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
String sentence = "";
String sentence2 = "";
String sentence3 = "";
String finalcount = "";
System.out.println("Input sentence ");
sentence = kbd.nextLine();
int space = sentence.indexOf(" ");
System.out.println(sentence.substring(space+1));
sentence2 = sentence.substring(space+1);
int space2 = sentence.indexOf(" ");
System.out.println(sentence2.substring(space2+1));
sentence3 = sentence2.substring(space2+1);
int space3 = sentence2.indexOf(" ");
System.out.println(sentence3.substring(space3+1));
finalcount = sentence3.substring(space3+1);
System.out.println("Your " + sentence.length() + " character long sentence was shortened to " + finalcount.length() + " characters.");
}
}
我创建了一个程序,它应该缩短四个单词的句子,然后再缩短到越来越短的“句子”,并计算你将句子缩短到多少个字符。
它应该是这样工作的:
Input sentence
The car runs good.
car runs good.
runs good.
good.
Your 18 character long sentence was shortened to 6 characters.
实际上如何 运行s:
Input sentence
The car runs good.
car runs good.
runs good.
good.
Your 18 character long sentence was shortened to 6 characters.
另一个运行:
Input sentence
Why is it raining?
is it raining?
t raining?
aining?
Your 18 character long sentence was shortened to 7 characters.
我是 Java 的初学者。
确定 space 索引的逻辑不正确。
尝试修改您的代码以具有
int space2 = sentence2.indexOf(" ");
int space3 = sentence3.indexOf(" ");
import java.util.Scanner;
public class SentenceShortener {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
String sentence = "";
String sentence2 = "";
String sentence3 = "";
String finalcount = "";
System.out.println("Input sentence ");
sentence = kbd.nextLine();
int space = sentence.indexOf(" ");
System.out.println(sentence.substring(space+1));
sentence2 = sentence.substring(space+1);
int space2 = sentence.indexOf(" ");
System.out.println(sentence2.substring(space2+1));
sentence3 = sentence2.substring(space2+1);
int space3 = sentence2.indexOf(" ");
System.out.println(sentence3.substring(space3+1));
finalcount = sentence3.substring(space3+1);
System.out.println("Your " + sentence.length() + " character long sentence was shortened to " + finalcount.length() + " characters.");
}
}
我创建了一个程序,它应该缩短四个单词的句子,然后再缩短到越来越短的“句子”,并计算你将句子缩短到多少个字符。
它应该是这样工作的:
Input sentence
The car runs good.
car runs good.
runs good.
good.
Your 18 character long sentence was shortened to 6 characters.
实际上如何 运行s:
Input sentence
The car runs good.
car runs good.
runs good.
good.
Your 18 character long sentence was shortened to 6 characters.
另一个运行:
Input sentence
Why is it raining?
is it raining?
t raining?
aining?
Your 18 character long sentence was shortened to 7 characters.
我是 Java 的初学者。
确定 space 索引的逻辑不正确。 尝试修改您的代码以具有
int space2 = sentence2.indexOf(" ");
int space3 = sentence3.indexOf(" ");