Java流程控制
1.用户交互Scanner
java.util.Scanner,我们可以通过Scanner类来获取用户的输入。
基本语法:Scanner s = new Scanner(System.in);
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。
实例next:
public class Demo1 {
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();
}
}
实例nextLine:
public class Demo2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用hasNext方式接收:");
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("输出内容为:"+str);
}
scanner.close();
}
}
next()与nextLine()的区别:
next():
1.一定要读取到有效字符后才可以结束输入。
2.对输入有效字符之后遇到的空白,next()方法会自动将其去掉。
3.只有输入有效字符后才将其后面输入的空白作为分隔符或结束符。
4.next不能得到带有空格的字符。
nextLine():
1.以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。
2.可以获得空白。
nextFloat()/nextInt()等:
public class Demo3 {
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("你输入的不是整数数据!");
}
}
}
求和、平均数
public class Demo04 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和、平均数,没输入一个数字用回车确认,通过输入非数字来结束并输出执行结果。
Scanner scanner = new Scanner(System.in);
double sum = 0;
int m = 0;
while (scanner.hasNextDouble()){
Double d = scanner.nextDouble();
m++;
sum = sum+d;
}
System.out.println("和:"+sum);
System.out.println("平均:"+sum/m);
scanner.close();
}
}
2.顺序结构
JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
顺序结构是最简单的算法结构。
语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。
实例:
public class Demo5 {
public static void main(String[] args) {
System.out.println(01);
System.out.println(02);
System.out.println(03);
System.out.println(04);
System.out.println(05);
}
}
3. 选择结构
if单选择结构
实例:
public class Demo6 {
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("可以");
}
System.out.println("不可以");
scanner.close();
}
}
if双选择结构
实例:
public class Demo6 {
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多选择结构
实例:
public class Demo6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入分数:");
int score = scanner.nextInt();
if (score == 100){
System.out.println("膜拜!");
}else if (score < 100 && score >= 80){
System.out.println("哎呦,不错嘛!");
}else if (score < 80 && score>=60){
System.out.println("哎呀妈呀!吓我一跳!");
}else{
System.out.println("呜呜呜,要挨打!!");
}
scanner.close();
}
}
switch多选择结构
多选择结构还有一个实现方式就是switch case语句。
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
switch 语句中的变量类型可以是:
byte、short、int、char、String同时case标签必须为字符串常量或字面量。
实例:
public class Demo7 {
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;
default:
System.out.println("愁人的娃!");
}
}
}
4. 循环结构
while 循环
只要布尔值表达式为true,循环就会一直执行下去。
我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。
少部分情况需要循环一直执行,比如服务器的请求响应监听等。
循环条件一直为true就会造成无限循环,避免死循环!
实例:
public class Demo8 {
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 循环
对于while语句而言,如果不满足条件,则不能进入循环,但有时候我们需要即使不满足条件,也至少执行一次。
do...while循环和while循环相似,不同的是,do...while循环至少会执行一次。
while和do...while的区别:
while先判断后执行,do...while先执行后判断。
do...while总是保证循环体至少执行一次,这是主要的差别。
实例:
public class Demo8 {
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);
i++;
}while (i<0);
}
}
For循环(重点)
1.最然所有的循环结构都可以用while或者do...while表示,但Java提供了另一种语句——For循环,使一些循环结构变得更加简单。
2.for循环语句是支持迭代的一种通用结构,是最有效最灵活的循环结构。
3.for循环执行的次数是在执行前就确定的。
实例:0-100之间奇数和偶数的和
public class Demo9 {
public static void main(String[] args) {
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);
}
}
实例:输出1-1000之间能被5整除的数,并且每行输出3个
public class Demo10 {
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){
System.out.println();
}
}
}
}
实例:九九乘法表
public class Demo11 {
public static void main(String[] args) {
for (int i = 0; i <= 9; i++) {
for(int j = 0;j<=i;j++){
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
增强For循环
语法:
for(声明语句:表达式){
//代码语句
}
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配,其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
实例:
public class Demo12 {
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);
}
}
}
5. break、continue
break:在任何循环语句的主体部分,均可用break控制循环的流程,break用于强行退出循环,不执行循环中剩余的语句。
continue:用在循环语句体中,用于终止某次循环过程,即跳出循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
实例:
public class Demo13 {
public static void main(String[] args) {
for (int i = 0;i<10;i++){
if (i==5){
//break;
continue;
}
System.out.println(i);
}
}
}
浙公网安备 33010602011771号