流程控制
1.Scanner
作用:获取用户的输入
语法:
Scanner scanner = new Scanner(System.in);
Scanner中的方法:
next():用于接收用户输入的数据,且一定要读取到有效字符,遇到空格则结束
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用next()方式接收:");
if (scanner.hasNext()){
String str = scanner.next();
System.out.println("输出的内容为:"+str);
}
scanner.close();
}
}
nextLine():用于接收用户输入的数据,结束条件为enter
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine()接收:");
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("接收的数据为:"+str);
}
scanner.close();
}
}
使用hasNext()方法和hasNextLine()方法判断接收的数据!
进阶使用:
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum = 0;
int m = 0;
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
m = m + 1;
sum = sum + x;
System.out.println("你输入了第"+m+"个数据,当前结果为sum="+sum);
}
System.out.println("输入的数据的和为:"+sum);
System.out.println("输入的数据的平均值为:"+(sum/m));
scanner.close();
}
}
2.顺序结构
概念:按照步骤一步步执行,它是所有算法的基础!
3.选择结构
3.1if选择结构
-
单选择 import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
if (s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
-
双选择 import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score>=60){
System.out.println("及格");
}else {
System.out.println("不及格!");
}
scanner.close();
}
}
-
多选择 import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score==100){
System.out.println("满分");
}else if (90<=score && score<100){
System.out.println("A级");
}else if (80<=score && score<90){
System.out.println("B级");
}else if (70<=score && score<80){
System.out.println("C级");
}else if (60<=score && score<70){
System.out.println("D级");
}else if (score<60){
System.out.println("不及格");
}else {
System.out.println("成绩不合法");
}
}
}
3.2switch多选择结构
public class SwitchDemo01 {
public static void main(String[] args) {
char grade = '4';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("中等");
break;
case 'D':
System.out.println("及格");
break;
case 'E':
System.out.println("挂科");
break;
default:
System.out.println("未知等级");
}
}
}
3.3break和continue的区别
都不终止代码的运行,只是跳出循环
break:用于强行退出循环(主要用于switch和一些需要跳出代码块的循环语句中)
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i==20){
break;
}
}
}
}
continue:只跳出某一次循环(即跳过尚未执行的语句,进入下一次循环,再接着判断是否满足)
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%5==0){
System.out.println();
continue;
}
System.out.println(i);
}
}
}
4.循环结构
4.1while循环
使用while循环时,需注意不要出现死循环!
public class WhileDemo02 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
4.2do…while循环
while循环是先判断后执行;do…while循环则是先执行后判断(保证最少执行一次)!
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
public class DoWhileDemo02 {
public static void main(String[] args) {
int a = 0;
while (a<0){
System.out.println(a);
a++;
}
System.out.println("=============");
do {
System.out.println(a);
a++;
}while (a<0);
}
}
4.3for循环
public class ForDemo01 {
public static void main(String[] args) {
int i = 0;
int a;
int sum = 0;
int sum1 = 0;
while (i<100){
i++;
sum += i;
}
System.out.println(sum);
System.out.println("========");
for (a=0;a<=100;a++){
sum1 = sum1 + a;
}
System.out.println(sum1);
}
}
public class ForDemo02 {
public static void main(String[] args) {
int oddSum = 0;
int evenSUm = 0;
for (int i = 0; i < 101; i++) {
if (i%2!=0){
oddSum += i;
}else {
evenSUm += i;
}
}
System.out.println("奇数的和为:"+oddSum);
System.out.println("偶数的和为:"+evenSUm);
}
}
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 0; i < 1001; i++) {
if (i%5==0){
System.out.println(i+"\t");
}
if (i%(5*3)==0){
System.out.println();
}
}
}
}
public class ForDemo05 {
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 + "=" + (j * i) + "\t");
}
System.out.println();
}
}
}
public class ForDemo04 {
public static void main(String[] args) {
for (int i=1;i<=9;i++) {
for (int a=i;a<9;a++){
System.out.print("\t");
}
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (j * i) + "\t");
}
System.out.println();
}
}
}
增强for循环
public class ForDemo06 {
public static void main(String[] args) {
int[] numbers = {1,2,3,4,5};
for (int i = 0;i<5;i++){
System.out.println(numbers[i]);
}
System.out.println("========");
for (int x:numbers){
System.out.println(x);
}
}
}
4.4练习
public class Test01 {
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
boolean k = true;
for (int n = 2; n < i; n++) {
if (i % n == 0) {
k = false;
break;
}
}
if (k) {
System.out.print(i + " ");
}
}
}
}
public class Test02 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++){
for (int j = 5; j >= i; j--){
System.out.print(" ");
}
for (int j = 1; j <= i; j++){
System.out.print("*");
}
for (int j = 1; j < i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
4.5Debug
作用:用于调试代码,可以检查代码中数据的走向!
流程控制的语句都可以嵌套!
|