通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据
基础语法:
Scanner s=new Scanner(System.in);
- next():
- 一定要读取到有效字符后才会结束输入
- next()不能输入带有空格的字符串(以空白为结束符)
public class Dome1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); //创建一个扫描器对象,用于接收键盘数据
System.out.println("请输入字符串:");
if (scanner.hasNext()){ //判断用户有没有输入字符串
String str= scanner.next(); //使用next方式接收,程序会等待用户输入完毕
System.out.println("输入的内容为:"+str);
}
scanner.close(); //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
}
}
- nextLine():
- 以Enter为结束符
- 可以获得空白
public class Dome2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入字符串");
if (scanner.hasNextLine()){
String str= scanner.nextLine() ;
System.out.println("输出字符串为:"+str);
}
scanner.close();
}
}
可简化为:
public class Dome2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入字符串");
String str= scanner.nextLine() ;
System.out.println("输出字符串为:"+str);
scanner.close();
}
}
常用nextLine()
小练习
- 输入多个数,求其和和平均数,没输入一个数字用回车确认,通过出入非数字来结束输入并输出执行结果
import java.util.Scanner;
public class Dome4 {
public static void main(String[] args) {
int sum=0; //定义和变量
int m=0; //计算输入了数字的个数
int a=0; //定义输入的数字
Scanner scanner = new Scanner(System.in);
System.out.println("请输入多个数字,每输入一个数字用回车确认");
while (scanner.hasNextInt()){
a= scanner.nextInt();
m=m+1; //m++
sum=sum+a;
}
System.out.println(m+"个数字之和为:"+sum);
System.out.println(m+"个数字的平均数为:"+(sum/m));
scanner.close();
}
}
顺序结构
自上而下依次运行
选择结构 if单选择结构语法:
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
if双选择结构
语法:
if (布尔表达式) {
//如果布尔表达式为true将执行的语句
} else {
//如果布尔表达式为false将执行的语句
}
if多选择结构
语法:
if (布尔表达式1) {
//如果布尔表达式1为true将执行的语句
} else if (布尔表达式2){
//如果布尔表达式2为true将执行的语句
}else if (布尔表达式3){
//如果布尔表达式3为true将执行的语句
}else{
//如果以上布尔表达式都为false将执行的语句
}
嵌套的if结构
语法:
if (布尔表达式1){
//如果布尔表达式1为true将执行的语句
if (布尔表达式2) {
//如果布尔表达式2为true将执行的语句
}
}
switch多选择结构
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为分支。
switch 语句中的变量类型可以是:
- byte,short,int或者char
- 支持字符串String类型
- case标签必须为字符串常量或者字面量
语法:
switch (expression){
case value:
//语句
break; //可选,不加可以执行但会出现穿透现象
case value:
//语句
break; //可选,不加可以执行但会出现穿透现象
//你可以有任意数量的case语句
default: //可选
//语句
}
穿刺现象:
char garde='2';
switch (garde){
case '1':
System.out.println("大于");
case '2':
System.out.println("等于");
case '3':
System.out.println("小于");
default:
System.out.println("未知");
}
char garde='2';
switch (garde){
case '1':
System.out.println("大于");
break;
case '2':
System.out.println("等于");
break;
case '3':
System.out.println("小于");
break;
default:
System.out.println("未知");
}
在匹配到对应的值的时候,会输出break之前的所有语句
if语句和switch语句的区别if语句判断匹配的是一个区间
switch语句判断匹配的是一个值
循环结构 while循环语法:
while (布尔表达式){
//循环内容
}
do…while循环
至少会执行一次
语法:
do{
//循环内容
}while (布尔表达式);
while和do…while的区别
while先判断后执行,do…while先执行后判断
for循环是支持迭代的一种通用结构,是最有效最灵活的循环结构
语法:
for (初始化;布尔表达式;更新){
//循环内容
}
for(; ; ){ //死循环结构
}
小练习
- 分别输出100以内奇数和偶数的和
public class While {
public static void main(String[] args) {
int sum=0;
int sum1=0;
for (int i=0;i<=100;i=i+2){
sum+=i;
}
for (int a=1;a<=100;a=a+2){
sum1+=a;
}
System.out.println("100以内的偶数之和为"+sum);
System.out.println("100以内的奇数之和为"+sum1);
}
}
- 输出1~1000内能被5整除的数,并且每行输出3个
public class While {
public static void main(String[] args) {
int sum=0;
int sum1=0;
for (int i=1;i<=1000;i++){
if (i%5==0){
System.out.print(i+"t");
}
if (i%5*3==0){
System.out.println();
}
}
}
}
println输出完会换行
print输出完不会换行
- 输出九九乘法表
public class While {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int i1 = 1; i1 <= i; i1++) {
System.out.print(i1 + "*" + i + "=" + i1 * i + "t");
}
System.out.println();
}
}
}
步骤:
- 打印第一列
- 把固定的1用循环包起来
- 去掉重复项
- 调整样式
语法:
for (声明语句:表达式){
//循环内容
}
break continue
任何循环的主体部分,都可以用break终止,强行退出整个循环,但不会终止程序
continue用于终止某一次循环
练习- 绘制三角形练习
public class Break {
public static void main(String[] args) {
for (int i = 1; i <=5; i++) {
for (int j=5; j >=i; j--) {
System.out.print(" ");
}
for (int j=2; j <=i; j++) {
System.out.print("*");
}
for (int j=2; j <=i; j++) {
System.out.print("*");
}
System.out.println("*");
}
}
}



