第一行包含一個整數 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
提交
取消