JAVA期末速成库(5)第五章

一、习题介绍

第五章

Programming Exercise:5.15,5.24,5.40,5.50

二、习题及答案

*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.

*5.15(显示ASCII字符表)

写一个程序打印ASCII字符表中的字符!~.每行显示十个字符.

ASCII表如附录b所示.字符之间仅用一个空格分隔.

public class AsciiTable {

    public static void main(String[] args) {

        for (int i = 33; i <= 126; i++) {

            System.out.print((char) i + " ");

            if ((i - 32) % 10 == 0) {

                System.out.println();

            }

        }

    }

}

运行结果 

*5.24 (Sum a series)编写程序对下列数列求和:

public class SumSeries {
    public static void main(String[] args) {
        double sum = 0.0;

        for (int i = 1, j = 3; i <= 97 && j <= 99; i += 2, j += 2) {
            sum += (double) i / j;
        }

        System.out.println("The sum of the series is: " + sum);
    }
}

运行结果  

​​​​​​​​​​​​​​

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.40(模拟:正面或反面)编写一个模拟掷硬币的程序百万次,显示正面和反面的次数。

public class CoinFlipSimulator {

    public static void main(String[] args) {

        final int FLIPS = 1000000;

        int headsCount = 0;

        int tailsCount = 0;

        

        for (int i = 0; i < FLIPS; i++) {

            if (Math.random() > 0.5) {

                headsCount++;

            } else {

                tailsCount++;

            }

        }

        

        System.out.println("Heads: " + headsCount);

        System.out.println("Tails: " + tailsCount);

    }

}

运行结果  

*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

*5.50 (Count uppercase letters)编写一个程序,提示用户输入一个字符串,并显示该字符串中大写字母的个数

import java.util.Scanner;



public class UppercaseCounter {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入一个字符串:");

        String input = scanner.nextLine();

        scanner.close();

        

        int uppercaseCount = 0;

        for (int i = 0; i < input.length(); i++) {

            if (Character.isUpperCase(input.charAt(i))) {

                uppercaseCount++;

            }

        }

        

        System.out.println("大写字母的个数是:" + uppercaseCount);

    }

}

运行结果  

 结语  

有风有雨是人生的常态

迎难而上才能逆风翻盘

!!!

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT 青年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值