【问题标题】:JAVA(array index out of bound exception),Nutanix placement questionJAVA(数组索引越界异常),Nutanix放置问题
【发布时间】:2020-01-01 07:03:47
【问题描述】:

问题参考链接: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】:
    for(int a = 0; a < 3; a++) {
        y[i] = s1.nextInt();
    }
    

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

    【讨论】:

    • 谢谢!我真是个愚蠢的错误,叹息。
    猜你喜欢
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多