nextInt存储在Java数组中
所以我有这个代码:nextInt存储在Java数组中,java,Java,所以我有这个代码: int[] player1 = new int[5]; // first player try{ for(int i=0;i<5;i++) { player1[i] = keyin.nextInt();} } catch(NumberFormatException e){ System.out
int[] player1 = new int[5]; // first player
try{
for(int i=0;i<5;i++) {
player1[i] = keyin.nextInt();}
}
catch(NumberFormatException e){
System.out.println("Player 2 : ")
}
int[] player2 = new int[5]; // Second Player
try{
for(int i=0;i<5;i++) {
player2[i] = keyin.nextInt();}
}
catch(NumberFormatException e){
System.out.println("Player 3 : ");
}
int[]player1=新int[5];//第一名选手
试一试{
for(inti=0;i使用for循环请求用户输入5个数字
for(int i=0;i<5;i++) {
player1[i] = keyin.nextInt();}
}
然后使用一个if语句,一旦输入达到5个字符,或者如果用户通过按键盘上的一个键来跳出循环。nextInt
后面应该跟着nextLine
,以删除仍保留在系统中的回车符。
缓冲区中…否则nextInt
将在下次启动时失败called@MadProgrammer我该怎么写呢?(新手程序员!)keyin.nextLine()
…@MadProgrammer你说后面跟着nextLine?对不起,复制并粘贴我正在尝试这样做,用户只需按int以外的任何东西,它必须看起来像Player1:1234
,在下一行Player2:3221
我会将while循环放在for循环之外?
while(keyin.hasNextInt()){
}