题解 | 截取字符串
截取字符串
https://www.nowcoder.com/practice/a30bbc1a0aca4c27b86dd88868de4a4a
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String str=in.nextLine();
if(str!=null&&!"".equals(str)){
int end=in.nextInt();
in.nextLine();//清除换行符
System.out.println(str.substring(0,end));
}
}
in.close();
}
}

查看9道真题和解析