第一行包含一个整数 p,表示数组的长度。第二行包含用空格分隔的整数,描述数组中的每个相应元素。第三行打印一个整数,表示负数组的数量。package asgn3;import java.util.*;public class Asgn3 { public static void main(String[] args) { int count = 0, result = 0; Scanner in = new Scanner(System.in); System.out.println("Enter the array "); String s = in.nextLine(); int j = 0; String[] s1 = s.split(" "); int a[] = new int[s1.length]; for(String s2:s1) { a[j] = Integer.parseInt(s2)); j++; } for (int i = 0; i < a.length; i++) { for ( j = i; j < a.length; j ++) { for (int k = i; k <= j; k++) { result += a[k]; } if(result < 0) count ++; } System.out.println("no. of negatve arrays is "+count); } }}
2 回答

慕桂英4014372
TA贡献1871条经验 获得超13个赞
问题是使用了额外的不必要的括号。改变,
a[j]=Integer.parseInt(s2));
和
a[j]=Integer.parseInt(s2);
添加回答
举报
0/150
提交
取消