不知道怎麼改才能按回車直接輸出0
還得按一次空格再回車才不出現錯誤 求教怎麼修改
import java.util.Scanner;
public class Length {
public static void main(String[]args){
Scanner in= new Scanner(System.in);
String[]num =null;
num= in.nextLine().split(" ");
int[]number = new int[num.length];
for(int i= 0;i<number.length;i++){
number[i]= Integer.valueOf(num[i]);
}
System.out.println(number.length);
}
}
在轉化int之前加個判斷
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String[] num = null;
num = in.nextLine().split(" ");
if (num[0].equals("")) {
System.out.println("#Total:0");
} else {
int[] number = new int[num.length];
for(int i= 0;i<number.length;i++){
number[i]= Integer.valueOf(num[i]);
}
System.out.println("#Total:" + number.length);
}
}
}