课程动手动脑练习
public class Main {
private enum MyEnum {
ONE, TWO, THREE
}
public static void main(String[] args) {
for (MyEnum value : MyEnum.values()) {
System.out.println(value);
}
}
}运行结果为,ONE TWO THREE
结构:enum 名称{ 数据1,数据二..... }
遍历: for (名称 value : 名称.values())
{
System.out.println(value);
}类似于增强for
public class Main {
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
//列出它的所有值{SMALL,MEDIUM,LARGE}
for (Size value : Size.values()) {
System.out.println(value);
}
}
enum Size{SMALL,MEDIUM,LARGE};
}
public class Main {
public static void main(String[] args) {
System.out.println("0.05 + 0.01 = " + (0.05+0.01));
System.out.println("1.0 - 0.42 = "+(1.0-0.42));
System.out.println("4.015 * 100 = "+(4.015*100));
System.out.println("123.3 / 100 = "+(123.3/100));
}
}计算结果有误差
原因 与计算机的二进制存储方式有关
可以用 IntegerDouble或者BigDemical传递字符串解决
import java.math.BigDecimal;
public class TestBigDecimal
{
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));
}
}
public class Main {
public static void main(String[] args) {
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
字符串的拼接先后顺序
public class Main {
public static void main(String[] args) {
//定义一个空字符串
String result = "";
//进行6次循环
for(int i = 0 ; i < 6 ; i ++)
{
//生成一个97~122的int型的整数
int intVal = (int)(Math.random() * 26 + 97);
//将intValue强制转换为char后连接到result后面
result = result + (char)intVal;
}
//输出随机字符串
System.out.println(result);
}
enum Size{SMALL,MEDIUM,LARGE};
}根据Ascii码表
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/17703330.html
千万千万赵千万