Java 数组因某种原因超出范围

Java 数组因某种原因超出范围,java,Java,我正在尝试编写一个程序,从用户那里获取一个int字符串,打印出包含多少正数和负数,并在读取0时停止。如果仅输入0,则应打印“除0外,未输入任何数字” 下面是我的代码: import java.util.Scanner; public class Ex4_1 { public static void main(String[]args) { Scanner input = new Scanner(System.in); System.out.pr

我正在尝试编写一个程序,从用户那里获取一个int字符串,打印出包含多少正数和负数,并在读取0时停止。如果仅输入0,则应打印“除0外,未输入任何数字” 下面是我的代码:

import java.util.Scanner;
public class Ex4_1
{
    public static void main(String[]args)
    {
         Scanner input = new Scanner(System.in);
         System.out.println("Enter an integer, the input ends if it is 0 (use commas in between numbers):");
         String str = input.nextLine();
         int x = str.length();
         int positive = 0;
         int negative = 0;
         for(int i = 0; i< x; i++)
         {
             int w = 0;
             String[] num = new String[x];
             num = str.split(",");
             if(d1 > 0)
             { 
                 positive += 1;
             }
             else if(d1 < 0)
             {
                 negative += 1;
             }
             else if(d1 == 0)
             {
                 System.out.println("No numbers entered except 0.");
             }
         }

         System.out.println("The number of positives is " + positive );
         System.out.println("The number of negatives is " + negative);
    }
}
import java.util.Scanner;
公共类Ex4_1
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入一个整数,如果它是0,则输入结束(在数字之间使用逗号):”;
String str=input.nextLine();
int x=str.length();
int正=0;
int负=0;
对于(int i=0;i0)
{ 
正+=1;
}
否则如果(d1<0)
{
负+=1;
}
否则如果(d1==0)
{
System.out.println(“除0外,未输入任何数字”);
}
}
System.out.println(“正数为”+正数);
System.out.println(“负片数为”+负片);
}
}
d1=Integer.parseInt(num[i])

import java.util.Scanner;
公共类Ex4_1
{  
公共静态void main(字符串[]args)
{  
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入一个整数,如果它是0,则输入结束(在数字之间使用逗号):”;
String str=input.nextLine();
String[]num=新字符串[x];
num=str.split(“,”);
int x=数值长度();
int正=0;
int负=0;
如果(x==1&&num[0]==0”)
System.out.println(“除0外,未输入任何数字”);
否则{
对于(int i=0;i0)
{   
正+=1;
}  
否则如果(d1<0)
{  
负+=1;
}  
否则如果(d1==0)
{  
打破
}  
}   
System.out.println(“正数为”+正数);
System.out.println(“负片数为”+负片);
}}  
}  

根据您的问题,这应该是正确的代码

等等,什么?
str.length()。您根本没有解析这些数字。如果
s,则
d1
变量(在
if
s中)是什么?此代码中没有
d1
的定义。这不足以产生ArrayIndexOutOfBoundsException。此数组只是在不使用它的情况下转储:
new String[x]
import java.util.Scanner;  
    public class Ex4_1  
    {  
          public static void main(String[]args)  
        {  
             Scanner input = new Scanner(System.in);  
             System.out.println("Enter an integer, the input ends if it is 0         (use commas in between numbers):");  
         String str = input.nextLine(); 
 String[] num = new String[x];  
         num = str.split(","); 
         int x = num.length();  
     int positive = 0;  
     int negative = 0;
if(x==1 && num[0]=="0")  
System.out.println("No numbers entered except 0.");  
else {    
     for(int i = 0; i< x; i++)  
     {  
         int w = 0;  
         if(d1 > 0)  
         {   
             positive += 1;  
         }  
         else if(d1 < 0)  
         {  
             negative += 1;  
         }  
         else if(d1 == 0)  
         {  
             break;  
         }  
     }   
     System.out.println("The number of positives is " + positive );  
     System.out.println("The number of negatives is " + negative);  
    }}  
}