您的位置:首页 > 编程语言 > Java开发

JAVA 基础知识复习4

2014-03-06 11:02 197 查看
1.字符串转化为二维数组且按顺序输出:

public class Test {
public static void main(String[] args) {
double[][] d ;
String s="0,1,2;3,4;5,6;7,8" ;
String[] sFrist = s.split(";");
d = new double[sFrist.length][] ;
for (int i = 0; i < sFrist.length; i++) {
String[] sSecond = sFrist[i].split(",");
d[i] = new double[sSecond.length];
for (int j = 0; j < sSecond.length; j++) {
d[i][j] = Double.parseDouble(sSecond[j]);
}
}

for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[i].length; j++) {
System.out.print(d[i][j]+" ");
}
System.out.println();
}

}
}


2.五个随机数博彩

public class TestRandom {
static Object[] a ={1,6,9,'l','y','r','i','c'};
public static void main(String[] args) {
int a ;
randomDemo();
dateDemo();

}
//输出五行随机数 。Random类的使用

public static void randomDemo(){
Random random = new Random();
int m = 0 ;
while(m<5){
String str="";
for (int i = 0; i < 5; i++) {
str+=a[random.nextInt(a.length)];
}
System.out.println(str);
m++;
}
}

public static void dateDemo(){

// Date date = new Date();
// System.out.println(date);  //Fri Sep 13 20:00:30 CST 2013
Date date = new Date();//得到一个日期
DateFormat f= new SimpleDateFormat("yyyy-M-dd a hh:mm:ss");
String time=f.format(date);
System.out.println(time);
}
}

3.枚举类型是一种自定义的类型

example:public enum Mycolor{ red  , yellow , blue  } ;(在调用时,使用Mycolor.red调用)、因为只能调用预先定义好的类型所以在编译期间,即可发现程序的错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: