Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.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 未正确添加数组正数_Java_Arrays_Recursion - Fatal编程技术网

Java 未正确添加数组正数

Java 未正确添加数组正数,java,arrays,recursion,Java,Arrays,Recursion,我有一个接受数字的数组。我的方法之一是计算数组中的正数。因此,如果他们输入2 3 4 5 6和0来终止程序,它应该打印出正数:5,但它会打印出正数:4。它错过了最后一个号码。但是,如果我执行23445-1400{0},它会打印出正确数量的正数,在本例中是5。我已经做了一些调试,但似乎没有弄明白。有什么帮助吗 public static void main (String args[]) throws IOException { int i = 0; int []

我有一个接受数字的数组。我的方法之一是计算数组中的正数。因此,如果他们输入2 3 4 5 6和0来终止程序,它应该打印出正数:5,但它会打印出正数:4。它错过了最后一个号码。但是,如果我执行23445-1400{0},它会打印出正确数量的正数,在本例中是5。我已经做了一些调试,但似乎没有弄明白。有什么帮助吗

    public static void main (String args[]) throws IOException
    {
    int i = 0;
    int [] nums;
    nums = new int [100];

    InputStreamReader inRead = new InputStreamReader(System.in);   // 
    BufferedReader buffRead = new BufferedReader(inRead);
    String line = buffRead.readLine();



        while (line.equals("0") == false && i<100)      
        {       
            i++;        
            line = buffRead.readLine();     
            nums[i]=(int) Double.parseDouble(line);     

        }




    System.out.print ("The minimum number is " + findMin (nums, 0, nums.length - 1) + ('\n'));
    System.out.println("Sum of the numbers at odd indices: " + computeSumAtOdd(nums, 0 , nums.length -1 ) + ('\n') );
    System.out.println("The total count of positive numbers is  " + countPositive(nums,0,nums.length -1));
}

 public static int countPositive(int[] numbers, int startIndex, int endIndex)
 {   
     if (startIndex == endIndex) 
     {   
         if (numbers[startIndex] > 0)        
         {   
              return 1;
         }   
         else
              return 0;      
     } else
     {       
       if (numbers[startIndex] > 0)        
       {       
        return 1 + countPositive(numbers, startIndex +1, endIndex); 
       }
       else        
        return countPositive(numbers, startIndex +1, endIndex);     
    }
 } 
publicstaticvoidmain(字符串args[])引发IOException
{
int i=0;
int[]nums;
nums=新整数[100];
InputStreamReader inRead=新的InputStreamReader(System.in);//
BufferedReader buffRead=新的BufferedReader(读取中);
String line=buffRead.readLine();
while(line.equals(“0”)==false&&i 0)
{   
返回1;
}   
其他的
返回0;
}否则
{       
如果(数字[startIndex]>0)
{       
返回1+count正值(数字、开始索引+1、结束索引);
}
其他的
返回countPositive(数字,开始索引+1,结束索引);
}
} 

您在此处向我们展示的代码有效且有效

在调用此函数之前,请尝试输出数组中的值,以查看它们是什么

例如,如果您的输入是一个名为numbers的数组,请在代码前面输入:

for ( int i=0; i < numbers.length; i++)
{
    System.out.println(numbers[i]);
}
并确保没有用错误的输入切断任何东西。

在代码中

String line = buffRead.readLine();

while (line.equals("0") == false && i < 100) {
    i++;
    line = buffRead.readLine();
    nums[i] = Integer.parseInt(line);
}

输入的第一个数字将不包括在数组中(请参阅我的嵌入注释)


String line=buffRead.readLine()//这个代码看起来不错。您确定输入数组的代码正确吗?另外,如果有大括号和没有大括号混合在一起,这是一个非常糟糕的编码实践,要么做一个,要么做另一个,而不是两个都做。我感觉你的endIndex计算不正确。查看对it.System.out.println的调用(“正数的总数是”+countPositive(nums,0,nums.length-1));正如我告诉你的,这段代码似乎运行良好。你的问题可能在别的地方。您能告诉我们您是如何从用户处读取和存储数据并使用此方法的吗?第一次进入函数的值是什么?
String line = buffRead.readLine();

while (line.equals("0") == false && i < 100) {
    i++;
    line = buffRead.readLine();
    nums[i] = Integer.parseInt(line);
}
String line = buffRead.readLine();

while (line.equals("0") == false && i < 100) {
    nums[i] = Integer.parseInt(line);
    i++;
    line = buffRead.readLine();
}
String line = buffRead.readLine(); //<--read a line...

while (line.equals("0") == false && i < 100) {
    i++;
    line = buffRead.readLine(); <-- read another line, what happened to the last line?
    nums[i] = Integer.parseInt(line);
}
System.out.println("The total count of positive numbers is  " + countPositive(nums, 0, nums.length - 1));