没有合适的资源?快使用搜索试试~ 我知道了~
java练习题11111
资源推荐
资源详情
资源评论
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/release/download_crawler_static/89481300/bg1.jpg)
5.15 (Display the ASCII character table)
Write a program that prints the characters in the ASCII character table from ! to ~. Display ten
characters per line. The ASCII table is shown in Appendix B. Characters are separated by exactly
one space
(显示ASCII字符表)写一个程序打印ASCII字符表中的字符!~。每行显示十个字符。ASCII表如附录b所示。
字符之间仅用一个空格分隔
程序代码
这段代码中,我们定义了两个变量 startAscii 和endAscii 来指定我们要打印的ASCII字符范围(即!到~)。
然后,我们用一个for循环遍历这个范围,并使用 System.out.print 来打印每个字符,并在字符之间添加
一个空格。我们使用 if 语句来检查是否已经打印了十个字符,如果是并且当前字符不是ASCII码范围的最
后一个字符,则打印个换行符。运行这个程序,你将看到按要求格式打印的ASCII字符表,其中每行显示
十个字符。字符之间用空格分隔。
5.24 (Sum a series)
Write a program to sum the following series:
编写程序对下列数列求和:
假设我们有一个整数数组numbers,我们将遍历这个数组并对所有元素进行求和。
public class AsciiCharTablePrinter {
public static void main(String[] args) {
// 定义起始和结束的ASCII码
int startAscii = 33; // !
int endAscii = 126; // ~
// 打印ASCII字符表,每行10个字符
for (int i = startAscii; i <= endAscii; i++) {
// 打印当前字符,并在字符之间添加空格
System.out.print((char) i + " ");
// 每打印十个字符后换行
if ((i - startAscii + 1) % 10 == 0 && i != endAscii) {
System.out.println(); // 换行
}
}
// 在所有字符打印完毕后,再次换行
System.out.println();
}
}
![](https://csdnimg.cn/release/download_crawler_static/89481300/bg2.jpg)
分数数组求和(1/3,3/5……97/99)
整数数组求和
import java.math.BigDecimal;
public class FractionSum {
public static void main(String[] args) {
// 定义数组长度,确保足够长以覆盖97/99的数值范围(这里只是一个示例长度)
int length = 100; // 根据需要调整长度,确保足够长以覆盖97/99的数值范围
BigDecimal[][] fractions = new BigDecimal[length][2]; // 使用BigDecimal来
存储分数,每个子数组包含分子和分母
BigDecimal sum = BigDecimal.ZERO; // 分数总和初始化为0
// 填充数组并计算总和
for (int i = 0; i < length; i++) {
int numerator = i + 1; // 分子
int denominator = (i + 1) + 1; // 分母,确保不是0,且可以表达到97/99(在计
算中需要使用一个较大的分母)
BigDecimal fraction = new BigDecimal(numerator).divide(new
BigDecimal(denominator)); // 将分子和分母组合成一个BigDecimal分数
fractions[i][0] = fraction; // 设置分子(这里用BigDecimal存储)
fractions[i][1] = BigDecimal.valueOf(denominator); // 设置分母(这里用
BigDecimal存储)也可以不用数组来存储分母
sum = sum.add(fraction); // 将当前分数加到总和中(这里直接计算BigDecimal的
分数总和)
}
// 这里可以打印数组内容(可选),用于验证分数序列是否正确生成
// ...
// 打印分数总和(这里不展示打印完整的数组,而是展示总和)
System.out.println("分数的总和是: " + sum); // 打印分数总和(这里使用
BigDecimal来确保精度)
}
}
import java.util.Scanner;
public class SumArrayElementsDynamic {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // 创建Scanner对象来获取用户输入
System.out.print("请输入数组的长度: ");
int length = scanner.nextInt(); // 读取用户输入的数组长度
int[] numbers = new int[length]; // 创建数组
System.out.println("请输入数组的元素:");
// 循环用户输入数组元素并求和
int sum = 0; // 初始化求和变量
for (int i = 0; i < length; i++) {
numbers[i] = scanner.nextInt(); // 读取用户输入的数字并存入数组
sum += numbers[i]; // 将当前元素加到求和变量上(这里没有使用for-each循环,以
展示for循环结构)
}
// 打印数组元素的总和(实际上就是用户输入的求和结果)
![](https://csdnimg.cn/release/download_crawler_static/89481300/bg3.jpg)
5.40 (Simulation: heads or tails)
Write a program that simulates flipping a coin one million times and displays the number of
heads and tails
(模拟:正面或反面)编写一个程序,模拟抛硬币一百万次,并显示正面和反面的次数
程序代码
5.50 (Count uppercase letters)
Write a program that prompts the user to enter a string and displays the number of the
uppercase letters in the string.
(计数大写字母)编写一个程序,提示用户输入一个字符串,并显示该字符串中大写字母的个数。
System.out.println("数组元素的和是: " + sum); // 打印结果
scanner.close(); // 关闭Scanner对象以释放资源
}
}
import java.util.Random;
public class CoinTossSimulation {
public static void main(String[] args) {
int totalTosses = 1000000; // 抛硬币一百万次
int headsCount = 0; // 正面次数
int tailsCount = 0; // 反面次数
Random random = new Random(); // 创建Random对象
for (int i = 0; i < totalTosses; i++) {
// 使用Random类随机生成0(代表反面)或1(代表正面)
int result = random.nextInt(2); // 生成0或1
if (result == 0) {
tailsCount++; // 如果结果是0,增加反面次数
} else {
headsCount++; // 如果结果是1,增加正面次数
}
}
// 显示结果
System.out.println("抛硬币一百万次的结果:");
System.out.println("正面次数: " + headsCount);
System.out.println("反面次数: " + tailsCount);
}
}
![](https://csdnimg.cn/release/download_crawler_static/89481300/bg4.jpg)
程序代码
7.10 (Find the index of the smallest element)
Write a method that returns the index of the smallest element in an array of integers. If the
number of such elements is greater than 1, return the smallest index. Use the following header:
public static int indexOfSmallestElement(double[] array)
Write a test program that prompts the user to enter ten numbers, invokes this method to return
the index of the smallest element, and displays the index.
(查找最小元素的索引)编写一个方法,返回整数数组中最小元素的索引。如果这类元素的数量大于1,则
返回最小的索引。使用下面的标题:
public static int indexOfSmallestElement(double[] array)
编写一个测试程序,提示用户输入十个数字,调用该方法返回最小元素的索引,并显示该索引。
indexOfSmallestElement方法
import java.util.Scanner;
public class CountUppercaseLetters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 提示用户输入字符串
System.out.print("Enter a string: "); //请输入一个字符串
String input = scanner.nextLine();
// 计算大写字母的个数
int count = 0;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (Character.isUpperCase(c)) {
count++;
}
}
// 显示大写字母的个数
System.out.println("The number of uppercase: " + count); //字符串中大写字
母的个数是:
// 关闭scanner
scanner.close();
}
}
剩余17页未读,继续阅读
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/7d4c016aea164454825c0fe830214182_jwfish_007.jpg!1)
编程浪子007
- 粉丝: 31
- 资源: 9
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)