我试图打印出用户指定的字母数量。对于例如用户键入一个字 - 马,并指定他们想要打印的字母数 - 4.输出结果应该是: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;
}
}
}
我建议你阅读子的文档。 – 2014-12-02 19:37:40