确定此数组中符号更改的次数。 例如,在数组 1,-34, 8, 14, -5,-8,-78, 3 中,符号改变了 4 次。
3条回答 默认 最新
- limit、T 2022-08-09 10:57关注
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = 0; Integer last = null; while (true) { try { System.out.println("请输入一个数字(输入其他任意字符退出):"); String s = sc.nextLine(); Integer tmp = Integer.parseInt(s); if (last != null) { int r = tmp * last; if (r < 0) { count++; } } last = tmp; }catch (Exception e) { break; } } System.out.println(count); }
本回答被题主和专家选为最佳回答 , 对您是否有帮助呢?解决 1无用