1.Scanner键盘输入对象
-
导包 import java.until.Scanner
-
创建键盘输入对象 Scanner input = new Scanner(System.in)
-
通过键盘输入给变量赋值
nextInt(); 获取用户输入的整数;
nextDouble; 获取用户输入的小数
nextBoolean(); 获取用户输入的boolean值
nextLine(); 结果是String类型,获取用户输入的字符串
next(); 结果是String类型,获取用户输入的字符串
这些方法统称为阻塞方法: 程序执行到这个方法的时候,会阻塞主线程,程序不会继续执行,知道用户输入结束,程序继续执行
例子:键盘录入两个数,并将这两个数求和,求最大值,并输出
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个数: ");
int num1 = input.nextInt();
System.out.print("请输入第二个数: ");
int num2 = input.nextInt();
int sum = num1 + num2;
int max = (num1 > num2) ? num1 : num2;
System.out.println("两个数的和为: " + sum + ",两个数中较大的值为: " + max);
}
}
流程控制语句结构(顺序,分支,循环)
if语句
if(条件表达式){语句体;}
执行流程:满足条件,执行语句体的内容;不满足条件,不做任何处理。
备注:如果可以用三目运算符改进的就尽量使用三目运算符。
if-else语句
单if-else语句:
if(条件表达式){语句体1;}else{语句体2;}
执行流程:满足条件,执行语句体1的内容;不满足条件,执行语句体2的内容。
多重if语句:
if(条件表达式1) {
语句体1;
} else if(条件表达式2) {
语句体2;
} else if(条件表达式3) {
语句体3;
} …(条件表达式n) {
语句体n;
} else {
语句体n+1;
}
执行流程
1.首先判断条件表达式1是否成立
成立
执行语句体1;
不成立
继续判断条件表达式2是否成立
成立
执行语句体2;
不成立
继续判断…
2.如果全部不成立,执行else语句体
例:我想买车,买什么车决定于我在银行有多少存款。
如果我的存款超过500万,我就买凯迪拉克。
否则,如果我的存款超过100万,我就买帕萨特。
否则, 如果我的存款超过50万,我就买依兰特。
否则, 如果我的存款超过10万,我就买奥托。
否则, 如果我的存款10万以下 ,我买捷安特。
import java.util.Scanner;
public class Pratice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入银行存款(万)");
double deposit = input.nextDouble();
if (deposit > 500)
System.out.println("买凯迪拉克");
else if (deposit > 100 && deposit < 500)
System.out.println("买帕萨特");
else if (deposit > 50 && deposit < 100)
System.out.println("买伊兰特");
else if (deposit > 10 && deposit < 50)
System.out.println("买奥托");
else
System.out.println("捷安特");
}
}
switch多分支选择结构
根据表达式值的不同执行许多不同的操作
switch (表达式) {
case 值1 :
语句序列;
[break];
case 值2:
语句序列;
[break] ;
… … … … …
default:
默认语句 ;
[break];
}
switch语句特点:
1.switch表达式可以是变量也可以是常量;
2.case值必须是常量表达式
3.break表示退出swtich语句体,如果case或者default子句没有break语句,会出现break穿透现象
break穿透现象: 从上至下逐级往下执行,知道碰到break停止
for循环
for(1.初始化语句;2.循环条件语句;4.控制条件语句) {
3.循环体语句;
}
//求1-100的和。
public class Demo02 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println(sum);
}
}
//求1~100之间不能被3整除的数之和
/*
* 1.找出不能被3整数的数(x % 3 !=0)
* 2.求和
*/
public class Demo03 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 3 != 0) {
sum += i;
}
}
System.out.println("1~100之间不能被3整除的数之和是: " + sum);
}
}
while循环
/*
* 求1~100之间偶数的和
*/
public class WhileDemo {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
if (i % 2 == 0) {
sum += i;
}
i++;
}
System.out.println(sum);
}
}
for循环和while循环的区别:
-
for循环的循环变量作用于循环体内,在循环体外循环变量自动销毁
-
while循环的循环变量作用于全局
-
for循环一般针对循环次数确定的情况下可读性更强
-
while循环一般针对循环次数不确定的情况可读性更强
do-while循环
1.初始化语句;
do {
3.循环体语句;
4.控制条件语句;
} while(2.循环条件语句);
-
do-while和while的区别:
-
两者都是针对循环次数不确定的情况
-
while是先判断循环条件是否成立,再来执行循环体
-
而do-while是先执行一次循环体语句,再来判断循环条件是否成立
import java.util.Scanner; /*循环输入商品编号和购买数量 当输入n时结账 结账时计算应付金额并找零*/ public class Demo { public static void main(String[] args) { System.out.println("******************************"); System.out.println("请选择购买的商品编码:"); System.out.println("1.T恤 2.网球鞋 3.网球拍"); System.out.println("******************************"); Scanner input = new Scanner(System.in); String isContinue = ""; // 定义变量保存应付金额 double cost = 0.0; do { System.out.println("请输入商品编码"); int code = input.nextInt(); System.out.println("请输入购买数量"); int number = input.nextInt(); //定义变量保存商品价格 double prize = 0.0; //定义变量保存商品名称 String goodsName = ""; switch (code) { case 1: goodsName = "T恤"; prize = 245.0; break; case 2: goodsName = "网球鞋"; prize = 570.0; break; case 3: goodsName = "网球拍"; prize = 320.0; break; default: System.out.println("您输入的商品不存在!"); break; } //定义变量保存商品总价 double money = 0.0; money = prize * number; System.out.println(goodsName + " ¥ " + prize + " 数量" + number + " 合计¥" + money); cost += money; System.out.println("是否继续 (y/n)"); isContinue = input.next(); } while (isContinue.equals("y") || isContinue.equals("y")); //定义变量保存折扣值 double discount = 0.8; System.out.println("折扣: " + discount); System.out.println("应付金额: " + (cost * discount)); System.out.println("实付金额:"); double repay = input.nextDouble(); while (repay < (cost * discount)) { System.out.println("您输入的金额小于应付金额,请重新输入: "); repay = input.nextDouble(); } System.out.println("找钱: " + (repay - cost * discount)); } }
执行结果如下图:
嵌套循环
格式:
for(表达式一;表达式二;表达式三){
for(表达式一;表达式二;表达式三){
}
}
public class Demo {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}
System.out.println();
}
}
}
输出结果如下图所示:
例2:嵌套循环打印99乘法表
public class PrintMultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (i * j) + " ");
}
System.out.println();
}
}
}