JAVA数组+扫描仪错误



我正在做一个小程序,很难使用数组和扫描仪。我希望用户输入他玩过的游戏数量,然后输入他完成的游戏数量。之后,我想让他输入他完成每一场比赛所花费的时间。

我是新手,现在真的很困。我需要得到用户完成游戏的每一次时间的价值,因为我需要稍后对其应用一些统计数据。

我有两个错误!

这是我的一点代码:

import java.util.Scanner;
public class games {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        System.out.println("How many games have you played? (Limit of 10) : ");
        int gamesplayed = input.nextInt();input.nextLine();
        while ( gamesplayed < 0 || gamesplayed> 10 ) {
             System.out.println("This number is invalid!");
             System.exit(0);
        }
        System.out.println("How many of these games have you finished? (Limit of 10) : ");
        int finishedgames = input.nextInt();input.nextLine();
        while ( finishedgames < 0 || finishedgames > 10 || gamesplayed < finishedgames ) {                
             System.out.println("This number is invalid!");
             System.exit(0);
        }
        int game = 1;
        int list[]=new int[game];
        while ( finishedgames+1 != game) 
        {
            System.out.print("How many time you took to finish your game " + game);System.out.println("?");
            list[game] = input.nextInt();
            input.nextLine();
            ++game;
        }
    }
}

非常感谢你们的帮助!

public class Games {
public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many games have you played? (Limit of 10) : ");
    String nextLine = input.nextLine();
    int gamesplayed = toInteger(nextLine);
    while (gamesplayed < 0 || gamesplayed > 10) {
        System.out.println("Wrong input, try again.");
        gamesplayed = toInteger(input.nextLine());
    }
    System.out.println(String.format(
            "How many of these games have you finished? (Limit of %d) : ",
            gamesplayed));
    nextLine = input.nextLine();
    int finishedgames = toInteger(nextLine);
    while (finishedgames < 0 || finishedgames >  gamesplayed ) {
        System.out.println("Wrong input, try again.");
        finishedgames = toInteger(input.nextLine());;
        if (finishedgames > gamesplayed) {
            System.out.print("Invalid Entry. ");
        }
    }
    int list[] = new int[finishedgames];
    for (int i = 0; i < finishedgames; i++){
        System.out.println(String.format("How many time you took to finish your game %d?", i + 1));
        int val = toInteger(input.nextLine());
        while (val == Integer.MAX_VALUE) {
            System.out.println("Wrong input, try again.");
            val = toInteger(input.nextLine());
        }
        list[i] = val;  
    }
    for (int i = 0; i < list.length; i++) {
        System.out.println(String.format("list[%d] = %d", i, list[i]));
    }
}
// Helper method that converts user entry to integer value
private static int toInteger(String nextLine) {
    try {
        return Integer.parseInt(nextLine);
    } catch (NumberFormatException e) {
        return  Integer.MAX_VALUE;
    }
}

}

import java.util.Scanner;
public class games {
      public static void main(String args[]) {
       Scanner input = new Scanner(System.in);
       System.out.println("How many games have you played? (Limit of 10) : ");
       int gamesplayed = input.nextInt();
       while ( gamesplayed < 0 || gamesplayed> 10 ) {
           System.out.println("This number is invalid!");
           gamesplayed = input.nextInt();
       }
       System.out.println("How many of these games have you finished? (Limit of 10) : ");
       int finishedgames = input.nextInt();
       while ( finishedgames < 0 || finishedgames > 10 || gamesplayed < finishedgames ) {
             System.out.println("This number is invalid!");
             finishedgames = input.nextInt();
       }
       int game = 1,Rem =finishedgames-gamesplayed ;
       int list[]=new int[Rem+1];
       while (Rem!=0) {
            System.out.println("How many time you took to finish your game " +game+"?");
            list[game] = input.nextInt();
            ++game;
            --Rem;
       }
   } 

}

首先,请使用if语句,而不是两个while循环。

问题是这条线:int list[]=new int[game];

上面的行(int game=1;)将此变量的值设置为1,因此int数组只能存储一个elment。

您应该使用int list[]=new int[finishedgames];

另外,考虑用for循环替换上一个while循环。

编辑:

import java.util.Scanner;
public class games {
public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many games have you played? (Limit of 10) : ");
    int gamesplayed = input.nextInt();input.nextLine();
    if ( gamesplayed < 0 || gamesplayed> 10 ) {
         System.out.println("This number is invalid!");
         System.exit(0);
    }
    System.out.println("How many of these games have you finished? (Limit of 10) : ");
    int finishedgames = input.nextInt();input.nextLine();
    if ( finishedgames < 0 || finishedgames > 10 || gamesplayed < finishedgames ) {                
         System.out.println("This number is invalid!");
         System.exit(0);
    }
    int list[]=new int[finishedgames];
    for (int i=0;i<list.length;i++) {
        System.out.println("How many time you took to finish your game " + (i+1) + "?");
        list[i] = input.nextInt();
        input.nextLine();
    }
}
}

最新更新