-
Scanner对象:
基本语法:Scanner scanner = new Scanner(System.in);
通过Scanner类的next()与nextLine() 方法获取输入的字符串,在读取之前
我们一般需要用hasNext()与hasNextLine()判断是否还有输入的数据举例1:使用 { next(),hasNext() } (不能得到带有空格的字符串) Scanner scanner = new Scanner(System.in); if(scanner.hasNext()){ //判断用户有没有输入字符串 String str = scanner.next(); //程序会等待用户输入完毕 sout(str); // 输入hello world ,则会输出 hello } scanner.close(); //凡是属于IO流的类,不关闭会一直占用资源,要养成良好的习惯,用完就关掉 举例2:使用 { nextLine(),hasNextLine() } (得到按回车前的所有字符) Scanner scanner = new Scanner(System.in); if(scanner.hasNextLine()){ //判断用户有没有输入字符串 String str = scanner.nextLine(); //程序会等待用户输入完毕 sout(str); // 输入hello world ,则会输出 hello world } scanner.close(); //凡是属于IO流的类,不关闭会一直占用资源,要养成良好的习惯,用完就关掉
-
选择结构:(略)
-
switch多选择结构:
switch(){
case value1 :
//语句;
break; //不加break,case会穿透,继续往下运行
case value2 :
//语句;
break;
case value3 :
//语句;
break;
default: //可选
//语句
} -
增强for循环:
int [] numbers = {10,20,30,40};
for(int x: numbers){
sout(x);
} -
continue:用于终止某次循环,即跳过循环内未执行的语句,接着进行下一次执行循环的判定
-
outer:(不建议使用)
outer:for(int i = 101 ;i < 150 :i++){
for(int i = 2 ;j < i/2;j++){
continue outer; //跳出循环,从outer开始执行
}
}
sout(i+" ");
08-10
08-10