1.循环:反复执行一段相同或相似的代码(逻辑相似或者相同)
2.循环三要素:
1.循环变量的初始化
2.循环的条件(以循环变量为基础)
3.循环变量的改变(向着循环的结束变)循环变量:在整个循环过程中所反复改变的那个数
3.循环结构:
1)while:先判断后执行,有可能一次都不执行
2)do...while:先执行后判断,至少执行一次
要素1与要素3相同时,首选do...while
3)for:应用率高,固定次数循环
4.break:跳出循环
continue:跳过循环体中剩余语句而进入下一次循环
案例代码:
//6.根据年份和月份计算天数:
int year = 2001; //年
int month = 4; //月
int days = 0; //天数
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if(year%4==0 && year%100!=0 || year%400==0){
days = 29;
}else{
days = 28;
}
}
System.out.println("days="+days);
/*
//5.个人所得税:
double salary = 10000.0; //工资
double taxSalary = salary-3500; //计税工资
double tax = 0.0; //税
if(taxSalary<=0){
tax=0.0; //不纳税
}else if(taxSalary<=1500){
tax=taxSalary*0.03-0;
}else if(taxSalary<=4500){
tax=taxSalary*0.1-105;
}else if(taxSalary<=9000){
tax=taxSalary*0.2-555;
}
System.out.println("tax="+tax);
*/
/*
//4.闰年的判断:
int year = 2000;
if((year%4==0 && year%100!=0) || year%400==0){
System.out.println(year+"是闰年");
}else{
System.out.println(year+"不是闰年");
}
*/
/*
* 补充:
* 1)变量的作用域/范围:
* 从变量的声明开始,到包含它最近的大括号结束
* 2)变量的同名问题:
* 作用域重叠时,变量不能同名
*/
/*
int a=5;
if(a>2){
int b=8;
//int a=9; //编译错误,作用域重叠时,变量不能同名
}else{
int b=3;
}
*/
/*
int a=5;
if(a>2){
int b=8;
System.out.println(a);
System.out.println(b);
}
System.out.println(a);
//System.out.println(b); //编译错误,超出b的作用范围了
*/
/*
//3.三个数的排序:
int a=8,b=5,c=2;
if(a>b){
int t=a;
a=b;
b=t;
}
if(a>c){
int t=a;
a=c;
c=t;
}
if(b>c){
int t=b;
b=c;
c=t;
}
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
*/
/*
//2.两个数的排序:
int a=8,b=55;
if(a>b){
int t=a;
a=b;
b=t;
}
System.out.println("a="+a);
System.out.println("b="+b);
*/
/*
//1.两数的最大值
int a=55,b=8;
int max = a>b?a:b;
System.out.println("max="+max);
*/
/*
short a = 5;
a += 10; //扩展赋值,在需要的时候会自动强制类型转换
//a = a+10; //编译错误,需要强制类型转换
*/
/*
* while的练习:
* 1)输出10次"行动是成功的阶梯"
* 2)输出9的乘法表:
* 2.1)正序:1到9
* 2.2)倒序:9到1
* 2.3)奇数:1,3,5,7,9
* 3)任选其一写执行过程
*/
/*
int num = 1;
while(num<=9){
System.out.println(num+"*9="+num*9);
num++;
}
System.out.println("over");
*/
/*
int num = 9;
while(num>=1){
System.out.println(num+"*9="+num*9);
num-=2;
}
*/
/*
int times = 0; //1.循环变量的初始化
while(times<10){ //2.循环的条件
System.out.println("行动是成功的阶梯");
times++; //3.循环变量的改变
}
System.out.println("over");
*/
/*
* times=0
* true 输出 times=1
* true 输出 times=2
* true 输出 times=3
* true 输出 times=4
* true 输出 times=5
* true 输出 times=6
* true 输出 times=7
* true 输出 times=8
* true 输出 times=9
* true 输出 times=10
* false while循环结束
* 输出over
*/
package day04;
import java.util.Scanner;
//随机加法运算器
public class Addition {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int score = 0; //总分
for(int i=1;i<=10;i++){ //10次
int a = (int)(Math.random()*100); //加数a
int b = (int)(Math.random()*100); //加数b
int result = a+b; //存和
System.out.println("("+i+")"+a+"+"+b+"=?"); //1.出题
System.out.println("算吧!");
int answer = scan.nextInt(); //2.答题
if(answer==-1){ //3.判题
break;
}
if(answer==result){
score += 10; //答对1题加10分
System.out.println("答对了");
}else{
System.out.println("答错了");
}
}
System.out.println("总分为:"+score);
}
}
package day04;
import java.util.Scanner;
//猜数字小游戏
public class Guessing {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = (int)(Math.random()*1000+1);
System.out.println(num); //作弊
int guess;
do{
System.out.println("猜吧!");
guess = scan.nextInt(); //1+3
if(guess==0){
System.out.println("下次再来吧!");
break;
}
if(guess>num){
System.out.println("太大了");
}else if(guess<num){
System.out.println("太小了");
}else{
System.out.println("恭喜你,猜对了!");
}
}while(guess!=num); //2.
/*
System.out.println("猜吧!");
int guess = scan.nextInt(); //1.
while(guess!=num){ //2.
if(guess==0){
break;
}
if(guess>num){
System.out.println("太大了");
}else{
System.out.println("太小了");
}
System.out.println("猜吧!");
guess = scan.nextInt(); //3.
}
if(guess==num){
System.out.println("恭喜你,猜对了!");
}else{
System.out.println("下次再来吧!");
}
*/
}
}
我是初学者,如有更新不好的,欢迎这位大神指出,谢谢大家!
更多精彩以后更新,转载注明!