JAVA(数组索引超出范围的异常),Nutanix放置问题

问题描述 投票:0回答:1

该问题的参考链接:https://www.geeksforgeeks.org/nutanix-interview-question-placement-2019-2020/

行prp [b] = s1.nextInt();中的数组越界异常;卡在几个相同的错误问题上。感谢您的帮助。TIA

代码:

    package pkg161219;
import java.util.*;
public class xD{
    static Scanner s1=new Scanner(System.in);
    public static void main(String[] args)
    {
        int x=s1.nextInt();
        for(int i=0;i<x+1;i++)
        {
            int[] y=new int[3];

            for(int a=0;a<3;a++)
            {
                y[i]=s1.nextInt();
            }
            int[] prp=new int[y[0]];
            int[] prr=new int[y[1]];
            int[] prs=new int[y[2]];

            for(int b=0;b<y[0];b++)
            {
                System.out.println("Enter prices of specs of Processor");
                prp[b]=s1.nextInt();
            }

            for(int c=0;c<y[1];c++)
            {
                System.out.println("Enter prices of specs of RAM");
                prr[c]=s1.nextInt();
            }

            for(int d=0;d<y[2];d++)
            {
                System.out.println("Enter prices of specs of SSD");
                prs[d]=s1.nextInt();
            }
            for(int q=0;q<y[0];q++)
            {
                for(int w=0;w<y[1];w++)
                {
                    for(int e=0;e<y[2];e++)
                    {
                        if(prr[e]>=prs[w]&&prr[e]>=prp[q])
                        {
                            System.out.println(prp[q]*prr[w]+prr[w]*prs[e]);
                        }
                    }
                }
            }       
        }    
    }
}
java arrays indexoutofboundsexception
1个回答
1
投票
for(int a = 0; a < 3; a++) {
    y[i] = s1.nextInt();
}

以上是错误的。应该是y[a]

© www.soinside.com 2019 - 2024. All rights reserved.