数字格式化DecimalFormat
package com.company.commonClasses05;
import java.text.DecimalFormat;
public class NumberTest01 {
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("###,###");
System.out.println(df.format(12345678));
DecimalFormat df1 = new DecimalFormat("###,###.##");
System.out.println(df1.format(123456.0789));
DecimalFormat df2 = new DecimalFormat("###,###.0000");
System.out.println(df2.format(12345.67));
}
}
package com.company.commonClasses05;
import java.math.BigDecimal;
public class NumberTest02 {
public static void main(String[] args) {
BigDecimal v1 = new BigDecimal(10);
BigDecimal v2 = new BigDecimal(20);
BigDecimal v3 = v1.add(v2);
System.out.println(v3);
}
}
随机数生成Random
package com.company.commonClasses05;
import java.util.Random;
public class NumberTest03 {
public static void main(String[] args) {
Random r = new Random();
int i = r.nextInt(101);
System.out.println(i);
for(int j=0; j<5; j++){
System.out.println(r.nextInt(101));
}
int[] a = new int[5];
Random r1 = new Random();
a[0] = r1.nextInt(5);
for(int k=0; k<a.length; k++){
a[k] = r1.nextInt(5);
for(int j=0; j<k; j++){
if(a[k] == a[j]){
k--;
break;
}
}
}
for(int j=0; j<a.length; j++){
System.out.println(a[j]);
}
}
}
枚举类型 enum
package com.company.commonClasses05;
public class EnumTest {
public static void main(String[] args) {
int a = 10;
int b = 0;
int result = divide(a, b);
if(result == 1){
System.out.println("成功");
}else if(result == 0){
System.out.println("失败");
}
}
public static int divide(int a, int b){
try{
int c = a / b;
return 1;
}catch(Exception e){
return 0;
}
}
}
package com.company.commonClasses05;
public class EnumTest02 {
public static void main(String[] args) {
int a = 10;
int b = 0;
Result result = divide(a, b);
if(result == Result.SUCCESS){
System.out.println("成功");
}else if(result == Result.FALL){
System.out.println("失败");
}
}
public static Result divide(int a, int b){
try{
int c = a / b;
return Result.SUCCESS;
}catch(Exception e){
return Result.FALL;
}
}
}
enum Result{
SUCCESS, FALL
}
enum Season{
SPRING, SUMMER, AUTUMN, WINTER
}