java输入x和y相减,Java基础方面的陷阱(1)

01. 找奇数 ★★★☆☆

不能用 i % 2 == 1 来当条件,因为需要考虑负数。

判断一个数是否为奇数的条件应该是 “不能整除”。

而不是 除了2之后 等于 1。

public static boolean isOdd(int i){

return i % 2 != 0; // “不能整除”

}

public static void main(String[] args){

syso(isOdd(-1)); // true

syso(isOdd(0)); // false

syso(isOdd(1)); // true

syso(isOdd(2)); // false

}

02. 浮点数相减 ★★★★★

public static void main(String[] args){

System.out.println(2.0 - 1.1); // = 0.89999999  不符合需求,应该是0.9才对

System.out.printf("%.1f", 2.0 - 1.1); // = 0.9  JDK API特性使然

BigDecimal value = new BigDecimal("2.0")

.subtract(new BigDecimal("1.1"));

syso(value);

}

03. 长整除 ★★★★★

public static void main(String[] args){

final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; // 微秒

final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; // 毫秒

syso(MICROS_PER_DAY / MILLIS_PER_DAY); // = 5 错误 应该是1000

/*

java语法中 对于乘法两端的常数 24和60 会默认为int类型

这样一直乘下去 就会出现越界情况

正确的解决方案应该是:

强调其为Long类型的数据

*/

final long MICROS_PER_DAY = 24L * 60 * 60 * 1000 * 1000; // 微秒

final long MILLIS_PER_DAY = 24L * 60 * 60 * 1000; // 毫秒

syso(MICROS_PER_DAY / MILLIS_PER_DAY); // = 1000

}

04. 互换内容 ★★☆☆☆

public static void main(String[] args){

// 不需要定义temp变量 实现数值互换

int x = 5;

int y = 8;

x = x ^ y; // 第一次 x = 13 ,y = 8 不变

y = x ^ y; // 第二次 x = 13 , y = 5, 原理为 一个数异或同一个数两次 结果还是原来的值

x = x ^ y; // 第三次 x = 8, y = 5 依旧为5

syso("x = " + x + ", y = " + y); // x=0, y=1984

}

05. 字符串和字符 ★★★★★

public static void main(String[] args){

syso("H" + "a"); // Ha

syso('H' + 'a'); // 169

/*

由于char类型的数据在加减乘除运算中,

会自动提升为int类型的数据。

使用ascii表来存储char类型数据具体的值。

所以就会变成

'H' = 72

'a' = 97

所以等于 169

*/

}

06. 字符数组 ★★★★☆

public static void main(String[] args){

String letters = "ABC";

char[] numbers = {'1','2','3'};

System.out.println(letters + "easy as " + numbers);  // ABC easy as [C@de6ced

System.out.println(numbers); // 123

/*

println()该方法为重构,可以传递String类型参数,

也可传递其它类型参数。

当与字符串拼接的时候,println()方法默认传递的参数为字符串类型。

那么,数组会调用自身的toString()方法,即打印该数组的哈希码

如果println()中传递的参数为char[]数组,

默认将一个字节数组中的所有数,一一打印出来,便成了 123.

*/

}

07. 转义字符 ★★☆☆☆

public static void main(String[] args){

// \u0022 是双引号的Unicode转义字符

System.out.println("a\u0022.length()+\u0022b".length());// = 2

// 说白了 就是 "a".length() + "b".length() = 2

}

08. 打印输出类名 ★★☆☆☆

public static void main(String[] args){

System.out.println(

MyClass.class.getName()

.replaceAll(".","/") + ".class"

);

// 特殊字符必须转义 不然输出结果会事与愿违u

System.out.println(

MyClass.class.getName()

.replaceAll("\\.","/") + ".class"

);

}

09. 随机数问题 ★★★★☆

public class RandomTest{

private static Random rnd = new Random();

public static void main(String[] args){

StringBuffer word = null;

switch(rnd.nextInt(2)){

case 1: word = new StringBuffer('P');

break;

case 2: word = new StringBuffer('G');

break;

default: word = new StringBuffer('M');

}

word.append('a');

word.append('i');

word.append('n');

System.out.println(word); // 输出结果为: ain

/*

该题有3个知识点

1. case语句后面一定要加上break。

2. char类型的数据会自动提升为int类型,所以需要加上双引号,

将其改为字符串数据类型。

3. rnd.nextInt(2) 得出的结果是 0到2 不包含2

也就是只有 0 1

所以就算都改正了,但是Gain也一定不会出现的,因为条件不允许。

switch(rnd.nextInt(2)){

case 1: word = new StringBuffer("P");

break;

case 2: word = new StringBuffer("G");

break;

default: word = new StringBuffer("M");

}

*/

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值