Java语言程序设计(基础篇)第10版第五章答案

本节内容涵盖了Java编程中的条件表达式、异或操作、条件语句的使用,强调了break语句在循环控制中的作用。还讨论了字符输出、整数因子问题的解决思路,提倡使用英文注释以提高代码可读性。此外,提到了printf格式输出的重要性,制表符的应用,以及调试程序的技巧,特别是如何避免被固定思维模式束缚。
摘要由CSDN通过智能技术生成

5.10

public class Test {
    public static void main(String[] args){
     
        //程序说明:输出100-1000之间能同时被5和6整除的整数
        int count = 1;
        for (int i = 100; i <= 1000; i++) {
            if(i % 5 == 0 && i % 6 == 0)
                System.out.print((count++ % 10 != 0) ? i + " ": i + "\n");
        }
    }
}

总结:

  • 条件表达式
  • 输出格式:最后一个数字后不带空格

5.11

public class Test {
	public static void main(String[] args){
	
		//程序说明:输出100-200之间能被5或6整除,但不能同时被5和6整除的整数
		int count = 1;
		for (int i = 100; i <= 200; i++) {
			if((i % 5 == 0 ^ i % 6 == 0))
				System.out.print((count++ % 10 != 0) ? i + " ": i + "\n");
		}
	}
}

总结:

  • 异或的使用

5.14

public class Test {
	public static void main(String[] args){
	
		//程序说明:找最大公约数
		//提示用户输入两个正整数
		System.out.print("Enter the two number:");
		Scanner input = new Scanner(System.in);
		int n1 = input.nextInt();
		int n2 = input.nextInt();
		//找到并显示最大公约数
			//找到更小的那个数
//		int s = 0;
//		if(n1 < n2)
//			s = n1;
//		else
//			s = n2;
		int s = (n1 < n2) ? n1 : n2;
		while(s > 0)
		{
			if(n1 % s == 0 && n2 % s == 0)
			 break;
			s--;
		}
		System.out.print("GCD of " + n1 + " and " + n2 + " is " + s);
	}
}

总结:

  • 条件表达式的使用
  • break是跳出这个循环,并且是跳出与其接触最密切的一个循环

5.15

public class Test {
	public static void main(String[] args){
	
		//程序说明:打印ASCII字符
		int count = 1;
		for (int i = '!'; i <= '~'; i++) {
			System.out.print((count++ % 10 != 0) ? (char)i + " " : (char)i + "\n");
		}
	}
}

总结:

  • 打印字符:强制转换成char类型
  • 输出格式:最后一个数字后不带空格

5.16

public class Test {
	public static void main(String[] args){
	
		//程序说明:升序输出一个数的所有因子
		//提示用户输入整数
		System.out.print("Enter a positive integer:");
		Scanner input = new Scanner(System.in);
		int a = input.nextInt();
		//输出因子
		int f = 2;
		while(f <= a)
		{
			if(a % f == 0)
			{
				System.out.println(f);
				a = a / f;
			}
			else
				f++;
		}
	}
}

总结:

  • 整数的因子问题的思考

5.17

public class Test {
	public static void main(String[] args){
	
		
		//Prompt the user to enter the number of lines
		System.out.print("Enter the number of lines:");
		Scanner input = new Scanner(System.in);
		int lines = input.nextInt();
		//The limits of lines
		if(lines <= 1 || lines >= 15)
		{
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值