In this section, the learning program flow control, structure determination comprises selecting structure, cyclic structure.
1, the structure is determined (if, if-else)
Difference ternary operator and the if-else
Ternary operator, an operator, the operation must result if-else statement, only the control flow, so not necessarily result in some cases, it can be rewritten in the form of if-else ternary operator premise to ensure the if-else executing the specific results appear
Example 1: corresponding to the digital input, selects the output of the corresponding results
1 package day01; 2 3 import java.util.Scanner; 4 5 public class translate { 6 7 public static void main(String[] args) { 8 int num = 0; 9 // TODO Auto-generated method stub 10 System.out.println("请输入1-5当中的数字:"); 11 Scanner sc = new Scanner(System.in); 12 int i=sc.nextInt(); 13 if(i == 1) { 14 System.out.println ( "user input" + i + "is a word corresponding to the Apple" ); 15 } the else IF (I == 2 ) { 16 System.out.println ( "user input" + i + "corresponding to word is Banana " ); . 17 } the else IF (I ==. 3 ) { 18 is System.out.println (" user input "+ i +" is a word corresponding to PEAR " ); . 19 } the else IF (I ==. 4 ) { 20 is System.out.println ( "user input" + i + "is a word corresponding to Peach" ); 21 is } the else IF (I ==. 5 ) { 22 is System.out.println ( "user input" + i + "is a word corresponding to Orange" ); 23 is } 24 25 } 26 is 27 }
Example 2: if the nested
. 1 Package Homework; 2 . 3 Import java.util.Scanner; . 4 . 5 public class exer8 { . 6 public static void main (String [] args) { . 7 Scanner SC = new new Scanner (the System.in); . 8 System.out.println ( "Please enter whether Member: yES (y) / N (n-)" ); . 9 String I = sc.next (); 10 System.out.println ( "test" ); . 11 iF (i.equals ( "Y ' )) { 12 System.out.println ( "Please enter the purchase amount:" ); 13 a float Money = sc.nextFloat (); 14 IF (Money> 200 is ) { 15 System.out.println ( "actually paid:" + ( a float ) (Money * (0.75 ))); 16 } the else { . 17 the System.out .println ( "actually paid:" + ( a float ) (Money * (0.8 ))); 18 is } . 19 } the else IF (i.equals ( "n-" )) { 20 is System.out.println ( "Please enter the amount of shopping : " ); 21 is a float Money = sc.nextFloat (); 22 is System.out.println ( "actually paid:" + ( a float ) (Money * (0.9 ))); 23 is } 24 25 26 is } 27 28 }
2, selection structure (switch)
The switch statement Features : a, switch statement selects the type of support in only four kinds jdk1.6: byte, short, int, char. After jdk1.7 support of type String. between b, case and no default order. First implementation of a case, case no match finally execute default. c, the end of the switch statement in both cases: encountered break, execution until the end of the switch statement. d, matching case or default if there is no corresponding break, then the program continues downward, the statement can be executed to run until it encounters the end of the break or switch over.
Example 1: Achieving Calculator
1 package day02; 2 3 import java.util.Scanner; 4 5 public class switch01 { 6 7 public static void main(String[] args) { 8 9 // 计算器 10 int result = 0; 11 System.out.println("输入第一个数:"); 12 Scanner sc = new Scanner(System.in); 13 int num1=sc.nextInt(); 14 15 System.out.println("选择加减乘除:(1加 2减 3乘 4除)"); 16 int choice=sc.nextInt(); 17 switch(choice) { 18 case 1: 19 System.out.println("输入第二个数:"); 20 int num2=sc.nextInt(); 21 result = num1+num2; 22 System.out.println("得到的结果为:"+result); 23 break; 24 case 2: 25 System.out.println("输入第二个数:"); 26 int num3=sc.nextInt(); 27 result = num1-num3; 28 System.out.println("得到的结果为:"+result); 29 break; 30 case 3: 31 System.out.println("输入第二个数:"); 32 int num4=sc.nextInt(); 33 result = num1*num4; 34 System.out.println("得到的结果为:"+result); 35 break; 36 case 4: 37 System.out.println("输入第二个数:"); 38 int num5=sc.nextInt(); 39 result = num1/num5; 40 System.out.println("得到的结果为:%s"+result); 41 break; 42 default:break; 43 } 44 45 46 } 47 48 }
例子2:实现星期的翻译
1 package day02; 2 3 import java.util.Scanner; 4 5 public class week { 6 7 public static void main(String[] args) { 8 int num = 0; 9 // TODO Auto-generated method stub 10 System.out.println("请输入1-7当中的数字:"); 11 Scanner sc = new Scanner(System.in); 12 int i=sc.nextInt(); 13 switch(i) { 14 case 1: 15 System.out.println("今天是星期一"); 16 break; 17 case 2: 18 System.out.println("今天是星期二"); 19 break; 20 case 3: 21 System.out.println("今天是星期三"); 22 break; 23 case 4: 24 System.out.println("今天是星期四"); 25 break; 26 case 5: 27 System.out.println("今天是星期五"); 28 break; 29 case 6: 30 System.out.println("今天是星期六"); 31 break; 32 case 7: 33 System.out.println("今天是星期七"); 34 break; 35 default:break; 36 37 } 38 39 } 40 41 }
3、循环结构(while,do-while,for)
do while特点是条件无论是否满足, 循环体至少被执行一次。
定义循环要注意 1:定义循环的条件 2:控制循环执行的次数 while里面的表达式可以用true,这样的话这个循环就是一个死循环了,会一直执行。
格式: for(初始化表达式;循环条件表达式;循环后的操作表达式) {
执行语句;
(循环体)
}
注:
a:for里面的几个表达式运行的顺序,初始化表达式只读一次,判断循环条件,为真就执行循环体,然后再执行循环后的操作表达式,接着继续判断循环条件,重复这个过程,直到条件不满足为止。
b:while与for可以互换,区别在于for为了循环而定义的变量在for循环结束就是在内存中释放。而while循环使用的变量在循环结束后还可以继续使用。
c:最简单无限循环格式:while(true) , for(;;),无限循环存在的原因是并不知道循环多少次,而是根据某些条件,来控制循环。
例子1:for循环 实现99乘法表
1 package day02; 2 3 public class jiujiu { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int result; 8 for(int i=1;i<10;i++) { 9 for(int j=1;j<=i;j++) { 10 result = i*j; 11 System.out.print(j+"*"+i+"="+result+"\t"); 12 } 13 System.out.println(); 14 } 15 16 } 17 18 }
例子2: while循环 实现判断多少年后,学员达到1000000
1 package homework; 2 3 public class exer2 { 4 5 public static void main(String[] args) { 6 int year = 2012; 7 double student = 250000; 8 while(student <= 1000000) { 9 student = student * (1 + 0.25); 10 year++; 11 } 12 System.out.println("到"+ year +"年培训学员人数将达到1000000"); 13 14 } 15 16 }
4、其他控制流程语句
break(跳出), continue(继续)
break语句:应用范围:选择结构和循环结构。 跳出当前循环
continue语句:应用于循环结构。 结束本次循环,继续下次循环
注:
a:这两个语句离开应用范围,存在是没有意义的。
b:这两个语句单独存在下面都不可以有语句,因为执行不到。
c:continue语句是结束本次循环继续下次循环。
d:花括号的出现,可以让这两个语句作用于指定的范围。