Java流程控制
1.用户交互Scanner
Scanner对象
- Java提供了一个工具,我们可以获取用户的输入。java.util.Scanner 是 Java5 的新特征,我们可以通过Scanner类来获取用户的输入。
- 基本语法
Scanner s = new Scannner(System.in);//new,开辟新的内存空间
-
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。
-
next()
- 1.一定要读取到有效字符后才可以结束输入
- 2.对输入有效字符之前遇到的空白,next()方法会自动将其去掉
- 3.只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符
- 4.next()不能得到带有空格的字符串
-
nextLine():
- 1.以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符
- 2.可以获得空白
-
有if的两种
package com.Kuang.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()==true){
//使用next方式接收
String str =scanner.next(); //程序会等待用户输入
System.out.println("输入的内容为:"+str);
//凡是使用IO流的类如果不关闭会一直占用资源,要养成良好习惯用完就关掉
scanner.close();
}
}
}
package com.Kuang.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.hasNext());{
String str = scanner.nextLine(); //等待用户输入
System.out.println("输出的内容为:"+str);
}
scanner.close();
}
}
没有if同样适用
package com.Kuang.scanner;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数据:");
String str = scanner.nextLine();
System.out.println("输出的内容为:"+str);
scanner.close();
}
}
Scanner进阶使用
package com.Kuang.scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//从键盘接收数据
int i = 0;
float f = 0.0f;
System.out.println("请输入小数:");
//如果..那么
if (scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("整小数数据:"+ f);
}else{
System.out.println("输入的不是小数数据!");
}
System.out.println("请输入整数:");
//如果..那么
if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整数数据:"+ i);
}else{
System.out.println("输入的不是整数数据!");
}
scanner.close();
}
}
例题:我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果
以下是狂神操作
package com.Kuang.scanner;
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;
System.out.println("请输入数据");
//通过循环判断是否还有输入,并在里面对每一次进行求和统计
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
m = ++m;//m++ m=m+1
sum = sum+ x;
System.out.println("你输入了第"+m+"个数据,然后当前的结果是sum="+sum);
}
System.out.println(m + "个数的和"+ sum);
System.out.println(m + "个数的平均值"+(sum/m));
scanner.close();
}
}
以下是我写的
package com.Kuang.scanner;
import jdk.nashorn.internal.ir.WhileNode;
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum = 0;
int m = 0;
System.out.println("请输入数据");
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
m++;
sum = sum + x;
System.out.println("你输入到第"+m+"个数据,当前总和为"+sum);
}
System.out.println("总共输入"+m+"个数据,总和为"+sum);
System.out.println(m+"个数据的平均值为"+sum/m);
}
}
2.顺序结构
JAVA的基本结构就是数据结构,按顺序一句一句执行
任何一个算法都离不开的一种基本算法结构
package com.Kuang.struct;
public class ShunXuDemo {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
}
}
3.选择结构
if单选择结构
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
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();
}
}
if双选择结构
ackage com.Kuang.struct;
import java.util.Scanner;
public class IfDmeo02 {
public static void main(String[] args) {
//考试分数大于60分就是及格,否则就是不及格
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();
}
}
if多选择结构
package com.Kuang.struct;
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语句至多有一个else语句,else语句在所有的 else if 语句之后
if语句可以有多个else if 语句,但他们必须在 else 语句之前
一旦其中一个else if 语句检测为true,其他的 else if 语句以及 else 语句都将跳过执行。
*/
if (score==100){
System.out.println("恭喜满分");
}else if (score<100&&score>=90){
System.out.println("A级");
}else if (score<100&&score>=80){
System.out.println("B级");
}else if (score<100&&score>=70){
System.out.println("C级");
}else if (score<100&&score>=60){
System.out.println("D级");
}else if (score<60&&score>=0){
System.out.println("不及格");
}else {
System.out.println("成绩不合法");
}
scanner.close();
}
}
嵌套的if结构
if内置无限嵌套,类似于二分法。
switch多选择结构
即swich ease 语句
-
变量类型:byte、short、int、char
-
switch支持字符串String类型了
-
同时case标签必须为字符串常量或字面量
package com.Kuang.struct;
import java.util.Scanner;
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透
char grade = 'A';
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("未知等级");
}
}
}
String字符串
package com.Kuang.struct;
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "启帆远航";
//表达式的结果可以是字符串
//字符的本质的本质还是数字
//反编译 java---class(字节码文件)---反编译(IDEA)
//
switch(name){
case"扬帆起航":
System.out.println("扬帆起航");
break;
case"启帆远航":
System.out.println("启帆远航");
break;
default:
System.out.println("不确定");
}
}
}
该文件保存下 out 为class文件 ,可以暴力导入class文件查看源码
反编译 java—class(字节码文件)—反编译(IDEA)
4.循环结构
while循环
whlie最基本的循环,结构为
while(布尔表达式){
//循环内容
}
-
布尔表达式为true,循环就会一直执行下去
-
我们大多数i情况是会让循环停止下来的,所以需要一个让表达式失效的方式来结束循环;
-
少部分需要一直循环,比如服务器的请求相应监听等。
-
要避免死循环,不然会影响程序性能或者程序造成卡死崩溃。
-
思考:计算1+2+3+…+100=?
-
package com.Kuang.struct; 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); } }
do…while循环
do{
//代码语句
}while(布尔表达式)
while和do…while 的区别
- while先判断后执行,do…while先执行后判断
- Do…while总是保证循环体至少执行一次。
For 循环
for(初始化;布尔表达式;更新){
//代码语句
}
- 虽然while和do…while都能表示所有循环,但Java提供了另一种语句——for循环,使循环结构变得更加简单;
- for是支持更迭的一种通用结构,是最有效,最灵活的循环结构。
- for循环执行的次数是在执行前就确定的。
package com.Kuang.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a = 1; //初始化条件
while(a<100){ //条件判断
System.out.println(a);//循环体
a+=2; //迭代
}
System.out.println("whlie循环结束");
for (int i = 0; i < 100; i++) {
System.out.println(i);
}
System.out.println("for循环结束");
/*
最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
执行一次循环后,更新循环控制变量(迭代银子控制循环变量的增减)。
再次检查布尔表达式。循环执行上面的过程。
*/
}
}
练习1;计算0到100之间的奇数和偶数的和
package com.Kuang.struct;
public class ForDemo02 {
public static void main(String[] args) {
//练习1;计算0到100之间的奇数和偶数的和
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i < 100; i++) {
if (i%2!=0){
oddSum+=i;
}else {
evenSum+=i;
}
}
System.out.println("奇数的和:"+oddSum);
System.out.println("偶数的和:"+evenSum);
}
}
练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
for练习
package com.Kuang.struct;
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
//println 输出完会换行
//print 输出完不会换行
for (int i = 0; i <=1000; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if(i%(5*3)==0){
//System.out.print("\n");
System.out.println();
}
}
}
}
while练习
package com.Kuang.struct;
public class ForDemo04 {
public static void main(String[] args) {
int i = 0;
while(i<=1000) {
i++;
if (i%5==0){
System.out.print(i+"\t");
}
if(i%(5*3)==0){
//System.out.print("\n");
System.out.println();
}
}
}
}
练习3:九九乘法表
package com.Kuang.struct;
public class ForDemo05 {
public static void main(String[] args){
for (int j = 1; j <=9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");//此处System.out.print(i+"*"+j+"="+(j*i)+"\t");这样写就会比较整齐。
}
System.out.print("\n");
}
}
}