一 Scannery对象
java提供一个人机交互工具类java.util.Scanner,我们可以通过Scanner类获取用户的输入
基本语法:
Scanner s = new Scanner(System.in);//开始
...
String str.scanner.nextLine();
...
scanner.close()//关闭
获取输入的方法
- next(),一般读取前需要使用hasNext()判断是否还有输入的数据
- 一定要读到有效字符后才结束输入
- 对输入有效字符之前遇到的空白,next()方法会自动将其去掉
- 只有输入有效字符后才将后面输入的空白作为分隔符或结束符
- next()不能得到带有空格的字符串
- nextLine(),一般读取前需要使用hasNextLine()判断是否还有输入的数据
- 以回车enter作为结束符,也就是返回 回车前的所有字符
二 顺序结构
三选择结构
-
if单选择结构
package com.eighteen.struct; import java.util.Scanner; public class ifDemo01 { 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); }else { System.out.println("end"); } scanner.close();//关闭scanner } }
-
if双选择结构
package com.eighteen.struct; import java.util.Scanner; public class ifDemo02 { 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多选择结构
package com.eighteen.struct; import java.util.Scanner; public class ifDemo03 { 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>=90){ System.out.println("A级"); }else if (score<90 && score>=80){ System.out.println("B级"); }else if (score<80 && score>=60){ System.out.println("C级"); }else if (score<60 && score>=0){ System.out.println("D级"); }else { System.out.println("成绩不合法"); } scanner.close(); } }
-
switch多选择结构-分支结构
package com.eighteen.struct; public class SwitchDemo02 { public static void main(String[] args) { String name = "纪伟"; //JDK7的新特性,表达式结果可以是字符串!!! //字符的本质还是数字 //反编译 java -- class(字节码文件) -- 反编译(IDEA) switch (name){ case "纪伟": System.out.println("信息匹配,姓名是纪伟"); break; case "纪虹": System.out.println("信息不匹配!"); break; default: System.out.println("???"); } } }
四 循环结构
- while循环
package com.eighteen.struct;
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+3+...+100
int i = 0;
int sum = 0;
while (i<=100){
sum += i;
i++;
}
System.out.println("输出结果为:"+sum);
}
}
- do…while循环
package com.eighteen.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i;
i ++ ;
}while (i<=100);
System.out.println(sum);
}
}
- for循环
package com.eighteen.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a = 1; //初始化条件
while (a<=100){ //条件判断
System.out.println(a); //循环体
a += 2; //迭代
}
System.out.println("while循环结束!");
for (int i=1; i<=100; i++){
System.out.println(i);
}
System.out.println("for循环结束!");
// 100.for可以直接出来一个100的循环
// 这是一个死循环
for (; ; ){
}
}
}
代码示例:九九乘法表
package com.eighteen.struct;
public class ForDemo04 {
public static void main(String[] args) {
//九九乘法表
//1.我们先打印第一列
//2.我们把固定的1再用一个循环包起来
//3.去掉重复项,i<j
//4.调整样式
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j + "*" + i + "=" + j*i + "\t");
}
System.out.println();
}
}
}
- 增强for循环