收集面试题(四)(输出测试)

本文通过四个具体的Java编程实例,详细展示了for循环、字符串处理、变量递增及位操作的使用方法与注意事项。每个实例均附有完整的代码示例与运行结果,便于读者理解和实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.测试for循环的输出:

public class TestOutput {

	public static void main(String[] args) {

		int i = 0;
		for (foo('A'); foo('B') && (i < 3); foo('C')) {
			i++;
			foo('D');
		}
	}

	static boolean foo(char c) {
		System.out.print(c);
		return true;
	}
}

 

输出结果为:

ABDCBDCBDCB

 

2.测试split的输出

public class TestOutput {

	public static void main(String[] args) {

		String info = "abcd|123||7899|sdfg";

		for (String string : getString(info)) {
			System.out.println(string);
		}
	}

	public static String[] getString(String info) {

		if (info == null) {
			throw new NullPointerException("the info is null.");
		}

		String[] array = info.split("\\|");
		return array;
	}
}

 

输出结果为:

abcd
123

 

7899
sdfg

 

3.测试i++的输出:

public class TestOutput {

	public static void main(String[] args) {

		int i = 2;
		i = i++;
		int j = i++;
		System.out.println(i + ":" + j);

	}
}

 

输出结果为:

3:2

 

4.测试~的输出:

public class TestOutput {

	public static void main(String[] args) {

		int i = 0xFFFFFFFA;
		int j = ~i;
		System.out.println(i);
		System.out.println(j);
		System.out.println(~~i);

		int t = 8;
		System.out.println(~t);

		int k = -9;
		System.out.println(~k);

		System.out.println(String.format("%1$04d", Integer.valueOf("0003")));
	}
}

 

输出结果为:

-6
5
-6
-9
8
0003

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值