简体   繁体   English

线程“主”中的异常要多少个数字? java.lang.ArrayIndexOutOfBoundsException:4 at Calculator.main(Calculator.java:20)

[英]Exception in thread “main” How many numbers do you want? java.lang.ArrayIndexOutOfBoundsException: 4 at Calculator.main(Calculator.java:20)

So i was making a calculator that uses exponents... 所以我在做一个使用指数的计算器...

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("How many numbers do you want?");

        //starts the array 
        //[2] means numbers lower than 2
        int[] lnum = new int[2];

        for (int i=0; i<lnum.length; i++)
        {
            int[] num = new int[4];
            num[2] = 2;
            num[3] = 3;
            num[4] = 4; 

            Scanner x = new Scanner(System.in);
            int I = x.nextInt();

            if (I == num[2])
            {
                Scanner y = new Scanner(System.in);
                System.out.println("Enter a number...");
                int fnum = y.nextInt();
                System.out.println("Enter you're second number...");
                int snum = y.nextInt();
                int anum = (fnum + snum);
                System.out.println("The anser is " + anum);
            }

            if (I == num[3])
            {
                Scanner y = new Scanner(System.in);
                System.out.println("Enter a number...");
                int fnum = y.nextInt();
                System.out.println("Enter you're second number...");
                int snum = y.nextInt();
                System.out.println("Enter you're third number...");
                int tnum = y.nextInt();
                int anum = (fnum + snum + tnum);
                System.out.println("The anser is " + anum);
            }

            if (I == num[4])
            {
                Scanner y = new Scanner(System.in);
                System.out.println("Enter a number...");
                int fnum = y.nextInt();
                System.out.println("Enter you're second number...");
                int snum = y.nextInt();
                System.out.println("Enter you're third number...");
                int tnum = y.nextInt();
                System.out.println("Enter you're fourth number...");
                int ftnum = y.nextInt();
                int anum = (fnum + snum + tnum + ftnum);
                System.out.println("The anser is " + anum);
            }
        }
    }

}

and ended up with an error saying... 并以一条错误说...

Exception in thread "main" How many numbers do you want?
java.lang.ArrayIndexOutOfBoundsException: 4
    at Calculator.main(Calculator.java:20)

Array indexes always start with 0, so if you have 4 elements, last element will be 4-1 ie 3. Here you have problem: 数组索引始终以0开头,因此,如果您有4个元素,则最后一个元素将为4-1,即3。这是您遇到的问题:

    num[2] = 2;
    num[3] = 3; //This is the last element
    num[4] = 4; //This is not last element

Change your logic accordingly. 相应地更改逻辑。

您的num是一个int[4] ,这意味着它的索引范围为[0,3] ,但是您尝试访问无效的num[4]

Try this 尝试这个

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("How many numbers do you want?");

        // starts the array
        // [2] means numbers lower than 2
        int[] lnum = new int[2];

        for (int i = 0; i < lnum.length; i++) {
            int[] num = new int[4];
            num[1] = 2;
            num[2] = 3;
            num[3] = 4;

            Scanner x = new Scanner(System.in);
            int I = x.nextInt();

            if (I == num[1]) {
                Scanner y = new Scanner(System.in);
                System.out.println("Enter a number...");
                int fnum = y.nextInt();
                System.out.println("Enter you're second number...");
                int snum = y.nextInt();
                int anum = (fnum + snum);
                System.out.println("The anser is " + anum);
            }

            if (I == num[2]) {
                Scanner y = new Scanner(System.in);
                System.out.println("Enter a number...");
                int fnum = y.nextInt();
                System.out.println("Enter you're second number...");
                int snum = y.nextInt();
                System.out.println("Enter you're third number...");
                int tnum = y.nextInt();
                int anum = (fnum + snum + tnum);
                System.out.println("The anser is " + anum);

            }

            if (I == num[3]) {
                Scanner y = new Scanner(System.in);
                System.out.println("Enter a number...");
                int fnum = y.nextInt();
                System.out.println("Enter you're second number...");
                int snum = y.nextInt();
                System.out.println("Enter you're third number...");
                int tnum = y.nextInt();
                System.out.println("Enter you're fourth number...");
                int ftnum = y.nextInt();
                int anum = (fnum + snum + tnum + ftnum);
                System.out.println("The anser is " + anum);
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException: - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:-1 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException 4 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 4 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 7 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:6 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 6 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:2 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:5 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException,带数字的数组 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException , arrays with numbers 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:3 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM