9.27课后作业

public class EnumTest {

public static void main(String[] args) {
	Size s=Size.SMALL;
	Size t=Size.LARGE;
	//s和t引用同一个对象?
	System.out.println(s==t);  //
	//是原始数据类型吗?
	System.out.println(s.getClass().isPrimitive());
	//从字符串中转换
	Size u=Size.valueOf("SMALL");
	System.out.println(s==u);  //true
	//列出它的所有值
	for(Size value:Size.values()){
		System.out.println(value);
	}
}

}
enum Size{SMALL,MEDIUM,LARGE};
image
false
false
true
SMALL
MEDIUM
LARGE

package 测试;

import java.math.BigDecimal;

public class a
{
public static void main(String[] args)
{
BigDecimal f1 = new BigDecimal("0.05");
BigDecimal f2 = BigDecimal.valueOf(0.01);
BigDecimal f3 = new BigDecimal(0.05);
System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:");
System.out.println("0.05 + 0.01 = " + f1.add(f2));
System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
System.out.println("0.05 / 0.01 = " + f1.divide(f2));
System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:");
System.out.println("0.05 + 0.01 = " + f3.add(f2));
System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
System.out.println("0.05 / 0.01 = " + f3.divide(f2));
}
}

下面使用String作为BigDecimal构造器参数的计算结果:
0.05 + 0.01 = 0.06
0.05 - 0.01 = 0.04
0.05 * 0.01 = 0.0005
0.05 / 0.01 = 5
下面使用double作为BigDecimal构造器参数的计算结果:
0.05 + 0.01 = 0.06000000000000000277555756156289135105907917022705078125
0.05 - 0.01 = 0.04000000000000000277555756156289135105907917022705078125
0.05 * 0.01 = 0.0005000000000000000277555756156289135105907917022705078125
0.05 / 0.01 = 5.000000000000000277555756156289135105907917022705078125

以下代码的输出结果是什么?
int X=100;
int Y=200;
System.out.println("X+Y="+X+Y);
System.out.println(X+Y+"=X+Y");
为什么会有这样的输出结果?
X+Y=100200
300=X+Y
"X+Y=" + X + Y表达式从左到右结合,Java 中的 + 运算符在字符串和数字之间时,只要有一个是字符串,就会执行字符串拼接。
X + Y + "=X+Y"同样从左到右结合,但前两个是整数,先进行数值加法

一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做。二柱立马就想到写一个小程序来做这件事。 这个事情可以用很多语言或者工具来实现: Excel, C/C++, C#, VB, Unix Shell, Emacs, Powershell/Vbscript, Javascript, Perl, Python, …
import java.util.;
public class a {
static final Random R = new Random();
public static void main(String[] args) {
for (int i = 1; i <= 30; i++) {
System.out.println(i + ". " + makeOne());
}
}
private static String makeOne() {
int a, b, op;
while (true) {
a = 1 + R.nextInt(100);
b = 1 + R.nextInt(100);
op = R.nextInt(4); // 0+ 1- 2
3/
if (op == 1 && a < b) continue; // 保证结果非负
if (op == 3 && a % b != 0) continue; // 整除
break;
}
char c = "+-*/".charAt(op);
return "(" + a + ") " + c + " (" + b + ") =";
}
}

posted @ 2025-09-27 23:08  muyuxiaxing  阅读(8)  评论(0)    收藏  举报