Scanner对象
我们可以通过Scanner类来获取用户的输入
基本语法: Scanner s = new Scanner(System.in);
通过Scanner类的next()与nextLine()方法获取输入的字符串
在读取之前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据
next()方法
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接受:");
//判断用户有没有输入字符串
if(scanner.hasNext()==true){
//使用next方式接收
String str = scanner.next();
System.out.println("输出的内容为:"+str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close();
}
}
输入结果:
nextLine()方法
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接受:");
//判断用户有没有输入字符串
if(scanner.hasNextLine()){
//使用nextLine方式接收
String str1 = scanner.nextLine();
System.out.println("输出的内容为:"+str1);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close();
}
}
输出结果:
next():
- 一定要读取到有效字符后才可以结束输入
- 对输入有效字符之前遇到的空白,next()方法会自动将其去掉
- 只有输入有效字符后才能将其后面输入的空白作为分隔符或者结束符
- next()不能得到带有空格的字符串
nextLine():
- 以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符
- 可以获得空白
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("输入的不是整数数据!");
}
scanner.close();
}
}
输出结果:
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
//和
double sum =0;
//计算输入了多少个数字
int m = 0;
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
m++;
sum = sum+x;
System.out.println("你输入了第"+m+"数据,当前结果sum为"+sum);
}
System.out.println(m+"个数的和为"+sum);
System.out.println(m+"个数的平均值为"+(sum/m));
scanner.close();
}
}
输出结果:
顺序结构
选择结构
- if单选择结构
- if双选择结构
import java.util.Scanner;
public class IfDemo2 {
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多选择结构
import java.util.Scanner;
public class IfDemo2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if (score>=90){
System.out.println("优");
}else if(score>=70 && score<90){
System.out.println("良");
}else if(score>=60 && score<70){
System.out.println("中");
}else{
System.out.println("差");
}
scanner.close();
}
}
- 嵌套的if结构
5. switc多选择结构
public class SwitchDemo1 {
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("未知等级");
}
}
}
//输出1-1000能被5整除的数,每三个一行
public class ForDemo {
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%15==0){
System.out.println("\n");
}
}
}
}
test:三角形
public class test {
//将三角形分为俩部分
public static void main(String[] args) {
//5行
for (int i=1; i <= 5; i++){
//第一部分:空格数每行递减
for (int j=5; j>=i ; j--){
System.out.print(" ");
}
//第二部分:*每行增加,每行*2i-1
for (int j=1; j<2*i ; j++){
System.out.print("*");
}
System.out.println();//换行
}
}
}
运行结果: