JAVA阵列+扫描仪错误?

时间:2014-10-30 18:37:22

标签: java

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

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

我收到2个错误!

这是我的一些代码:

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;
        }
    }
}

非常感谢你的帮助!

3 个答案:

答案 0 :(得分:0)

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

问题在于这一行:int list[]=new int[game];

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

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

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

编辑:

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();
    }
}
}

答案 1 :(得分:0)

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;
       }
   } 

}

答案 2 :(得分:0)

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;
    }
}

}