for循环经典案例:
①:求1+2+3+...+100的和。
package test;
/**
* 案例:1+2+...+100的和
* 整理代码快捷键:ctrl+shift+l
*/
public class Demo1 {
public static void main(String[] args) {
int sum=0;
for(int i=1; i<=100 ; i++){
//sum+=i;
sum=sum+i;
}
System.out.println("1+2+...+100的和为:"+sum);
}
}
运行结果:
②打印水仙花
求100-999之间所有的水仙花(水仙花数 个位、十位、百位的三次方等于该数)。
用while循环来实现:
package test;
/**
* 求100-999之间所有的水仙花数
* (水仙花数 个位、十位、百位的三次方等于该数)
*/
public class Demo04 {
public static void main(String[] args) {
int i,j,m;
int n = 100;
while (n<=999){
i = n/100;
j = n/10%10;
m = n%10;
if(n == i*i*i+j*j*j+m*m*m)
System.out.println("水仙花数为:"+n);
n++;
}
}
}
用for循环来实现:
package test;
/**
* 求100-999之间所有的水仙花数
* (水仙花数 个位、十位、百位的三次方等于该数)
*/
public class Demo05 {
public static void main(String[] args) {
int i,j,m;
int n = 100;
for(n=100; n<=999; n++){
i = n/100;
j = n/10%10;
m = n%10;
if(n == i*i*i+j*j*j+m*m*m)
System.out.println("水仙花数为:"+n);
}
}
}
运行结果:
双重for循环经典案例:
①九九乘法表的打印:
package test;
/**
* 案例:双重for循环的运用
* ①九九乘法表的打印
* 1*1=1
* 1*2=2 2*2=4
* 1*3=3 2*3=6 3*3=9
* 1*4=4 2*4=8 3*4=12 4*4=16
* 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
* 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
* 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
* 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
* 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
*/
public class Demo01 {
public static void main(String[] args) {
// 打印九九乘法表
for(int i= 1; i<=9; i++){
for(int j= 1; j<=i; j++){
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
}
}
运行结果:
②三角形的打印
package test;
/**
* 案例:双重for循环的运用
* ②三角形的打印
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
*/
public class Demo02 {
public static void main(String[] args) {
//正三角
for(int i= 1; i<=4; i++){
for(int j= 1; j<=i; j++){
System.out.print("*\t");
}
System.out.println();
}
System.out.println("=========分割线=========");
// 倒三角
for(int i= 1; i<=4; i++){
for(int j= 4; j>=i; j--){
System.out.print("*\t");
}
System.out.println();
}
}
}
运行结果:
③打印菱形
package test;
/**
* 案例:打印菱形
*/
public class Demo03 {
public static void main(String[] args) {
for(int i= 1; i<=4; i++){
//上半部分空格区
for(int j= 3; j>=i; j--){
System.out.print(" ");
}
//上半部分*区
for(int j= 1; j<=i; j++){
System.out.print("* ");
}
System.out.println();
}
for(int i= 1; i<=4; i++){
//上半部分空格区
for(int j= 1; j<=i; j++){
System.out.print(" ");
}
//上半部分*区
for(int j= 3; j>=i; j--){
System.out.print("* ");
}
System.out.println();
}
}
}
运行结果:
数组经典案例:
①登录验证:
package test;
import java.util.Random;
import java.util.Scanner;
/**
* 固定的账号 密码
* 判定用户输入的账号密码是否一致,如果一致随机生成四位数验证码,验证也OK,则登录成功
* 如果用户输入账号密码不一致,则随机生成四位数验证,告知用户登录失败,账号密码不正确
*/
public class Demo01 {
public static void main(String[] args) {
//用户输入
Scanner sc = new Scanner(System.in);
//随机数
Random rd = new Random();
//账号
String username = "admin";
//密码
String password = "123";
//验证码字典A、B、C、D、E、F、G、H、I、J、K、L、M、N、O、P、Q、R、S、T、U、V、W、X 、Y、Z
String[] strArr = new String[]
{"a","b","c","d","e","f","g","h","i","j","k","l","m"+
"n","o","p","q","r","s","t","u","v","w","x","y","z"+
"A","B","C","D","E","F","G","H","I","J","K","L","M"+
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z"+
"0","1","2","3","4","5","6","7","8","9"
};
//验证码对象 +:拼接 (String字符串的 + 就是拼接)
String verify = strArr[rd.nextInt(strArr.length)]+strArr[rd.nextInt(strArr.length)]+
strArr[rd.nextInt(strArr.length)]+strArr[rd.nextInt(strArr.length)];
System.out.println("欢迎来到百度");
System.out.println("请输入账号:");
String inputUsername = sc.next();
System.out.println("请输入密码:");
String inputPassword = sc.next();
if(inputUsername.equals(username) && inputPassword.equals(password)){
System.out.println("请输入验证码:"+verify);
String inputVerify = sc.next();
if(inputVerify.equals(verify)){
System.out.println("登录成功了~");
}else{
System.out.println("验证码或者密码错误~");
}
}else {
System.out.println("请输入验证码:"+verify);
String inputVerify = sc.next();
System.out.println("密码错误~");
}
}
}
运行结果:
②模拟双色球原理
双色球规定:红球1-33之间生成6个红球不重复,蓝球1-16之间随机生成一个数。
package homeworktest;
import java.util.Arrays;
import java.util.Random;
public class Demo02 {
public static void main(String[] args) {
//双色球规定:红球1-33之间生成六个红球不重复 篮球1-16之间随机一个数
System.out.println("欢迎来到中国福利彩票");
System.out.println("两元做福利,圆你暴富梦");
//创建随机数
Random rd = new Random();
//蓝球 1-16 随机一个
int blueBoll = rd.nextInt(16)+1;
//目标球
int[] target = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,
23,24,25,26,27,28,29,30,31,32,33};
//目标状态数组 boolean类型默认值:false
boolean[] flag = new boolean[target.length];
//红球 1-33 之间随机出六个
int[] redBoll = new int[6];
for(int i=0 ; i < redBoll.length ; i++){
//redBoll[i] = rd.nextInt(33)+1;
//先定义下标
int k;
//直到摸到false才停止抽奖
do{
k = rd.nextInt(33);
}while (flag[k]);
//证明我的球没被拿过
redBoll[i] = target[k];
//改变状态
flag[k] = true;
}
// 红球需要从小到大进行排序
// 用冒泡进行排序 但在Arrays里面封装了一个方法叫sotr,这里面也可以对数组进行排序(从小到大)
Arrays.sort(redBoll);
// 新建一个数组用来追加蓝球
int[] newArr = Arrays.copyOf(redBoll,redBoll.length+1);
// 数组最后一位进行赋值
newArr[newArr.length-1] = blueBoll;
// 打印新数组
System.out.println("两百万给我买它!!!"+Arrays.toString(newArr));
}
}
运行结果:
递归算法思想:
①斐波那契数列
指的是这样一个数列:1,1,2,3,5,8,13,21,34,55,89,144........
规律:数列从第三项开始每一项等于前两项之和。 n = ( n - 1 )+( n - 2 )
package test;
/**
* [递归] 求斐波那契数列
* 递归:递归是指程序在运行中,不断的调用自身的编程技巧
* 注意事项:[递归一定要有出口,必须要让程序停下来]
* 斐波那契数列又称黄金分割数列:指的是这样一个数列
* 1 1 2 3 5 8 13 21 34 55 89 144.....
* 求第n项斐波那契数列对应的值
* 规律:数列从第三项开始每一项等于前两项之和
* n = (n-1)+(n-2)
*/
public class Demo01 {
public static void main(String[] args) {
System.out.println("请输入您要查询斐波那契数列的第几项");
int fa = fb(7);
System.out.println(fa);
}
public static int fb(int n){
//结束条件是什么
if(n==1||n==2){
return 1;
}
return fb(n-1)+fb(n-2);
}
}
运行结果: