我给你一个java题目帮我解答一下:使用JAVA语句进行整数输入输出与简单运算Write programs that read a sequence of integer inputs and print a. The smallest and largest of the inputs. b. The number of even and odd inputs. c. Cumulative totals. For example, if the input is 1 7 2 9, the program should print 1 8 10 19. d. All adjacent duplicates. For example, if the input is 1 3 3 4 5 5 6 6 6 2, the program should print 3 5 6.
时间: 2023-03-14 18:00:29 浏览: 129
答案:import java.util.Scanner;
public class IntegerInputOutput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int evenCount = 0;
int oddCount = 0;
int total = 0;
System.out.print("请输入一个整数序列:");
while (sc.hasNextInt()) {
int num = sc.nextInt();
//求最小值
if (num < min)
min = num;
//求最大值
if (num > max)
max = num;
//求偶数个数
if (num % 2 == 0)
evenCount++;
//求奇数个数
if (num % 2 != 0)
oddCount++;
//求累加和
total += num;
}
System.out.println("最小值:" + min);
System.out.println("最大值:" + max);
System.out.println("偶数个数:" + evenCount);
System.out.println("奇数个数:" + oddCount);
System.out.println("累加和:" + total);
//求所有相邻重复的数字
int currNum = 0;
int preNum = 0;
System.out.print("所有相邻重复的数字:");
while (sc.hasNextInt()) {
currNum = sc.nextInt();
if (preNum == currNum)
System.out.print(currNum + " ");
preNum = currNum;
}
}
}