当前位置: 代码迷 >> 综合 >> 代码 : 第一阶段 模块一 Java编程基础 任务四 流程控制
  详细解决方案

代码 : 第一阶段 模块一 Java编程基础 任务四 流程控制

热度:8   发布时间:2024-02-19 21:58:39.0

1, 编程实现调和数列的累加

/*编程实现调和数列的累加 */import java.util.Scanner;public class While {
    public static void main(String[] args) {
    System.out.println("请输入想要计算的项数n:");Scanner sc = new Scanner(System.in);int n = sc.nextInt();double sum = 0.0;int i = 1;while(i <= n) {
    sum += (1.0 / i);i++;}System.out.println("前"+ n + "项的和为:" + sum);}
}

2, 编程实现,出租车计费系统

/*编程实现,出租车计费系统 */import java.util.Scanner;public class TaxiPrice {
    public static void main(String[] args) {
    //1.提示输入公里数和等候秒数存入变量System.out.println("请输入公里数和等候秒数:");Scanner sc = new Scanner(System.in);int km = sc.nextInt();int sec = sc.nextInt();int kmprice,secprice;//2.计算费用if(km <= 3) {
    kmprice = 13; } else if(km > 3 && km <= 15) {
    	//这里可以写成 else(km <= 15),不加前面的限制也可kmprice = (km - 3) * 2 + 13;} else {
    kmprice = (km - 15) *3 + 24 + 13;}secprice = sec / 150;//3.打印车费System.out.println(km + "公里且等候时间" + sec + "秒,总计车费为:" + (secprice + kmprice));}
}

3, 编程实现,switch 实现字符界面

/*编程实现,switch 实现字符界面 */import java.util.Scanner;public class SwitchMenu {
    public static void main(String[] args) {
    //1.绘制字符界面System.out.println(" ");System.out.println(" 欢迎来到拉勾教育");System.out.println("-------------------------------------");System.out.print(" [1]学员系统");		// println里面的ln相当于换行的作用System.out.println(" [2]管理员系统");System.out.println(" [0]退出系统");System.out.println("-------------------------------------");System.out.println("请选择要进入的系统:");Scanner sc = new Scanner(System.in);int choose = sc.nextInt();//2.使用switch模拟用户的选择并给出提示switch(choose){
    case 1: System.out.println("正在进入 [1]学员系统 ..."); break;case 2: System.out.println("正在进入 [2]管理员系统 ..."); break;case 0: System.out.println("谢谢使用,下次再见!"); break;default:System.out.println("输入有误,请重新输入");}}
}

4, 编程实现,使用switch语句判断等级

