Java SE 流程控制 Struct
Scanner
package Scanner;import java.util.Scanner;public class Demo3 {public static void main(String[] args) {//从键盘接收数据Scanner scanner = new Scanner(System.in);System.out.println("使用nextLine方式接收:");String str = scanner.nextLine();System.out.println("输出的内容为: "+str);//属于IO流的类如果不关闭会一直占用资源scanner.close();}
}
int a = scanner.nextInt();
double b = scanner.nextDouble();
next():
- 一定要读取到有效字符后才开始结束输入;
- 输入有效字符后才将其后面输入的空白作为分隔符或者结束符;
- next()不能得到带有空格的字符串;
nextLine():
- 以ENTER为结束符,nextLine()方法返回的是输入回车之前的所有字符;
- 可以获得空白;
选择结构
- if单选择
- if-else双选择
- if-else if-else多选择
- switch
- JDK支持了Srtring类型
- case穿透现象
- break
- default
if单选泽
package Struct;import java.util.Scanner;public class IfDemo1 {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-else双选择
package Struct;import java.util.Scanner;public class IfDemo2 {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();}}
if-else if-else多选择
package Struct;import java.util.Scanner;public class IfDemo3 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入你的成绩");//int score = scanner.nextInt();float score = scanner.nextFloat();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("不及格");}else{System.out.println("输入不合法");}scanner.close();}
}
switch
package Struct;import java.util.Scanner;public class IfDemo3 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入你的成绩");//int score = scanner.nextInt();float score = scanner.nextFloat();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("不及格");}else{System.out.println("输入不合法");}scanner.close();}
}
package Struct;public class Switch2 {public static void main(String[] args) {String name = "卡莎";//JDK7的新特性 表达式结果可以是字符串//字符的本质还是数字//反编译 java---class(字节码文件)---反编译(IDEA)switch (name){case "卡莎":System.out.println("虚空之女");break;case "崔丝塔娜":System.out.println("麦林炮手");break;case "艾希":System.out.println("寒冰射手");break;default:System.out.println("含羞蓓蕾");}}
}
循环结构
- while & do while
- for
- for-each增加for循环
while & do while
- while先判断再执行
- do while先执行再判断
package Struct;public class DoWhileDemo1 {public static void main(String[] args) {int i = 0;while(i<0){i++;System.out.println(i);//没有执行}do {i++;System.out.println(i);//执行了两次}while (i<2);}
}
for
100以内奇数和偶数的和
package Struct;public class ForDemo2 {public static void main(String[] args) {int evenSum = 0;int oddSum = 0;for (int i = 0; i <= 100; i++) {if (i%2==0){//偶数evenSum = evenSum + i;}else{//奇数oddSum = oddSum + i;}}System.out.println("偶数和为:"+evenSum);System.out.println("奇数和为:"+oddSum);}
}
package Struct;public class ForDemo3 {public static void main(String[] args) {//输出1000以内可以被5整除的数,每行三个for (int i = 0; i <= 1000; i++) {if (i%5==0){System.out.print(i+"\t");}if (i%15==0) {//3×5System.out.println("");}//System.out.print不换行输出//System.out.println换行输出}}
九九乘法表
package Struct;public class ForDemo5 {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();}}
}
增加for循环
package Struct;public class ForDemo6 {public static void main(String[] args) {int[] numbers ={10,20,30,40,50};//定义了一个数组for (int i = 0; i < 5; i++) {System.out.println(numbers[i]);}System.out.println("=========================");//遍历数组的元素for(int x : numbers){System.out.println(x);}}
}
break&continue
package Struct;public class BreakContinueDemo1 {public static void main(String[] args) {//break用于强行退出循环//continue用于终止某次循环过程int i = 0;while(i<=100){i++;if (i%10==0){System.out.println(i);break;}}System.out.println(i);}
}
练习
Test1 打印三角形
思路:
package Struct;public class TestDemo1 {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("*");}for (int j = 5; j >= i; j--) {//可以不写System.out.print(" ");}System.out.println();}}
}
输出:
* *** ***** ******* *********
Test2 质数
- 标签的方法
package Struct;public class LabeDemo1 {public static void main(String[] args) {//打印101-150之间所有的质数int count = 0;outer:for (int i = 101; i <= 150; i++) {for (int j = 2; j < i/2; j++) {if (i%j==0){//不是质数continue outer;}}System.out.println(i);}}
}
- 布尔值flag
package Struct;public class LabeDemo2 {public static void main(String[] args) {//101到150以内的质数;for (int i = 101; i <= 150; i++) {boolean flag = true;for (int j = 2; j < i/2; j++) {if (i%j==0){//不是质数flag = false;}}if(flag){System.out.print(i+" ");}}}
}
输出:
101 103 107 109 113 127 131 137 139 149
Process finished with exit code 0
Java SE 流程控制 Struct相关推荐
- 黑马 程序员——Java基础---流程控制
黑马程序员--Java基础---流程控制 ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------ 一.概述 Java提供了两种基本的流程控制结构:分支结构 ...
- Java架构师成长之道之Java程序流程控制
Java架构师成长之道之Java程序流程控制 Java架构师成长之道 4.1 程序流程控制概述 之前编写的绝大多数程序都是顺序执行,也就是从main函数开始,由上到下一行一行的执行,也被称为顺序结构. ...
- Java程序流程控制(顺序结构、分支结构、循环结构、if-else、switch-case、for、while、do-while、break、continue、return)
流程控制语句是用来控制程序中各语句执行顺序的语句,可以把语句组合成能完成一定功能的小逻辑模块. 其流程控制方式采用结构化程序设计中规定的三种基本流程结构,即: 顺序结构 ...
- 第三堂:Java程序流程控制
在程序设计时,我们常常需要改变程序的控制流程.语句的执行顺序.而我们有三种基本的技术可以改变这个流程:①调用方法,②选择,③循环. 流程控制是所有编程语言的基础部分,Java自然也不例外:Java的流 ...
- Java程序流程控制(符号函数sgn、闰年判断)
选择.循环.跳转 块(block):复合语句:由一对{ }起来的Java语句 block控制着变量的作用域(scope) public static void main(String[] args){ ...
- java程序流程控制
写到这里,才算正式进入编程语言. 之前的都是基础中的基础,用数学方式发打比方来说,就是学习阿拉伯数字和简单的四则运算,现在开始做应用题了 程序流程控制分为 1.顺序结构 程序从上到下逐行执行,中间没有 ...
- Java基础+流程控制+方法+数组【笔记含代码】
文章目录 什么是计算机 计算机硬件 计算机软件 DOS命令 计算机语言发展史 第一代语言 第二代语言 第三代语言 Java帝国的诞生 C & C++ 反抗 Java初生 Java发展 Java ...
- java程序流程控制_java程序流程控制
顺序结构 分支结构(条件结构) 循环结构 控制循环结构 顺序结构: 如果代码里没有流程控制,程序是至上而下一行一行执行的,一条语句执行完之后继续执行下一条语句,直到程序的最后. if语句: 基本语法: ...
- java高级流程控制多线程作业设计_Java高级-解析Java中的多线程机制
线程的状态控制 在这里需要明确的是:无论采用继承Thread类还是实现Runnable接口来实现应用程序的多线程能力,都需要在该类中定义用于完成实际功能的run方法,这个run方法称为线程体(Thre ...
最新文章
- python Intel Realsense D435 多线程资源分配问题(卡住、卡死)
- numpy高级操作,求高维矩阵的距离矩阵(方阵)以及 求某一个维度的累加和 , 矩阵切片操作
- CSS3的弹性盒子flex详解(2)
- Linux 普通用户和超级用户的切换
- SpringBoot集成Cache缓存(Ehcache缓存框架,注解方式)
- mr图像翻转的原因_MR成像技术讲解
- Linux Windows下忘记mysql超级管理员root密码的解决办法
- 高速公路、铁路交通的常识
- Spring Security构建Rest服务-1401-权限表达式
- SpringBoot项目的测试类
- random.choice与random.choices
- 【优化覆盖】基于matlab模因算法求解集群无线传感器网络中节能覆盖控制优化问题【含Matlab源码 1563期】
- python全局名称空间_21、Python之名称空间与作用域
- 第1章 Linux内核概述
- 利用python构建信用卡评分
- 几款入夏品牌包包可以看看
- SQL 汉字转拼音函数(转)+将表中汉字转拼音
- 台式计算机找不到蓝牙发射器,电脑网络适配器里没有蓝牙怎么办
- 什么是串扰crosstalk
- 【ESP8266 ES01 小爱】使用ESP 8266 WOL 远程唤醒电脑
热门文章
- ASP.NET基本学习路线
- 1060显卡支持dx12吗_“显卡危机”再次上演?Crytek发布光追测试程序Neon Noir
- linux 端口complain,Linux命令Warning:bad syntax, perhaps a bogus '-'?的解决方法-吾爱编程网...
- 海天味业创意制作表白瓶感恩母亲,唤醒饱含亲情的味道记忆
- 学tlc和JAVA,SIMULINK_S-Function_TLC_RTW基础(给初学者)
- 从女人的24个死穴下手! 1
- 全国城市三级联动 html+js
- B站发帖软件哪个好用?好用的哔哩哔哩发帖工具
- ArcGIS小知识(十三)——出图精度:DPI和比例尺以及分辨率
- Win10系统如何安装配置maven