【发布时间】:2014-12-02 19:36:06
【问题描述】:
我正在尝试打印用户指定的字母数量。例如用户输入单词-Horse 并指定要打印的字母数- 4. 输出应为Result: Hors。我必须使用子字符串方法。
我的程序删除了用户指定的字母并打印出其余的字母。我怎样才能解决这个问题?
import java.util.Scanner;
public class FirstPart {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Type a word: ");
String word = reader.nextLine();
System.out.println("Length of the first part: ");
int firPar = Integer.parseInt(reader.nextLine());
int i = 0;
while (i <= firPar) {
System.out.print("Result: " + word.substring(firPar));
i++;
break;
}
}
}
【问题讨论】:
-
我建议你阅读 substring 的文档。
标签: java input while-loop