/*编程实现,使用switch语句判断等级 */import java.util.Scanner;public class SwitchLvJudge {
    public static void main(String[] args) {
    //1.提示输入考试成绩System.out.println("请输入考试成绩:");Scanner sc = new Scanner(System.in);int score = sc.nextInt();char Lv = 'F';//2.使用switch判断并打印switch(score / 10) {
    case 10://Lv = 'A';case 9: Lv = 'A';break;		//case穿透case 8: Lv = 'B';break;case 7: Lv = 'C';break;case 6: Lv = 'D';break;default: Lv = 'E';}System.out.println("您所在的等级为:" + Lv);//3.打印一句话System.out.println("-----------------------------------------------------------------------");System.out.println("世界上最痴情的等待,就是我当case你当switch,或许永远都不会选到自己!");}
}

5, 编程实现,使用switch语句判断等级

/*编程实现,使用switch语句判断等级 */import java.util.Scanner;public class ReverNum {
    public static void main(String[] args) {
    /*System.out.println("请输入任意正整数");Scanner sc = new Scanner(System.in);int num = sc.nextInt();int sum = 0;int N1;while(num % 10 != 0) {N1 = num % 10;num = num / 10;sum = sum * 10 + N1;}System.out.println("反向输出的结果为:" + sum);*/System.out.println("请输入任意正整数");Scanner sc = new Scanner(System.in);int num = sc.nextInt();while(num > 0) {
    System.out.print(num % 10);	//拆分个位数num /= 10;	//丢弃个位数}}
}

6, 编程实现等级判断

/*编程实现等级判断 */import java.util.Scanner;public class LvJudge {
    public static void main(String[] args) {
    //1.提示输入考试成绩System.out.println("请输入考试成绩:");Scanner sc = new Scanner(System.in);int score = sc.nextInt();char Lv = 'F';//2.判断等级并打印if(score < 60) {
    			//小于60的设置为defaultLv = 'E';} else if(score < 70) {
    		//61到69对10去商,结果都是6Lv = 'D';} else if(score < 80) {
    Lv = 'C';} else if(score < 90) {
    Lv = 'B';} else if(score <= 100) {
    Lv = 'A';}System.out.println("您所在的等级为:" + Lv);}
}

7, 编程使用if分支结构模拟网吧上网的过程


/*编程使用if分支结构模拟网吧上网的过程 */import java.util.Scanner;public class IfTest {
    public static void main(String[] args) {
    //1.提示输入年龄并存入变量System.out.println("请输入您的年龄:");Scanner sc = new Scanner(System.in);int age = sc.nextInt();//2.判断是否成年并给出提示if (age >= 18) {
    System.out.println("开心的浏览起了网页...");}//3.打印一句话System.out.println("美好的时光总是短暂的");}}

8, 编程实现,输入薪水,计算个人所得税并打印

/*编程实现,输入薪水,计算个人所得税并打印 */import java.util.Scanner;public class Ifsalary {
    public static void main(String[] args) {
    //1.提示输入薪水并存入变量System.out.println("请输入本月薪水:");Scanner sc = new Scanner(System.in);//局部变量: 作用范围是声明开始,到方法体结束int salary = sc.nextInt();//2.计算所得税double gainrate = 0.0;if(salary <= 5000) {
    gainrate = 0.0;	//块变量:控制结构内部的声明仅用于当前结构} else if(salary > 5000 && salary <= 8000) {
    //gainrate = (salary - 5000) * 0.03; gainrate = (salary - 5000) * 0.03 - 0;} else if(salary > 8000 && salary <= 17000) {
    //gainrate = (salary - 8000) * 0.1 + 90;gainrate = (salary - 5000) * 0.1 - 210;} else if(salary > 17000 && salary <= 30000) {
    gainrate = (salary - 5000) * 0.2 - 1410;//gainrate = (salary - 17000) * 0.2 + 990;} else if(salary > 30000 && salary <= 40000) {
    gainrate = (salary - 5000) * 0.25 - 2660;//gainrate = (salary - 30000) * 0.25 + 3590;} else if(salary > 40000 && salary <= 60000) {
    gainrate = (salary - 5000) * 0.3 - 4410;//gainrate = (salary - 40000) * 0.3 + 6090;} else if(salary > 60000 && salary <= 85000) {
    gainrate = (salary - 5000) * 0.35 - 7160;//gainrate = (salary - 60000) * 0.35 + 12090;} else {
    gainrate = (salary - 5000) * 0.45 - 15160;//gainrate = (salary - 85000) * 0.45 + 20840;}//3.打印所得税System.out.println("本月纳税额为:" + gainrate);}
}

9, 编程实现,提示输入一个整数,使用if else判断正负并输出

/*编程实现,提示输入一个整数,使用if else判断正负并输出 */import java.util.Scanner;public class IfMinus {
    public static void main(String[] args) {
    //1.提示输入整数存入变量System.out.println("请输入一个整数:");Scanner sc = new Scanner(System.in);int num = sc.nextInt();//2.判断正负并输出/*if(num < 0) {System.out.println(num + "是一个负数");} else {System.out.println(num + "是一个非负数");}*///3.判断是正是负还是0if(num < 0) {
    System.out.println(num + "是一个负数");} else {
    if(0 == num) {
    System.out.println(num + "是零");} else {
    System.out.println(num + "是一个正数");}}/*if(num < 0) {System.out.println(num + "是一个负数");} else if (0 == num) { System.out.println(num + "是零");} else {System.out.println(num + "是一个正数");}*/}
}

10, 编程实现,提示用户输入两个整数,使用if分支结构找到最大值并打印出来

/*编程实现,提示用户输入两个整数,使用if分支结构找到最大值并打印出来 */import java.util.Scanner;public class IfMax {
    public static void main(String[] args) {
    //1.提示用户输入,并存入两个变量System.out.println("请输入两个整数:");Scanner sc = new Scanner(System.in);int num1 = sc.nextInt();int num2 = sc.nextInt();//2.查找最大值并打印/*if(num1 >= num2) {System.out.println("最大值为:" + num1);}if(num2 > num1) {System.out.println("最大值为:" + num2);}*///方式二:假设第一个最大并记录int max = num1;if(num2 >= max) {
    max = num2;}System.out.println("最大值为:" + max);}
}

11, 编程实现 if else if else分支结构的使用,模拟购票

/*编程实现 if else if else分支结构的使用,模拟购票 */import java.util.Scanner;public class IfElseIfElse {
    public static void main(String[] args) {
    //1.提示输入身份信息并记入变量System.out.println("请输入您的身份信息:");Scanner sc = new Scanner(System.in);String str = sc.next();//判断身份信息if ("军人".equals(str)) {
    System.out.println("免费乘坐");} else if("学生".equals(str) ) {
    System.out.println("半价购票");} else {
    System.out.println("请购买全票");}//3.打印一句话System.out.println("坐上了火车去拉萨,去看那美丽的布达拉 ");}
}

12, 编程使用if else分支结构来模拟考试成绩查询过程

/*编程使用if else分支结构来模拟考试成绩查询过程 */import java.util.Scanner;public class IfElse {
    public static void main(String[] args) {
    //1.提示用户输入并记入变量System.out.println("请输入您的考试成绩:");Scanner sc = new Scanner(System.in);int score = sc.nextInt();//2.判断是否合格并给出提示if(score >= 60) {
    System.out.println("恭喜你考试通过了");} else {
    System.out.println("下学期来补考吧");}//3.打印一句话System.out.println("世界上最遥远的距离不是生与死,而是你在if我在else,似乎一直相伴却又永远分离 ");}
}

13, 编程实现for循环的使用,模拟玩游戏的过程

/*编程实现for循环的使用,模拟玩游戏的过程 */public class ForTest {
    public static void main(String[] args) throws Exception {
    for(int i = 1; i <= 10; i++) {
    	System.out.println("今晚吃鸡,大吉大利,正在进行第" + i + "场游戏...");Thread.sleep(5000);	//表示模拟睡眠5秒的效果System.out.println("本场游戏结束!\n\n\n");}System.out.println("该休息了,否则明天就要迟到了");}
}

14, 编程实现,1到10000的累加并打印

/*编程实现,1到10000的累加并打印 */
public class ForSum {
    public static void main(String[] args) {
    int sum = 0;for(int i = 1; i <= 10000; i++) {
    sum += i;}System.out.println("1到10000的和为:" + sum);}
}

15, 编程实现,打印1-100的所有奇数

/*编程实现,打印1-100的所有奇数 */
public class ForNum {
    public static void main(String[] args) {
    //方式一:根据奇数概念打印int count = 0;for(int i = 1; i < 101; i++) {
    if((i % 2) != 0) {
    System.out.print(i + " ");count++;if(10 == count) {
    System.out.println("");count = 0;}}}System.out.println("------------------------------------");//方式二:根据等差数列打印for(int i = 1; i <= 100; i += 2) {
    System.out.print(i + " ");count++;if(10 == count) {
    System.out.println("");count = 0;}}System.out.println("------------------------------------");//方式三:for(int i = 1; i <= 50; i++) {
    System.out.print(i * 2 - 1 + " ");count++;if(10 == count) {
    System.out.println("");count = 0;}}}
}

16, 编程实现,使用for打印1~20之间所有整数,遇到5的倍数则跳过不打印

/*编程实现,使用for打印1~20之间所有整数,遇到5的倍数则跳过不打印 */
public class ForJump5 {
    public static void main(String[] args) {
    for(int i = 1; i <= 20; i++) {
    if(0 == i % 5) {
    continue;}System.out.println("i = " + i);}}
}

17, 编程使用for循环实现猜数字游戏

/*编程使用for循环实现猜数字游戏 */import java.util.Random;
import java.util.Scanner;public class ForGuess {
    public static void main(String[] args) {
    //1.生成1~100之间的随机数并用变量记录Random ra = new Random();int temp = ra.nextInt(100) + 1;//System.out.println("temp = " + temp);//4.声明一个int变量来统计用户猜测的次数int count = 0;//2.提示输入1~100之间猜测的整数并使用变量记录System.out.println("\n请输入一个猜测数(1~100之间):");Scanner sc = new Scanner(System.in);int num = sc.nextInt();//3.使用用户输入的整数与随机数之间比较大小,并给出对应的提示for(;;) {
    if(num > temp) {
    System.out.println("太...太大了,再试一次吧!\n");num = sc.nextInt();count++;} else if(num < temp) {
    System.out.println("太...太小了,再试一次吧!\n");num = sc.nextInt();count++;} else {
    count++;System.out.println("恭喜,大小刚刚好!您一共尝试了" + count +"次\n\n");break;}}if(1 == count) {
    System.out.println("你果然棒棒");} else if(count <= 6) {
    System.out.println("666,继续加油!");} else {
    System.out.println("菜鸡,多来几次吧");}}
}

18, 编程使用双重for打印星星

/*编程使用双重for打印星星 */
public class ForForStar {
    public static void main(String[] args) {
    //1.打印第1个星星图System.out.println();for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= 5; j++) {
    System.out.print("*");}System.out.println();}System.out.println();//2.打印第2个星星图System.out.println("-------------------------------------");System.out.println();for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
    System.out.print("*");}System.out.println();}System.out.println();//3.打印第3个星星图System.out.println("-------------------------------------");System.out.println();for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= 6 - i; j++) {
    System.out.print("*");}System.out.println();}System.out.println();//4.打印第4个星星图System.out.println("-------------------------------------");System.out.println();for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= 5 - i; j++) {
    System.out.print(" ");}for(int k = 1; k <= 2 * i - 1; k++) {
    System.out.print("*");}System.out.println();}}
}

19, 打印2~100之间所有的素数

/*打印2~100之间所有的素数 */
public class ForForPrime {
    public static void main(String[] args) {
    for(int i = 2; i <= 100; i++) {
    boolean flag = true;//针对每一个当前的整数都要判断是否是素数//判断方法为:2到 它本身-1之间的所有整数都无法整除,则为素数for(int j = 2; j <= Math.sqrt(i); j++) {
    //只需要判断2到该数的平方根即可//for(int j = 2; j <= i - 1; j++) {
    if(0 == i % j) {
    flag = false;//System.out.println(i + "不是素数");break;}}if(flag) {
    System.out.println(i + "是素数");}}System.out.println("------------------------------------------------------");}
}

