簡體   English   中英

JAVA陣列+掃描儀錯誤?

[英]JAVA array + scanner errors?

我正在做一些程序,但很難使用陣列和掃描儀。 我希望用戶輸入他玩過的游戲數,然后輸入他完成的游戲數。 之后,我要他輸入完成每場比賽所花費的時間。

我對此並不陌生,現在真的很困。 我需要獲取用戶完成游戲所花費的每一次價值,因為以后需要將一些統計信息應用於該值。

我遇到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;
        }
    }
}

非常感謝您的幫助!

首先,請使用if語句,而不是兩個while循環。

問題是這一行: int list[]=new int[game];

上一行( int game=1; )將此變量的值設置為1,因此您的int數組只能存儲一個元素。

您應該使用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();
    }
}
}
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;
       }
   } 

}

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM