gpt4 book ai didi

java(程序如何查找并打印总和和乘积。Ex.4)

转载 作者:行者123 更新时间:2023-12-02 08:29:18 25 4
gpt4 key购买 nike

练习 #3:编写一个程序,读取 3 个整数值,然后用户可以以小写或大写字母输入排序顺序偏好(即,a 或 A 表示升序,d 或 D 表示降序)。输入/输出示例:输入3个数字:7 8 3输入排序顺序(a 或 A 表示升序,d 或 D 表示降序):d8 7 3

练习 #4:编写一个程序来打印数字 5、10、15、20 … 100。该程序还应该查找并打印它们的总和和乘积。

最佳答案

Exercise #3: Write a program that reads 3 integer values

我将从创建一个 Scanner 开始从 System.in 读取输入:

Scanner s = new Scanner(System.in);

可以使用 nextInt() 方法读取这三个整数。

int i = s.nextInt();
int j = ...

and then the user may input the sorting order preference in small or capital letters (i.e., a or A for ascending order, d or D for descending order).

例如从用户那里读取一个字符

char c = s.nextLine().charAt(0);

并使用类似的东西进行比较

if (c == 'A') {
// ascending
} else if (c == 'D') {
// descending
}

我会将给定的整数放入数组中 (int[] anArray = { i, j, k };),然后使用 Arrays.sort .

鉴于您只有三个元素,反转数组应该足够简单。

关于java(程序如何查找并打印总和和乘积。Ex.4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3811985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com