20, 使用双重for打印99乘法表

/*使用双重for打印99乘法表 */
public class ForFor99 {
    public static void main(String[] args) {
    System.out.println("-----------------------------------------------------------------------");System.out.println();//1.使用外层for控制打印的行数,一共9行outer: for(int i = 1; i <= 9; i++) {
    //2.用内层for控制打印的列数,最多9列,规律是与当前行数相等for(int j = 1; j <= i; j++) {
    //3.拼接结果System.out.print(i + "*" + j + "=" + (i * j) + " " );//4.打印完6*6 = 36后结束整个打印 if(6 == j) {
    //break; //主要用于跳出循环,但该关键字只能跳出当前所在循环break outer;	//表示可以跳出所标循环}}System.out.println();/*if(6 == i) {break;}*/}System.out.println();System.out.println("-----------------------------------------------------------------------");System.out.println();for(int i = 1; i <= 9; i++) {
    for(int j = 1; j <= 10 - i; j++) {
    System.out.print(i + "*" + j + "=" + (i * j) + " " );}System.out.println();}System.out.println("-------------------------------------");System.out.println();	for(int i = 1; i <= 9; i++) {
    for(int k = 1; k <= 9 - i; k++){
    System.out.print(" ");}for(int j = 1; j <= i; j++) {
    System.out.print(i + "*" + j + "=" + (i * j) + " " );}System.out.println();}System.out.println("-------------------------------------");System.out.println();for(int i = 1; i <= 9; i++) {
    for(int k = 1; k <= i - 1; k++){
    System.out.print(" ");}for(int j = i; j <= 9; j++) {
    System.out.print(i + "*" + j + "=" + (i * j) + " " );}System.out.println();}}
}

21, 编程实现双重for循环的使用

/*编程实现双重for循环的使用 */
public class ForFor {
    public static void main(String[] args) {
    for (int i = 1; i <= 5; i++) {
    for(int j = 1; j <=5; j++) {
    System.out.print("厉害了我的哥 ");}System.out.println("");}}
}

22, 编程,打印三位数中所有水仙花数

/*编程,打印三位数中所有水仙花数 */
public class ForFlower {
    public static void main(String[] args) {
    int count = 0;System.out.println("三位数中所有水仙花数是:" );for(int i = 100; i <= 999; i++) {
    int i1 = i / 100;int i2 = (i % 100) / 10;int i3 = i % 10;if(i == i1 * i1 * i1 + i2 * i2 * i2 + i3 * i3 * i3) {
    System.out.print(" " + i);count++;if(5 == count) {
    System.out.println("");count = 0;}}}	}
}

23, 不断提示用户输入新内容,bye退出

/*不断提示用户输入新内容,bye退出 */import java.util.Scanner;public class ForBye {
    public static void main(String[] args) {
    /*System.out.println("请输入新内容:");Scanner sc = new Scanner(System.in);String str = sc.next();System.out.println("您输入的是:" + str);for( ; !("bye".equals(str)); ) {System.out.println("请输入新内容:");str = sc.next();System.out.println("您输入的是:" + str);}*/ //5.声明一个boolean类型变量作为发送方标志boolean flag = true;//4.使用无限循环来模拟不断地聊天for(;;) {
    //1.提示用户输入要发送的聊天内容并使用变量记录System.out.println("请" + (flag? "张三" : "李四") + "输入新内容:");Scanner sc = new Scanner(System.in);String str = sc.next();//2.判断是否是bye,是则结束聊天if("bye".equals(str)) {
    System.out.println("已退出,谢谢使用!");break;}//3.不是则打印输入的内容//System.out.println((flag? "聊天内容是:") + str);System.out.println((flag? "张三说:" : "李四说:") + str + "\n\n\n");flag = !flag;}}
}

24, 编程使用dowhile来模拟学习效果的检查

/*编程使用dowhile来模拟学习效果的检查 */import java.util.Scanner;public class DoWhileCheck {
    public static void main(String[] args) throws Exception{
    String msg = null;do {
    System.out.println("正在疯狂学习中...");Thread.sleep(5000);System.out.println("是否合格?(y/n)");Scanner sc = new Scanner(System.in);msg = sc.next();} while(!"y".equals(msg));System.out.println("恭喜任务合格!");System.out.println("-------------------------------------------------------");//典故:十动然拒int = 1;while(i <= 10000) /* ; */ {
    System.out.println("I love You !");}}
}
  相关解决方案