Java基础学习到进阶自我总结三
2019-12-26
随机数的基本操作和数组操作
随机数
/**
* 随机数
* @author Administrator
*
*/
public static void main(String[] args) {
/*
* 随机数:在某一个范围内,系统任意产生的一个数(int)
*/
Random random = new Random();
//产生一个随机数
int a = random.nextInt();//整数型随机
//产生0-10的以为随机数
a = random.nextInt(11)+0;
System.out.println("一位数的随机数:"+a);
//产生一个三位数的随机数
a = random.nextInt(900)+100;
System.out.println("三位数的随机数:"+a);
//-10~+10的随机数
a = random.nextInt(21)+(-10);
System.out.println("-10~+10的随机数:"+a);
double d = random.nextDouble();
DecimalFormat dFormat = new DecimalFormat("#.##");//四舍五入法保留两位小数
String string = dFormat.format(d);
System.out.println("保留两位小数:"+string);
//数学方法产生0-1的随机数
d = Math.random();
System.out.println(d);
}
随机数与数组
/**
* 随机获取数组中的元素
* @author Administrator
*
*/
public static void main(String[] args) {
int as[] = {12,45,13,46,25,89,78,79,59};
//随机获取数组中的元素
Random random = new Random();
int a = random.nextInt(as.length)+0;
System.out.println("随机获取的数组值为:"+as[a]);
}
利用循环,随机产生5个5位数的回文数
ps 回文数:12321; 32123 两边不包括0
/**
* @author Administrator
*利用循环,随机产生5个5位数的回文数,完成后输出到控制台
*回文数:12321;32123 两边不包括0
*/
public static void main(String[] args) {
Random random = new Random();
for (int i = 0; i < 5; i++) {
while(true){
//随机获取从10000开始的90000个5位数
int a = random.nextInt(90000)+10000;
int w = a/10000,q=a/1000%10, b= a/100%10,s = a/10%10,g = a%10;
if (w==g&&q==s&&((b>q&&q>w)||(b<q&&q<w))) {
System.out.println(a);
break;
}
}
}
}
随机获取数组中7个不同的值
/**
* 随机数与数组
* @author Administrator
*有数组String ss[] = {"1","2","3","4","5","6","7","8","9","a","b","h","j","m","b","1","3","9"},
*随机获取数组中7个不同的值,输出控制台
*/
public static void main(String[] args) {
String ss[] = {"1","2","3","4","5","6","7","8","9","a","b","h","j","m","b","1","3","9"};
//声明随机方法
Random random = new Random();
//定义空字符串数组
String nss[] = {};
//循环7次,取七个数
for (int i = 0; i < 7; i++) {
//一直循环
while(true){
boolean flat = false;
int a = random.nextInt(ss.length);
//当nss[j]==ss[a]d时给flat赋值true并跳出循环
for (int j = 0; j < nss.length; j++) {
if (nss[j].equals(ss[a])) {
flat = true;
break;
}
}
//当flat==false时nss字符串数组添加并跳出while循环
if (!flat) {
nss = Arrays.copyOf(nss, nss.length+1);
nss[nss.length-1] = ss[a];
break;
}
}
}
System.out.println(Arrays.toString(nss));
}
输出直角三角形*
/*
*输出直角三角形的'*'的方法
*
*/
public void draw(int lines , String c){
for (int i = 0; i < lines; i++) {
for (int j = 0; j < (1+i); j++) {
System.out.print(c);
}
System.out.println();
}
}
输出等腰三角形*
/*
* 输出等腰三角型的'*'的方法
*
*/
public void draw(int lines,String c){
for (int z = 0; z < lines; z++) {
for (int j = lines-1; j >z ; j--) {
System.out.print(" ");
}
for (int i = 1; i < z*2; i++) {
System.out.print(c);
}
System.out.println();
}
}
两个变量交换值(值均为正数时), 不引用第三变量
“1加2减”
a = a+b ;//a = 10,b = 2;a=a+b=12;
b = a-b;//b=a-b = 12-2=10;
a = a-b;//a=a-b = 12-10=2;
方法的简单概念
方法的概念:
/*
* 方法就是类的功能,即这个类能做什么
* public:公共的 任何的类都可以使用
* 默认 不写:在本工程内可以使用
* protected:保护 继承关系的类或者同一个包内
* private:私有化 同一个类内可以使用
* void: 没有返回值类型 方法运行的结果值在本方法中使用,别的方法调用该方法无法获取结果
*/
/*
* static: 静态 修饰状态
* 修饰变量 方法:静态变量或静态方法(没有就是非静态变量或非静态方法)
* 在同意个类中:非静态方法可以调用任意的静态或非静态方法
* 静态方法不能直接使用非静态的方法或变量,但可以通过类对象名(new ClassName)去调用
* 静态可以直接调用静态方法和变量
*/
/*
* 什么时候分方法: 方法名尽量用动词,首字母小写,一个方法就是一种功能。一些工具类的方法是public static的
* 方法的代码行一般不超过200
*/
/*
* continue:跳过一次循环 只能在循环体用
* break:跳出循环,能在循环体或switch
* return:结束方法 在方法体里面可以使用
*/