Java核心技术前四章 习题

这篇博客包含了Java编程的基础知识和习题,涉及变量命名、数据类型、运算符、流程控制、类与对象等方面,旨在帮助读者巩固Java核心概念。
摘要由CSDN通过智能技术生成

前四章 单元测验

String str2 = 3.5F + "";
System.out.println(str2); // 3.5
System.out.println(3 + 4 + "hello!"); // 7hello!
System.out.println("hello!" + 3 + 4); // hello34
System.out.println(3 + 4 + "hello!"); // 7hello!
System.out.println('a' + 1 + "hello"); // 98hello
System.out.println("hello!" + 'a' + 1); // hello!a1
int x = 5, y = 6, z;
z = ++x + y++; // 6 + 6
System.out.println(z); // 12
z = ++x + x++; // 7 + 7
System.out.println(z); // 14
x = ++x + x++; // 9 + 9
System.out.println(x); // 18
z = x > y ? ++x : y++; // 7 = 18 > 7 ? 19 : 7
System.out.println(y); // 7
System.out.println(x); // 19
System.out.println(z); // 19

int a = 1;
System.out.println(++a + a); // 2 + 2 = 4

int i = 0;
int j = i++;
int k = --i;
System.out.println(i + j + k); // 0
// 需要在main函数的输入参数中设置5,输出5*5的数字方格。如果是输入7,则是7*7的数字方格。
//	    1    2    3    4    5
//	    6    7    8    9   10
//	   11   12   13   14   15
//	   16   17   18   19   20
//	   21   22   23   24   25
	Scanner sc = new Scanner(System.in);
	int c = sc.nextInt();
	for (int j = 1; j <= c * c; j++) {
   
		System.out.printf("%5d", j);
		if (j % c == 0) {
   
			System.out.println();
		}
	}

需要在main函数的输入参数中设置5,输出5层星塔。如果是输入7,则是7层星塔。

	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	for (int i = 0; i < n; i++) {
   
		// 5/2-0=2
		// 5/2-1=1
		// 5/2-2=0
		// 5/2-3=1
		// 5/2-4=2
		for (int space = Math.abs(n / 2 - i); space > 0; space--) {
   
			System.out.print(" "
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值