,1,循环结构:
* for结构
* do..while结构
* while结构
*
* (1) for结构
* 语法格式:
* for(初始化语句;判断条件语句;控制条件语句){
* //语句块
* }
* 循环结构:
* for结构
* do..while结构
* while结构
*
* for结构
* 语法格式:
* for(初始化语句;判断条件语句;控制条件语句){
* //语句块
* }
*
* 注意事项:
* 1:判断条件语句的结果是一个boolean类型
* 2:循环体语句如果是一条语句,大括号可以省略;如果是多条语句,大括号不能省略。建议永远不要省略。
(2)while
*
* 演示while循环的结构
*
* 语法格式:
* while(判断条件语句 ){
* 循环体语句;(可以有很多条java语句)
* }
*
*
* while结构的扩展形式
*
* 初始化语句;
* while(判断条件语句 ){
* 循环体语句;(可以有很多条java语句)
* 控制条件语句;
* }
*/
public classWhileDemo1 {
public static void main(String[] args) {
// 定义一个本地变量 i
// 变量i的作用域 是在main方法中有效
int i =0;//1
while(i<10){//2
System.out.println("helloworld"); //3
i++; //4
}
System.out.println(i);
// 上面的形式与 for结构 作用一样
// 使用while循环的形式,来做题: 输出1-100之间的和
int j =1;
int sum = 0;
while(j<=100){
sum +=j;
j++;
}
System.out.println("1-100之间的和:"+sum);
/**
* 演示一下使用for循环来 输出1-100之间的和
*/
int sum1 = 0;
for(int m=1;m<=100;m++){
sum1+=m;
}
/*m变量的作用域是在for循环的大括号内,离开了for循环的大括号,m就失效了*/
System.out.println("1-100之间的和:"+sum1);
//m cannot be resolved to a variable
// System.out.println(m);
/**
* 总结来说,
* for循环结构 的本地变量,占用内用内存的时间更短,
* for循环结束。本地变量,即可被jvm回收,能够提高内存的使用效率。
*
* 建议大家多使用for循环结构。
*/
}
}
**
* while循环的练习
* 我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,
* 厚度为:0.01m。请问,我折叠多少次,
* 就可以保证厚度不低于珠穆朗玛峰的高度?
*
* 程序的分析:
* double high = 8848;
* double thickness = 0.01;
*
* 第一次折: thickness *=2;
* 第二次折: thickness *=2;
* ...
*
*
* n次,thickness >= high
*
* 要求while
* @author jerry
*
*/
public classWhileDemo2 {
public static void main(String[] args) {
//定义三个变量
double high = 8848;
double thickness = 0.01;
// 表示折纸的次数
int n = 0;
while(thickness <high){
// 折纸
thickness *=2;
n++;
System.out.println("第 "+n+" 次折,厚度为:"+thickness+" m");
}
// System.out.println("最终折纸 次数为:"+n +" 厚度为:"+thickness+" m");
}
}
(3)do while
**
* do while的演示
*
* 语法格式:
* do {
* 循环体语句(可以有多条java语句)
* }while(判断条件语句);
*
*
* 扩展形式:
* 初始化语句:
* do{
* 循环体语句(可以有多条java语句);
* 控制条件语句;
* }while(判断条件语句);
* 举例说明:
*
* dowhile改造 输出"helloworld" 100次
*
* dowhile 改造 输出 1-100 之间所有数字的和
*
*
* @author jerry
*
*/
public classDoWhileDemo1 {
public static void main(String[] args) {
int i =0;
do{
System.out.println("第 "+i+" 次,输出hello world");
i++;
}while(i<100);
//=======================
int j = 1;
int sum = 0;
do {
sum +=j;
j++;//控制条件语句
}while(j<=100);
System.out.println("1-100之间的数字的和:"+sum);
}
}
*(4) 循环结构中的特殊形式,"死循环"
* 一单进来,就出不去
* java中的空语句: ;
*
* 语法结构:
* for(;;){
* 循环体语句
* }
* while(true){
* 循环体语句
* }
*
* 举例说明:
*
* @author jerry
*
*/
public classDeadLoop {
public static void main(String[] args) {
;//java中的空语句
// for(;;){
// System.out.println("helloworld");
// }
// compile compiler 编译器
//Unreachable code
while(true){
System.out.println("helloworld");
}
}
}
(5)循环嵌套
* 循环套循环的结构,
* for(int i=0;i<10;i++){
* 循环体语句;
* for(int j=0;j<10;j++){
*
* 循环体语句;
* for(int m=0;m<10;m++){
* 循环体语句;
* ....
* }
* }
* }
* 建议:工作中,尽量只出现两层嵌套的循环
*
*
* 举例说明:
*
* 请输出一个4行5列的星星(*)图案
* @author jerry
*
*/
public classNestedLoop {
public static void main(String[] args) {
//使用循环来解决问题
// for(int i=0;i<5;i++){
// System.out.print("*");
// // print 只打印不换行,println 打印并且换行
// }
// System.out.println("");
// for(int i=0;i<5;i++){
// System.out.print("*");
// // print 只打印不换行,println 打印并且换行
// }
// System.out.println("");
// for(int i=0;i<5;i++){
// System.out.print("*");
// // print 只打印不换行,println 打印并且换行
// }
// System.out.println("");
// for(int i=0;i<5;i++){
// System.out.print("*");
// // print 只打印不换行,println 打印并且换行
// }
for(int j = 0;j<4;j++){
for(int i=0;i<5;i++){
System.out.print("*");
// print 只打印不换行,println 打印并且换行
}
System.out.println("");
}
}
}
2,选择结构
两种语法形式:
* 1: if结构(语句)
* 2:switch结构(语句)
*
*
* (1) if语句有三种表现形式:
* 第一种:
* if(逻辑表达式){
* // 其他语句块 (可以写很多java语句)
* }
* 第二种形式:
* if(逻辑表达式){
* //语句块
* }else{
* // 语句块
* }
*
* 第三种:
* if(表达式){
* //语句块
* }
* else if(表达式){
*
* //语句块
* }
* ...
* else{
*
* //语句块
* }
举例说明:
*
*
*
*
* @author jerry
*
*/
public classIFDemo1 {
public static void main(String[] args) {
// 定义两个变量
int x = 10;
int y = 20;
if(x!=y){
System.out.println("x不等于y");
}
//使用选择结构
if(x > y){
System.out.println("最大值为 x="+x);
}else{
System.out.println("最大值为 y="+y);
}
/**
* 考试成绩 score = 80;
* 如果100>score >=90 优秀
* 如果90>score >70 良好
* 如果 70>score >60 及格
* 如果 score < 60 努力吧,少年!
* else 没意思
*/
int score = 500;
if( score >= 90 && score<= 100){
System.out.println("优秀");
} else if(score >= 70 &&score < 90){
System.out.println("良好");
}else if(score >= 60 &&score < 70){
System.out.println("及格");
}else if(score < 60){
System.out.println("努力吧,少年!");
}else{
System.out.println("没意思!");
}
}
}
(2)
switch 语句
*
* 语法结构:
* switch(表达式){
*
* case 数值1:
* 可以有很多语句;
* break;
* case 数值2:
* 可以有很多java的语句;
* break;
* ....
*
* default:
* 可以有很多java的语句;
* break;
*
* }
* */
public classSwitchDemo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个月份(1-12):");
int month = sc.nextInt();
int a = 10;
switch (month) {
case 12:
case 1:
case 2:
System.out.println("大约在冬季");
break;
case 3:
case 4:
case 5:
System.out.println("大约在春季");
break;
case 6:
case 7:
case 8:
System.out.println("大约在夏季");
break;
case 9:
case 10: //case expressions must beconstant expressions
case 11:
System.out.println("大约在秋季");
break;
default:
System.out.println("的解放开绿灯解放的");
System.out.println("你输入的数据无效,请重新输入!!");
break;
}
}
}
5,典型例题:
小芳的妈妈,每天给小芳2.5元钱,小芳每天都会把这个钱存起来,但是,第五天,或者当数是5的倍数的时候,小芳就会花掉6元钱,请问多少天以后,小芳的钱能多于100元。请用for循环解决问题。
int day=0;
int sum=100;
while(true){
day++;
money+=2.5;
if(money>=sum){
break;
}
if(day%5==0){
money-=6; }
System.out.println("第"+day+"天,共存了"+money+"钱");
}
System.out.println("共"+day+"天");
答案;74天。
package com.newedu.jb.day03.exercise;
public class Demo2 {
publicstatic void main(String[] args) {
/**
* 请在控制台输出满足如下条件的五位数
* 个位等于万位
* 十位等于千位
* 个位+十位+千位+万位=百位
*
*
* 程序的分析:
* 五位数:10000 ~ 99999
*
* 假设:abcde
* e = a
* d = b
* e + d + b + a = c
*
* 小小的问题?
* 如何根据一个数,来得到的个数数字呢?
* int x = 12345;
*
* 除以10获取余数(对10的求余数)
* 12345 %10 = 5
* 12345/10 = 1234
*
* 对商值进一步的重复上面的两个操作
* 1234%10 = 4;
* 1234 /10 = 123
*
* ...
*
* int e = 5;?
*/
for(inti=10000;i<=99999;i++){
intnum = i;
//规则
//abcde
inte = num%10;
num/=10;
intd = num%10;
num/=10;
intc = num%10;
num/=10;
intb = num%10;
num/=10;
inta = num%10;
//个位等于万位
//十位等于千位
//个位+十位+千位+万位=百位
//规则
//选择结构
if(e==a&& d == b&& e+d+b+a==c){
System.out.println("满足条件的值: "+ i);
}
}
}
}