ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

Java学习笔记:Java流程控制

Java学习笔记:Java流程控制 Java流程控制用户交互ScannerScanner对象Scanner知识点:java.util.Scanner是Java5的新特性我们可以通过Scanner类来获取用户的输入。基本语法Scanner s new Scanner(Sysem.in);通过Scanner类的next()与nextLine()方法获取输入的字符串在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入数据。next()和hasNext()import java.util.Scanner; ​ public class Demo{ public static void main(String[] args){ //创建一个扫描器对象用于接受键盘数据 Scanner scanner new Scanner(System.in); System.out.printn(使用next()方法接收); //判断用户有没有输入字符串 if(scanner.hasNext()){ //使用next()方法接受 String str scanner.next(); System.out.println(输出的内容为 str); } //凡是属于IO流的类如果不关闭会一直占用资源要养成好习惯用完关掉 scanner.close(); } }答案: 使用next()方法接收:hello world //手动输入内容输出的内容为hellonextLine()和hasNextLine()import java.util.Scanner; ​ public class Demo{ public static void main(String[] args){ //创建一个扫描器对象用于接受键盘数据 Scanner scanner new Scanner(System.in); System.out.printn(使用nextLine()方法接收); //判断用户有没有输入字符串 if(scanner.hasNextLine()){ //使用nextLine()方法接受 String str scanner.nextLine(); System.out.println(输出的内容为 str); } //凡是属于IO流的类如果不关闭会一直占用资源要养成好习惯用完关掉 scanner.close(); } }答案: 使用nextLine()方法接收:hello world //手动输入内容输出的内容为hello world注意next(): 读取有效字符后才会结束输入对输入的有效字符之前遇到的空白next()会自动去除只有输入有效字符后才会将其后面输入的吧、空白作为分割符后结束符next()不能得到带有空格的字符串。nextLine(): 以Enter为结束符可以获得空白。Scanner示例判断整数和小数import java.util.Scanner; ​ public class Demo{ 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(输入的不是整数); } System.out.println(请输入小数); //小数 if(scanner.hasNextLine()){ f scanner.nextFloat(); System.out.println(小数为 f); }else{ System.out.println((输入的不是小数)); } Scanner.close(); } }答案请输入整数10整数为10请输入小数0.52小数为0.52顺序结构public class Demo{ public static void main(String[] args){ System.out.println(hello1); System.out.println(hello2); System.out.println(hello3); System.out.println(hello4); } }答案hello1hello2hello3hello4选择结构if选择结构if单选择结构1.语法if(布尔表达式){ //如果布尔表达式为true将执行的语句 }2.代码示例public class Demo{ 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(); } }答案请输入内容11111 //用户手动输入的内容Endif双选择结构语法if(布尔表达式){ //如果布尔表达式为true将执行的语句 }else{ //如果布尔表达式为false将执行的语句 }if多选择语句语句if(布尔表达式1){ //如果布尔表达式1为true将执行的语句 }else if(布尔表达式2){ //如果布尔表达式2为ture将执行的语句 }else if(布尔表达式3){ //如果布尔表达式3为ture将执行的语句 }else{ //如果以上布尔表达式都不为true将执行的语句 }Switch多选择结构语句switch(expression){ case value : //语句 break; //可选 case value : //语句 break; //可选 //可以有任意数量的case语句 default : //可选 //语句 }switch case 语句判断一个变量与一系列值中的某一个值是否相等每一个值成为一个分支。循环结构while循环语句while(布尔表达式){ //循环内容 }只要布尔表达式为true循环就会一直执行下去。我们大多数情况是会让循环停下来我们需要一个让表达式失效的方式来几位数循环。少部分情况需要循环一直执行比如服务器的请求响应监听等。循环条件一直为true会造成无限循环【死循环】正常业务要尽量避免。会影响程序性能或造成程序卡死do...while循环语句do{ //代码语句 }while(布尔表达式);do...while循环至少会执行一次While和do...while的区别while先判断后执行。do...while先执行后判断Do...while总是保证循环体会被至少执行一次for循环1.语句for(初始值; 布尔值表达式; 更新){ //代码语句 }2.代码示例打印九九乘法表public class Demo{ public static void main(String[] args){ for(int i1; i9; i){ for(int j1; ji; j){ System.out.print(i * j (i*j) (\t)); } System.out.println(); } } }增强for循环1.语句for(声明语句 : 表达式){ //代码语句 }2.代码示例public class Demo{ public static void main(String[] args){ int[] numbers {10, 20, 30, 40}; //遍历数组的元素 for(int x : numbers){ System.out.println(x); } } }答案10203040break 和 coutinue1.Breakbreak用于强制跳出循环不执行循环内剩余语句3.Continuecontinue用于终止某次循环过程即跳过循环体中尚未执行的语句接着进行下一次是否执行循环的判断。
返回列表