JAVA的流程控制
1.流程控制:顺序结构,分支(判断)结构,循环结构。
package com.langtao.scanner;
import com.sun.deploy.security.SelectableSecurityManager;
import java.sql.SQLOutput;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
//New an object of Scanner to receive the input from Keyboard from the user.
Scanner scanner = new Scanner(System.in);
//String str = scanner.nextLine();
//System.out.println(str);
int m = 3;
while(m>0){
System.out.println("Please enter your score: ");
if(scanner.hasNextInt()){
int score = scanner.nextInt();
System.out.println("Your score is: " + score);
switch (score/10){
case 10:
System.out.println("Excellent, full mark!");
break;
case 9:
System.out.println("Very good!");
break;
case 8:
case 7:
System.out.println("Not bad.");
break;
case 6:
System.out.println("Pass!");
break;
default:
System.out.println("Failed");
}
}else{
System.out.println("Invalid entry, please enter your score.");
}
m--;
}
scanner.close();
}
}
2. switch case语句在JDK7以后支持String.
package com.langtao.scanner;
import java.util.Scanner;
public class demo4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
System.out.println("Where are you from? ");
String str = scanner.nextLine();
switch (str){
case "Beijing":
System.out.println("You are from Beijing");
break;
case "Shanghai":
System.out.println("You are from Shanghai");
break;
default:
System.out.println("You are from China");
}
}
scanner.close();
}
}
3. 循环结构:while, do while, for, 增强for
3.1 while循环
package com.langtao.struct;
public class WhileDemo1 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while (i < 100){
System.out.println(++i);
sum = sum + i;
}
System.out.println(sum);
}
}
3.2 do while循环
package com.langtao.struct;
public class DoWhileDemo2 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do{
i++;
sum = sum + i;
}while(i<1);
System.out.println(sum);
}
}
3.3 for循环
package com.langtao.struct;
public class ForDemo {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i <= 100; i++){
sum = sum + i;
}
System.out.println(sum);
int evenSum = 0;
int oddSum = 0;
for (int i = 0; i <= 100; i++) {
if(i%2!=0){
evenSum += i;
}else{
oddSum += i;
}
}
System.out.println("evenSum = "+evenSum);
System.out.println("oddSum = "+oddSum);
for (int i = 1; i <= 1000; i++) {
if(i%5 == 0){
System.out.print(i+"\t");
}
if(i%(5*3) == 0){
System.out.println();
}
}
}
}
3.4 for循环打印9*9乘法表
package com.langtao.struct;
public class MultiTablePrint {
public static void main(String[] args) {
for(int i = 1; i <= 9; i++){
for (int j = 1; j <= i; j++) {
System.out.print(i + "" + j + "=" + ij + "\t");
}
System.out.println();
}
}
}
3.5 增强for循环
package com.langtao.struct;
public class EnhancedFor {
public static void main(String[] args) {
int[] numbers = {1, 10, 34};
for(int i:numbers){
System.out.println(i);
}
}
}
3.6 break和continue。break语句在循环体中,直接退出循环体;continue只跳过某次循环尚未执行的语句,继续判断下次循环是否可以执行。
package com.langtao.struct;
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while(i<10){
System.out.println(i++);
if(i == 8){
break;
}
}
System.out.println("The circle has been ended.");
}
}
package com.langtao.struct;
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i++<10){
if(i/2==0){
continue;
}
System.out.println(i++);
}
System.out.println("The circle has been ended.");
}
}
3.6 循环练习
package com.langtao.struct;
public class TrianglePrint {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int m = 0; m < (5-i); m++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int n = 1; n < i ; n++) {
System.out.print("*");
}
System.out.println();
}
}
}

浙公网安备 33010602011771号