最大错误

时间:2015-12-27 07:21:58

标签: java

import java.util.Random;

import java.util.Scanner;

class apples {

    public static void main(String[] args){

    Random rand = new Random();

    Scanner sc = new Scanner(System.in);

    int freq[] = new int[7];

    for (int roll=1;roll<=10000;roll++){

        ++freq[1+rand.nextInt(6)];

    }

    System.out.println("So much money at stake,make a right decision!\nWhat are betting on?");

    int input= sc.nextInt();

    int comparison=max(freq[1],freq[2],freq[3],freq[4],freq[5],freq[6],freq[7]);

    if(input==comparison){

        System.out.println("You won");

    }else {

        System.out.println("Whoops,you lost all your money");

    }

}

private static int max(int i, int j, int k, int l, int m, int n, int o) {

    // TODO Auto-generated method stub

    return 0;

}

3 个答案:

答案 0 :(得分:2)

好吧,var AwesomeProject = React.createClass({ getInitialState: function() { return { isLoading: true, photos: this.props.photos }; }, componentWillMount: function(){ fetch("http://localhost:3000/api/photos/", { method: "GET", headers: { "x-access-token":"xxxxx", "x-key":"xxxx" }, }) .then((response) => response.json()) .then((responseData) => { AlertIOS.alert( "GET Response", "Search Query -> " + JSON.stringify(responseData) ) // Set both state properties at the same time to avoid passing an undefined value as a prop to RenderPhotos this.setState({ isLoading: false, photos: JSON.stringify(responseData) }); }) .done(); }, render: function() { if(this.state.isLoading){ return <View><Text>Loading...</Text></View> } // RenderPhotos should receive this.state.photos as a prop, not this.props.photos return ( <RenderPhotos photos={this.state.photos}/> ); }, }); var RenderPhotos = React.createClass({ getInitialState: function() { return { photos: this.props.photos }; }, render: function(){ var photos = Photos; return ( <View> <Text> {this.props.photos[0]} </Text> </View> ) } }); 会给你freq[7],因为你的数组只有7个元素,索引从0到6。

除此之外,将数组元素作为单独的参数传递给ArrayIndexOutOfBoundsException方法是没有意义的。只需在一个参数中传递整个数组。

答案 1 :(得分:1)

您有java.lang.ArrayIndexOutOfBoundsException这意味着您尝试从索引中获取大于数组大小的元素。 所以,使用

int comparison=max(freq[0],freq[1],freq[2],freq[3],freq[4],freq[5],freq[6]);

而不是

int comparison=max(freq[1],freq[2],freq[3],freq[4],freq[5],freq[6],freq[7]);

因为JAVA索引从 0 开始而不是 1

答案 2 :(得分:-2)

请为您的帖子提问,而不仅仅是空方法。下面的代码可用于查找max:

nn.nn.nn.nn