Java流程控制
Scanner对象
- 我们可以通过Scanner类来获取用户的输入
- 基本语法
- 通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。
- 实例
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();
}
}
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);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close();
}
}
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;//m++
sum = sum + x;
System.out.println("你输入了第" + m + "个数据,然后当前的结果sum=" + sum);
}
System.out.println(m + "个数的和为:" + sum);
System.out.println(m + "个数的平均数为:" + sum / m);
scanner.close();
}
}
顺序结构
-
JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
-
顺序结构是最简单的算法结构。
- 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。
选择结构
if 单选择结构
- 我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程在程序中用if语句来表示
- 语法:
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容:");
String s = scanner.nextLine();
//equals:判断字符串是否相等
if(s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
if 双选择结构
- 语法:
public class IfDemo02 {
public static void main(String[] args) {
//考试分数大于60就是及格,小于60分为不及格。
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
if(score > 60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
scanner.close();
}
}
if 多选择结构
- 语法:
public class IfDemo03 {
public static void main(String[] args) {
//考试分数大于60就是及格,小于60分为不及格。
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
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 >= 60){
System.out.println("C级");
}else if(score<60){
System.out.println("D级");
}else {
System.out.println("成绩不合法");
}
scanner.close();
}
}
- 注意
- if语句至多有1 个else 语句,else语句在所有的else if语句之后。
- if语句可以有若干个 else if语句,它们必须在else语句之前。
- 一旦其中一个else if 语句检测为 true,其他的else if以及else 语句都将跳过执行。
if 嵌套结构
- 语法:
switch 选择结构
-
多选择结构还有一个实现方式就是switch case 语句。
-
switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
-
switch语句中的变量类型可以是:
- byte、short、int或者char。
- 从Java SE7开始
- switch支持字符串String类型了
- 同时case标签必须为字符串常量或字面量。
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透 switch匹配一个具体的值
char grade = 'F';
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("等级未知");
}
}
}
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "Gao";
//JDK7的新特性,表达式结果可以是字符串!!!
// 字符的本质还是数字
//反编译java---class(字节码文件)----反编译(IDEA)
switch (name){
case "Hao":
System.out.println("Hao");
break;
case "Gao":
System.out.println("Gao");
break;
default:
System.out.println("错啦");
}
}
}
循环结构
while循环
-
while是最基本的循环,它的结构为:
-
只要布尔表达式为true,循环就会一直执行下去。 -
我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。
-
少部分情况需要循环一直执行,比如服务器的请求响应监听等。
-
循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死奔溃!
public class WhileDemo02 {
public static void main(String[] args) {
//计算1+2+。。。+100的值
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的区别:
- while先判断后执行。dowhile是先执行后判断!
- 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 DoWhile02 {
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);
}
}
for 循环
-
for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
-
for循环执行的次数是在执行前就确定的。语法格式如下:
-
关于for循环有以下几点说明:
- 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
- 然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
- 再次检测布尔表达式。循环执行上面的过程。
public class ForDemo03 {
public static void main(String[] args) {
//练:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
for (int i = 0; i <= 1000; i++) {
if(i % 5 == 0){
System.out.print(i + "\t");
}
if(i % (5 * 3) == 0){//每行
System.out.println();
// System.out.println("\n");
}
}
//println 输出完换行
//print 输出完不换行
}
}
public class ForDemo04 {
//打印九九乘法表
/*1.先打印第一列
2.把固定的1再用一个循环包起来
3.去掉重复项, i<= j
4.调整样式
* */
public static void main(String[] args) {
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i + "*" + j + "=" + (j * i) + "\t");
}
System.out.println();
}
}
}
增强for循环
-
Java增强for循环语法格式如下:
-
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
-
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
public class ForDemo05 {
public static void main(String[] args) {
int[] number = {10, 20, 30, 40, 50};//定义了一个数组
for (int i = 0; i < 5; i++) {
System.out.println(number[i]);
}
System.out.println("====================");
//遍历数组的元素
for (int x:number){
System.out.println(x);
}
}
}
break和continue
-
break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
-
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句。接着进行下一次是否执行循环的判定。
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while (i < 100){
i++;
System.out.println(i);
if(i == 30){
break;
}
}
System.out.println("123");
}
}
public class ContinueDemo {
public static void main(String[] args) {
int i = 0 ;
while (i < 100){
i++;
// System.out.println(i);
if(i % 10 == 0){
System.out.println();
continue;
}
System.out.print(i+"\t");
}
}
}
- 打印三角形
public class TestDemo01 {
public static void main(String[] args) {
//打印三角形 5行
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();
}
}
}
【注】本文根据B站UP主遇见狂神说的Java零基础学习视频通俗易懂教程总结所得,特此感谢。