// 创建一个扫描器对象,用于接收键盘输入
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接受:");
// 判断用户有没有输入字符串
if(scanner.hasNext()) { // 这里也可以使用hasNextLine
// 使用next方式接受
String str = scanner.next(); // 如果上面使用的是hasNextLine,那么这里就需要使用nextLine
System.out.println("输出的内容:"+str);
}
// 凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close();
next()和nextLine()的区别:
java的基本结构就是顺序结构,代码按照顺序从上到下依次执行。
if(true) {
}
if(true){
} else {
}
if(true){
}else if(true){
}else{
}
if(true){
if(true){ }
}
switch一般用来判断一个变量与一系列中某个值是否相等,每个值称为一个分支。
switch (expression) {
case value:
// 语句
break;
case value2:
//语句
break;
default:
//语句
}
注意事项:
IDEA可以通过将字节码文件拖动到项目文件夹下后自动反编译。通过反编译,我们可以看到Switch的源码是
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package oop;
public class demo01 {
public demo01() {
}
public static void main(String[] args) {
String name = "苏木";
byte var3 = -1;
switch(name.hashCode()) {
case 1064505:
if (name.equals("苏木")) {
var3 = 0;
}
default:
switch(var3) {
case 0:
System.out.println("苏木");
break;
default:
System.out.println("错误");
}
}
}
}
do{
//循环体
} while(布尔表达式);
package oop;
public class ForDemo01 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};
for (int x:numbers) {
System.out.println(x);
}
}
}
只有少数时候需要循环一直进行下去,比如服务器的请求监听相应等
原文:https://www.cnblogs.com/sumuKiko/p/14464146.html