Java流程控制(1)
Java流程控制(1)
一、用户交互Scanner
- Scanner对象
-
之前我们学的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入。java.util.Scanner 是Java5的新特征,我们可以通过Scanner类来获取用户的输入。
-
基本语法:
Scanner s = new Scanner(System.in); -
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一-般需要使
用hasNext()与hasNextLine()判断是否还有输入的数据。
-
next():
-
一定要读取到有效字符后才可以结束输入。
-
对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
-
只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
-
next()不能得到带有空格的字符串。
-
package com.study.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()){
//使用next方式接收
String str = scanner.next();//程序会等待用户输入完毕
System.out.println("输入的内容为:"+str);
}
//凡是属于IO类的如果不关闭会一直占用资源,要养成良好的习惯用完就关掉
scanner.close();
}
}
运行结果:
使用next方式接受:
hello world
输入的内容为:hello
- nextLine():
package com.study.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);
}
scanner.close();
}
}
运行结果:
使用nextLine方式接收:
hello world
输入的内容是:hello world
二、顺序结构
-
JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序-句-句执行。
-
顺序结构是最简单的算法结构。
-
语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤
组成的,它是任何一个算法都离不开的一种基本算法结构。
三、选择结构
-
if单选择结构
-
我们很多时候需要去判断f个东西是否可行,然后我们才去执行,这样-个过程在程序中用if语
句来表示 -
语法:
if(布尔表达式){
布尔表达式.
//如果布尔表达式为true将执行的语句
}package com.study.struct; import java.util.Scanner; public class IfDemo01 { 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(str); } System.out.println("End"); scanner.close(); } } 运行结果: 请输入内容 hello hello End
-
-
if双选择结构
-
语法:
if(布尔表达式){
//如果 布尔表达式的值为true
}else{
//如果 布尔表达式的值为false
}package com.study.struct; import java.util.Scanner; public class IfDemo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入你的成绩:"); double score = scanner.nextDouble(); if (score>=60){ System.out.println("及格"); }else { System.out.println("不及格"); } scanner.close(); } } 运行结果: 请输入你的成绩: 66 及格
-
-
if多选择结构
-
语法:
if(布尔表达式1){
//如果布尔表达式1的值为true执行代码
}else if(布尔表达式2){
//如果布尔表达式2的值为true执行代码
}else if(布尔表达式3){
//如果布尔表达式3的值为true执行代码
}else {
//如果以上布尔表达式都不为true执行代码}
package com.study.struct; import java.util.Scanner; public class IfDemo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入你的成绩:"); double score = scanner.nextDouble(); /* if语句至多有1个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<90&&score>=80){ System.out.println("B"); }else if (score<80&&score>=70){ System.out.println("C"); }else if (score<70&&score>=60){ System.out.println("D"); }else if (score<60&&score>=0){ System.out.println("E"); }else { System.out.println("输入错误!"); } scanner.close(); } } 运行结果: 请输入你的成绩: 66 D
-
-
嵌套的if结构
-
使用嵌套的if...else语句是合法的。也就是说你可以在另一个if或者else if语句中使用if或
者else if语句。你可以像if语句一样嵌套else if...else. -
语法:
if(布尔表达式1){
////如果布尔表达式1的值为true执行代码
if(布尔表达式2){
////如果布尔表达式2的值为true执行代码
}
}
-
-
switch多选择结构
-
多选择结构还有一一个实现方式就是switch case语句。
-
switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
-
switch语句中的变量类型可以是:
- byte、 short. int 或者char.
- 从JavaSE7开始
- switch 支持字符串String类型了
- 同时case标签必须为字符串常量或字面量。
-
语法:
switch(expression){ case value : //语句 break; //可选 case value : //语句 break; //可选 //你可以有任意数量的case语句 default : //可选 //语句 } -
实例
package com.study.struct; public class SwitchDemo01 { public static void main(String[] args) { String grade = "中原"; //JDK7的新特性,表达式结果可以是字符串! ! ! // 字符的本质还是数字 //反编译 java---class (宁节码文件) ---反编用(IDEA) switch (grade){ case "中原": System.out.println("优秀"); break; case "中原1": System.out.println("良好"); break; case "中原2": System.out.println("及格"); break; case "中原3": System.out.println("再接再厉"); break; case "中原4": System.out.println("挂科"); break; default: System.out.println("呵呵"); } } } 运行结果: 优秀
-

浙公网安备 33010602011771号