Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java数组运行不正常? import java.util.Scanner; 公共类测试{ 静态扫描仪kb=新扫描仪(System.in); 公共静态void main(字符串参数[]){ System.out.println(“这就是你应该如何格式化你的方程=>3.0x5.5y=9.0”); System.out.println(“键入第一个等式:”); String first=kb.nextLine(); //第一方程 扫描仪1=新扫描仪(第一台); //第一台扫描仪 double[]arr=新的双精度[6]; //包含数字的arr 字符串[]变量=新字符串[2]; //包含变量名的arr System.out.println(“键入第二个等式:”); 字符串second=kb.nextLine(); //第二方程 扫描器2=新扫描器(秒); //二次扫描 一个.useDelimiter(“[^\\p{Alnum},\\\.-]”); 使用分隔符(“[^\\p{Alnum},\\.-]”); //除了字母数字字符以外的任何字符, //跳过逗号、点或负号 对于(int i=1;i_Java_Arrays_For Loop_Java.util.scanner_Indexoutofboundsexception - Fatal编程技术网

Java数组运行不正常? import java.util.Scanner; 公共类测试{ 静态扫描仪kb=新扫描仪(System.in); 公共静态void main(字符串参数[]){ System.out.println(“这就是你应该如何格式化你的方程=>3.0x5.5y=9.0”); System.out.println(“键入第一个等式:”); String first=kb.nextLine(); //第一方程 扫描仪1=新扫描仪(第一台); //第一台扫描仪 double[]arr=新的双精度[6]; //包含数字的arr 字符串[]变量=新字符串[2]; //包含变量名的arr System.out.println(“键入第二个等式:”); 字符串second=kb.nextLine(); //第二方程 扫描器2=新扫描器(秒); //二次扫描 一个.useDelimiter(“[^\\p{Alnum},\\\.-]”); 使用分隔符(“[^\\p{Alnum},\\.-]”); //除了字母数字字符以外的任何字符, //跳过逗号、点或负号 对于(int i=1;i

Java数组运行不正常? import java.util.Scanner; 公共类测试{ 静态扫描仪kb=新扫描仪(System.in); 公共静态void main(字符串参数[]){ System.out.println(“这就是你应该如何格式化你的方程=>3.0x5.5y=9.0”); System.out.println(“键入第一个等式:”); String first=kb.nextLine(); //第一方程 扫描仪1=新扫描仪(第一台); //第一台扫描仪 double[]arr=新的双精度[6]; //包含数字的arr 字符串[]变量=新字符串[2]; //包含变量名的arr System.out.println(“键入第二个等式:”); 字符串second=kb.nextLine(); //第二方程 扫描器2=新扫描器(秒); //二次扫描 一个.useDelimiter(“[^\\p{Alnum},\\\.-]”); 使用分隔符(“[^\\p{Alnum},\\.-]”); //除了字母数字字符以外的任何字符, //跳过逗号、点或负号 对于(int i=1;i,java,arrays,for-loop,java.util.scanner,indexoutofboundsexception,Java,Arrays,For Loop,Java.util.scanner,Indexoutofboundsexception,,如您所见,vars是一个大小为2的数组: import java.util.Scanner; public class Maintest { static Scanner kb = new Scanner(System.in); public static void main (String args[]) { System.out.println("This is how you should format your equation => 3.0 x

,如您所见,
vars
是一个大小为2的数组:

import java.util.Scanner;

public class Maintest {
    static Scanner kb = new Scanner(System.in);
    public static void main (String args[]) {
        System.out.println("This is how you should format your equation => 3.0 x + 5.5 y = 9.0");
        System.out.println("Type your first equation:");
        String first = kb.nextLine();
        //first equation
        Scanner one = new Scanner(first);
        //scanner for first
        double[] arr = new double[6];
        //arr containing numbers
        String[] vars = new String[2];
        //arr containing variable names
        System.out.println("Type your second equation:");
        String second = kb.nextLine();
        //second equations
        Scanner two = new Scanner(second);
        //scanner for second
        one.useDelimiter("[^\\p{Alnum},\\.-]");
        two.useDelimiter("[^\\p{Alnum},\\.-]");
        //anything other than alphanumberic characters,
        //comma, dot or negative sign is skipped
        for (int i = 1; i <= 3; i++) {
            if (one.hasNextDouble())
                arr[i-1] = one.nextDouble();
            else if (one.hasNext())
                vars[i-1] = one.next();
            else if (two.hasNextDouble())
                arr[i+2] = one.nextDouble();
            else if (arr[i-1] == 0.0)
                arr[i-1] = 1.0;
            //putting values into array
        }
        System.out.println(arr[3]);
        System.out.println(vars[2]);
    }
}
由于Java从零开始索引,这意味着数组有两个字符串的“空间”:
vars[0]
vars[1]
。超出该范围的任何内容都是不允许的,这就是为什么会出现错误:

String[] vars = new String[2];

您还应该确保您没有试图访问print语句前面的循环中的
vars[2]

在调试程序中逐步查看代码时,您看到了什么?
System.out.println(vars[2]); //vars[2] would be the third String, but vars's length is only two!