流程控制
1、用户交互Scanner
package com.lzi.Scanner;
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()){
//Shiyong next方式接收
String str = scanner.next();
System.out.println("输出的内容为:"+str);->hello
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯,用完就关掉
scanner.close();
}
}
package com.lzi.Scanner;
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);->hello world
}
scanner.close();
}
}
一样的
package com.lzi.Scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
String str = scanner.nextLine();
System.out.println("输出的内容为"+str);
scanner.close();
}
}
package com.lzi.Scanner;
import java.util.Scanner;
public class Ddemo03 {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
int i= 0;
float f = 0.0f;
System.out.println("输入整数");
if(scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整数数据:"+i);
}
else {
System.out.println("输入的数据不是整数");
}
}
}
作业
我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字对结束输入并输出执行结果:
package com.lzi.Scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字对结束输入并输出执行结果:
double sum = 0;
double avg = 0;
int a = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("输入多个数字");
while(scanner.hasNextDouble()){
double i = scanner.nextDouble();
sum = sum + i;
a++;
}
avg = sum/a;
System.out.println("总和为:"+sum);
System.out.println("平均数为:"+avg);
scanner.close();
}
}
next()
- 一定要读取到有效字符后才可以结束输入
- 对于输入有效字符之前遇到的空白字符,next()方法会自动将其去掉
- 只有输入有效字符之后才将其后输入的空白字符作为分隔符或者结束符
- next()不能得到带有空格的字符串
nextLine()
- 以回车(enter)作为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符
- 可以获得空白字符
2、选择结构
if结构
package com.lzi.struct;
import java.util.Scanner;
public class ShunXunDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容");
String str = scanner.nextLine();
//equals:判断字符串是否相等
if(str.equals("hello")){
System.out.println("相等");
}
scanner.close();
}
}
if 双选择结构
package com.lzi.struct;
import java.util.Scanner;
public class ShunXunDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入分数");
int i = scanner.nextInt();
if(i>60){
System.out.println("成绩及格");
}
else{
System.out.println("成绩不及格");
}
scanner.close();
}
}
if多选择结构
package com.lzi.struct;
import java.util.Scanner;
public class ShunXunDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入成绩:");
int score = scanner.nextInt();
if(score >= 90 && score <=100)
{
System.out.println("成绩等级为A");
}else if (score >= 80 && score < 90){
System.out.println("成绩等级为B");
}else if (score >= 70 && score < 80 ){
System.out.println("成绩等级为C");
}else if (score >= 60 && score < 70){
System.out.println("成绩等级为D");
}else{
System.out.println("成绩为不及格");
}
scanner.close();
}
}
switch多选择结构
package com.lzi.struct;
import java.util.Scanner;
public class ShunXunDemo {
public static void main(String[] args) {
char grade = 'c';
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;
default:
System.out.println("如果grade找不到值,就输入这句话");
}
}
}
public class ShunXunDemo {
public static void main(String[] args) {
String name = "dudu";
switch (name){
case "嘻嘻":
System.out.println("嘻嘻");
break;
case "duud":
System.out.println("duud");
break;
default:
System.out.println("找不到");
}
}
}
3、循环结构
while循环
package com.lzi.struct;
public class WhileDemo01 {
public static void main(String[] args) {
int i = 0;
while (i<20){
i++;
System.out.println(i);
}
}
}
计算1-100的和
package com.lzi.struct;
public class WhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
do…while循环
注意:do…while和while的区别
- while先判断在执行,dowhile是先执行后判断
- 即使不满足条件,也至少执行一次
package com.lzi.struct;
public class WhileDemo01 {
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 WhileDemo01 {
public static void main(String[] args) {
int i = 0;
while(i<0){ ->不满足条件根本不进来
System.out.println(i);
i++;
}
System.out.println("------------------------");
do{
System.out.println(i); ->输出0
i++;
}while(i<0);
}
}
for循环练习
练习1:计算0-100之间奇数和偶数的和
package com.lzi.struct;
public class WhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum1 = 0;
int sum2 = 0;
for(;i<=100 ;i++){
if(i%2==0){
sum1 = sum1+i;
} else{
sum2 =sum2+i;
}
}
System.out.println("奇数和为:"+sum2);->2500
System.out.println("偶数和为:"+sum1);->2550
}
}
练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
public class WhileDemo01 {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
}
if(i%(5*3)==0){
System.out.println();
}
}
}
}
public class WhileDemo01 {
public static void main(String[] args) {
int i=1;
while(i<=1000){
if(i%5==0)
{
System.out.print(i+"\t");
}
if(i%(5*3)==0){
System.out.println("");
}
i++;
}
}
}
练习3:打印九九乘法表
package com.lzi.struct;
public class WhileDemo01 {
public static void main(String[] args) {
int i;
int j;
for(i=9;i<=9&&i>0;i--){
for(j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println("");
}
}
}
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*4=4 2*4=8 3*4=12 4*4=16
1*3=3 2*3=6 3*3=9
1*2=2 2*2=4
1*1=1
public class WhileDemo01 {
public static void main(String[] args) {
int i;
int j;
for(i=1;i<=9;i++){
for(j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println("");
}
}
}
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
练习4:2-150之间的质数
质数:大于1的自然数,除了1和本身以外不再有其他因数的自然数
public class WhileDemo01 {
public static void main(String[] args) {
outer:for (int i = 2; i < 150; i++) {
for (int j = 2; j < i/2; j++) {
if(i%j==0)
{
continue outer;
}
}
System.out.println(i+"\t");
}
}
}
增强for循环
package com.lzi.struct;
public class WhileDemo01 {
public static void main(String[] args) {
int[] number = {10,20,30,40,50};//定义一个数组
for (int i = 0; i < 5; i++) {
System.out.print(number[i]+"\t");
}
System.out.println("\n------------------------");
//遍历数组的元素
for(int x:number){
System.out.print(x+"\t");
}
}
}
4、break&continue
注意:
- break用于强行退出循环,不执行循环中剩余的语句
- continue用于终止某次循环,即跳过循环中尚未执行的语句,接着进行下一次循环判定
5、练习
打印一个三角形
package com.lzi.struct;
import java.util.Scanner;
public class WhileDemo01 {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
for (int j = 10; 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(" ");
}
}
}
*
***
*****
*******
*********
***********
*************
***************
*****************