流程控制
流程控制
用户交互Scanner
package com.dai.scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
//创建一个扫描对象,用于接收键盘输入数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户是否输入字符串
if(scanner.hasNext()){
//使用next方式接收
String str = scanner.next();
System.out.println("输出内容为:"+str);
}
//凡是IO流的类如果不关闭会一直占用资源,用完就要关闭
scanner.close();
}
}
这里只需要更改nextLine()即可;后续可以不适用if判断,直接使用下面内容:
//这里不添加判断
String str = scanner.nextLine();
System.out.println("输出内容:"+str);
使用if判断语句:
package com.dai.scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//从键盘接收整数
int i = 0;
float f = 0.0f;
System.out.println("请输入一个整数:");
//nextInt表示
if(scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整数数据:"+i);
}else{
System.out.println("输入的不是整数数据");
}
System.out.println("请输入一个小数:");
if(scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小数数据:"+f);
}else{
System.out.println("输入的不是小数数据");
}
scanner.close();
}
}
这里nextInt()、nextFloat()和nextLine()分别是读取整数、小数、一行字符串;
package com.dai.scanner;
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//这里scanner可以使用sc缩写 double sum = 0;
int count = 0;
System.out.println("请输入数字,输入非数字结束:");
//这里直接在while中判断是否还有下一个double类型的输入
while (sc.hasNextDouble()) {
double x =sc.nextDouble();
count++;
sum = sum + x;
System.out.println("已输入"+count+"个数字,当前和为:"+sum);
}
System.out.println(count + "个数的和为:" + sum);
System.out.println(count + "个数的平均数为:" + (sum / count));
sc.close();
}
}
package com.dai.scanner;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//输入多个数字,求其和与平均数,每输入一个数字回车确认,通过输入非数字结束并输出结果
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//个数
int count = 0;
System.out.println("请输入数字,输入非数字结束:");
//通过循环判断是否还有输入,并在里面对每一次进行求和和统计
while (scanner.hasNext()) {
if(scanner.hasNextDouble()) {
double x = scanner.nextDouble();
count = count + 1; //count++
sum = sum + x;
System.out.println("已输入"+count+"个数字,当前和为:"+sum);
} else {
// 输入非数字,结束循环
System.out.println("检测到非数字输入,程序结束并输出结果。");
break;
}
}
System.out.println(count + "个数的和为:" + sum);
System.out.println(count + "个数的平均数为:" + (sum / count));
scanner.close();
}
}
上述两段代码实现功能一样,在while中,hasNext()不能判断下一个输入的是否是数字,而hasNextDouble()可以判断,当输入不是时可以直接结束,更简洁
顺序结构
选择结构(重点)
package com.dai.struct;
import java.util.Scanner;
public class ifDemo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入内容:");
String s = sc.nextLine();
//equals方法比较字符串内容是否相同
if (s.equals("hello")) {
System.out.println(s);
}
//上述不成立则输出End
System.out.println("End");
sc.close();
}
}
package com.dai.struct;
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
//考试分数大于60就是及格,否则不及格
Scanner sc = new Scanner(System.in);
//提示用户输入考试分数
System.out.println("请输入考试分数:");
int score = sc.nextInt();
if (score >= 60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
sc.close();
}
}
package com.dai.struct;
import java.util.Scanner;
public class IfDemo03 {
public static void main(String[] args) {
//k
Scanner sc = new Scanner(System.in);
//提示用户输入内容
System.out.println("请输入您的成绩:");
//这里输入的成绩必须为整数
int score = sc.nextInt();//double score = sc.nextDouble();这样就可以输入小数啦
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 >=70){
System.out.println("C级");
}else if (score < 70 && score >= 60){
System.out.println("D级");
}else if (score < 60 && score >= 0){
System.out.println("不及格");
}else{
System.out.println("输入成绩不合法");
}
sc.close();
}
}
二分法
package com.dai.struct;
import java.util.Scanner;
public class IfDemo04 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
// 二分法猜数字游戏:0-100之间找一个数
int left = 0;
int right = 100;
int count = 0;
System.out.println("请你心里默想一个0-100之间的整数,我来猜!");
while (left <= right) {
int mid = (left + right) / 2;
count++;
System.out.println("我猜是:" + mid + ",请告诉我:1-大了,2-小了,3-猜对了");
int feedback = sc.nextInt();
if (feedback == 3) {
System.out.println("太棒了!我猜对了,用了" + count + "次。");
break;
} else {
if (feedback == 1) {
//大了,也就是要更新右边,第一次猜50,大了右边=51,猜数范围0-51
right = mid - 1;
} else {
if (feedback == 2) {
//小了就更新左边,第一次猜50,小了左边=51,猜数范围51-100
left = mid + 1;
} else {
System.out.println("输入有误,请输入1、2或3。");
}
}
}
}
sc.close();
}
}
package com.dai.struct;
import java.util.Scanner;
public class SwitchDemo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//输入一个1-7的整数,输出对应的星期几
System.out.println("请输入一个1-7的整数:");
int week = sc.nextInt();
//case具有穿透性,执行完成后加上break
switch (week){
case 1 :
System.out.println("星期一");
break;
case 2 :
System.out.println("星期二");
break;
case 3 :
System.out.println("星期三");
break;
case 4 :
System.out.println("星期四");
break;
case 5 :
System.out.println("星期五");
break;
case 6 :
System.out.println("星期六");
break;
case 7 :
System.out.println("星期日");
break;
default :
System.out.println("输入有误");
break;
}
}
}
//反编译:java-->class(字节码文件)-->反编译(IDEA)
在IDEA中找到设置里面Project Structure,然后有一个out输出文件夹,在外部打开,找到你想要反编译的文件。然后再IDEA中在这个文件的包名右键选择Open in-->explore,把前面需要反编译的文件复制进来,然后在IDEA中打开即可。
循环结构
package com.dai.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出0~100
int i = 0;
while (i < 100){
i++;
System.out.println(i);
}
}
}
//伪代码
//死循环
while (true){
//等待客户端连接
//定时检查
//。。。。。
}
package com.dai.struct;
public class WhileDemo02 {
public static void main(String[] args) {
//计算1+2+3+.....+100=?
int i = 0;
int sum = 0;
while (i <= 100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
package com.dai.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
//do...while循环至少执行一次循环体
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
while和do while区别:前者不执行后者执行
package com.dai.struct;
public class DoWhileDemo02 {
public static void main(String[] args) {
int a = 0;
while (a < 0){
System.out.println(a);
a++;
}
System.out.println("========================");
do {
System.out.println(a);
a++;
}while (a < 0);
}
}
for循环(重要)
for循环:最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
然后,检测布尔表达式的值,如果为true则循环体被执行。反之循环终止,执行循环体后面的语句。
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
再次检测布尔表达式,循环执行上面过程。
//死循环
for ( ; ; ){
循环体
}
package com.dai.struct;
public class ForDemo02 {
public static void main(String[] args) {
//练习1;计算0~100之间的奇数和、偶数和
int oddSum = 0;//奇数和
int evenSum = 0;//偶数和
for (int i = 1; i <= 100; i++){
if (i % 2 == 0){//i % 2 != 0,这样算的是奇数
evenSum = evenSum + i;
}else{
oddSum = oddSum + i;
}
}
System.out.println("0~100之间的奇数和为:"+oddSum);
System.out.println("0~100之间的偶数和为:"+evenSum);
}
}
println 输出会换行,print 输出不会换行加(“\n")
快捷键:9.for然后按住tab
package com.dai.struct;
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用while或者for循环输出1~1000之间所有是5的倍数的整数,要求每行输出3个数据,数据之间用空格隔开
for (int i = 0; i <=1000; i++){
if (i % 5 == 0){
//注意这里不能用println,否则每输出一个数据就换行了
System.out.print(i+"\t");//这里\t表示制表符,相当于空格
}
if (i % (5*3) == 0){//每输出3个5的倍数后换行
System.out.println();//每输出3个5的倍数后换行
//System.out.print("\n");//也可以用这个换行
}
}
}
}
\t空格,\n换行
注意字符串之间要用“+”拼接
package com.dai.struct;
public class ForDemo04 {
public static void main(String[] args) {
//练习3;打印九九乘法表
//1.先打印第一列(一个for循环,1+“*”+i+"="+(1*i))
//2.把固定的1再用一个for循环包起来
//3.去掉重复项,j<=i
//4.调整样式
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
//注意这里的print和println的区别
System.out.print(j + "*" + i + "=" + (j * i) + "\t");
}
//每一行打印完后换行
System.out.println();
}
}
}
package com.dai.struct;
public class ForDemo05 {
public static void main(String[] args) {
int [] numbers = {10,20,30,40,50};
for (int i = 0; i < 5; i++){//元素从0开始
System.out.println(numbers[i]);
}
System.out.println("================================");
//增强for循环
//遍历数组元素
for (int x:numbers) {
System.out.println(x);
}
}
}
break & continue
package com.dai.struct;
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while (i < 100){
i++;
System.out.println(i);
if (i == 30){
break;//结束整个循环
}
}
}
}
package com.dai.struct;
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
//如果i是10的倍数,就跳过本次循环,继续下一次循环
if(i % 10 == 0){
System.out.println();
continue;
}
System.out.println(i);
}
}
}
不要求掌握;标签+中断
package com.dai.struct;
public class LabelDemo {
public static void main(String[] args) {
// 打印101到150之间的质数
int count = 0; // 计数器(未使用,可扩展)
// 定义一个外部循环标签 "outer"
outer:for (int i = 101; i < 150; i++) { // 遍历101到150之间的所有数
for (int j = 2; j < i / 2; j++) { // 检查当前数是否能被2到i/2之间的数整除
if (i % j == 0) { // 如果能整除,说明不是质数
continue outer; // 跳过当前数,继续外部循环的下一次迭代
}
}
// 如果内层循环未发现因子,说明当前数是质数
System.out.println(i + " "); // 打印质数
}
}
}
练习
打印三角形
package com.dai.struct;
public class TestDemo {
public static void main(String[] args) {
//打印一个五行的三角形
//先打印一个直角三角形
int rows = 5;
//直角在左下角的三角形
//外层循环控制行数
for (int i = 1; i <= rows; i++){
//内层打印每一行的数据
for (int j = 1; j <= i; j++){
System.out.print("*");//打印星号不换行
}
System.out.println();//每打印完一行星号换行
}
//直角在右下角的三角形
//外层循环控制行数
for (int i = 1; i <= rows; i++){
//内层打印每一行的数据
//也可以int j = 1; j <= rows - i; j++
for (int j = 5; j > i; j--){
System.out.print(" ");//打印空格不换行
}
//上面for循环执行完毕后,才执行这个
for (int k = 1; k <= i; k++){
System.out.print("*");//打印星号不换行
}
System.out.println();//每打印完一行星号换行
}
//打印一个等腰三角形
for (int i = 1; i <= rows; i++){
//打印空格
for (int j = 5; j > i; j--){
System.out.print(" ");
}
//打印星号,每行的星号数是2*i -1
for (int k = 1; k <= (2*i -1); k++){
System.out.print("*");
}
System.out.println();
}
//等腰三角形的另一种写法(最左侧空一列)
//外循环控制行数
for (int i = 1;i <= 5; i++){
//先打印空格(依次为5 4 3 2 1)
for (int j = 5; j >=i; j--){
System.out.print(" ");//打印空格
}
//先打印直角三角形(依次为1 2 3 4 5)
for (int j = 1; j <= i; j++){
System.out.print("*");//打印星号
}
//再打印直角三角形(依次为0 1 2 3 4)
for (int j = 1; j < i; j++){
System.out.print("*");//打印星号
}
System.out.println();
}
}
}
浙公网安备 33010602011771号