Java——第三章(流程控制语句)项目案例

来源:互联网 发布:mysql是大型数据库吗 编辑:程序博客网 时间:2024/04/30 14:09


不多说,直接上干货,搞起!这些题你做完,流程控制基本上就可以自己解决百分之90以上的题了。




1.马宁考试,得了一等奖奖励一个macbook,二等奖奖励ssd固态硬盘,三等奖奖励一个u盘,其他名次为挨


揍!(哈哈)


代码:

import java.util.Scanner;public class Demo {public static void main(String[] args) {        //创建scanner对象sc,键盘输入名次用i接收。Scanner sc = new Scanner(System.in);System.out.print("马宁荣获几等奖?"); int i = sc.nextInt(); if(i == 1) { System.out.println("奖励一个macbook"); } else if(i == 2) { System.out.println("奖励一个固态硬盘"); } else if(i == 3){ System.out.println("奖励一个u盘"); } else{ System.out.println("挨揍"); }    }}

用switch语句改写:

import java.util.Scanner;public class Demo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("马宁荣获几等奖?"); int i = sc.nextInt(); switch (i) {// jdk 1.8版本以上可以使用字符串case 1:System.out.println("macbook");break;case 2:System.out.println("固态硬盘");break;case 3:System.out.println("u盘");break;default:System.out.println("挨揍");}}}


这两种效果是等同的,不过等值判断时,switch-case语句执行效率要高于if语句。


2.键盘输入考试成绩,小于60为不及格,60~70为及格,70~80是良好,80~90是优秀,90~100是牛掰,100为


满分。请输入对应成绩的等级。

import java.util.Scanner;public class Demo02 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("请输入考试成绩:");int chengji = sc.nextInt(); if (chengji < 60) { System.out.println("成绩不及格!"); } else if(chengji < 70) { System.out.println("成绩及格!"); } else if(chengji <80) { System.out.println("成绩良好!"); } else if (chengji <90) { System.out.println("成绩优秀"); } else if (chengji <100) { System.out.println("成绩牛掰!"); } else { System.out.println("成绩满分!");}}}


想一想,能不能用switch语句改一下呢? 很多人说不行,因为不是等值判断,其实是可以的,只需要稍加修


改。


import java.util.Scanner;public class Demo03 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("请输入考试成绩:");int chengji = sc.nextInt();switch (chengji / 10) {case 6:System.out.println("成绩及格!");break;case 7:System.out.println("成绩良好!");break;case 8:System.out.println("成绩优秀!");break;case 9:System.out.println("成绩牛掰!");break;case 10:System.out.println("成绩满分!");break;default:System.out.println("成绩不合格!");}}}

这样就可以达到目的了。。。


3.编写一个简易计算器,实现基本+、-、*、/、%运算。


import java.util.Scanner;public class Demo04 {public static void main(String[] args) {// TODO Auto-generated method stubmenu();play();}public static void menu() {System.out.println("**********欢迎使用计算器**********");System.out.println("1.加法运算");System.out.println("2.减法运算");System.out.println("3.乘法运算");System.out.println("4.除法运算");System.out.println("5.取余运算");System.out.println("6.退出");System.out.println("*******************************");}public static void play() {int i = 0;double a = 0.0;double b = 0.0;Scanner sc = new Scanner(System.in);System.out.println("输入您要进行的操作:");i = sc.nextInt();while (true) {switch (i) {case 1:System.out.println("输入两个数进行加法");a = sc.nextDouble();b = sc.nextDouble();System.out.println("计算结果为:" + (a + b));break;case 2:System.out.println("输入两个数进行减法");a = sc.nextDouble();b = sc.nextDouble();System.out.println("计算结果为:" + (a - b));break;case 3:System.out.println("输入两个数进行乘法");a = sc.nextDouble();b = sc.nextDouble();System.out.println("计算结果为:" + (a * b));break;case 4:System.out.println("输入两个数进行除法");a = sc.nextDouble();b = sc.nextDouble();System.out.println("计算结果为:" + (a / b));break;case 5:System.out.println("输入两个数进行取余");a = sc.nextDouble();b = sc.nextDouble();System.out.println("计算结果为:" + (a % b));break;case 6:System.out.println("*********谢谢使用!*********");default:System.out.println("输入有误!");}menu();play();}}}


***做循环题套路:找到循环次数,找到循环操作,若操作是变化的,找到变化与操作次数之间的关系。***


4.打印图形,第一行2个空格,第二行一个,第三行0个。


  *******
 *******
*******

分析: 循环次数: 三行, 所以3次。

循环操作: 打印空格,打印7个*,换行。(又涉及到循环。)(!找到与循环次数的关系!)空格个数与次


数关系 是3 - 次数。


public class Demo09 {public static void main(String[] args) {// TODO Auto-generated method stubfor(int cishu = 1;cishu<=3;cishu++){int geshu = 3-cishu;for(int cishu_1 = 1;cishu_1<=geshu;cishu_1++){System.out.print(" ");}for(int cishu1 = 1;cishu1 <=7;cishu1++){System.out.print("*");}System.out.println();}}}


5.打印图形   依旧最后一行前面没空格。

   *
  ***
 *****
*******

(图形不好打,是对称的)


分析: 循环次数:4次


循环操作:打印空格,打印*,换行。(!找到与循环次数的关系!)每一行的空格数为 4 - 循环次数。*个数


为次数的2倍-1。


public class Demo10 {public static void main(String[] args) {// TODO Auto-generated method stubfor (int cishu = 1; cishu <= 4; cishu++) {// 1.打印空格int geshu = 4 - cishu;for (int cishu_1 = 1; cishu_1 <= geshu; cishu_1++) {System.out.print(" ");}// 2.打印*int geshu_1 = 2 * cishu - 1;for (int cishu1 = 1; cishu1 <= geshu_1; cishu1++) {System.out.print("*");}// 3.换行System.out.println();}}}


再来一个boss级别的图形题!




6.打印一个“王"字。(有难度啊!)

************
************
   ***
   ***
   ***
************
************
   ***
   ***
   ***
************
************


分析: 循环次数: 12次


循环操作: 第1,2,6,7,11,12行打印12个*,换行,第3,4,5,8,9,10,行为空格,*号还有换行。提示:本题


用到了标志位算法。


public class Demo11 {public static void main(String[] args) {// TODO Auto-generated method stubint t = 0;boolean bo = true;for(int cishu = 1;cishu<=12;cishu++){if(cishu == 1+5*t || cishu==2+5*t){//情况1for(int i =1;i<=12;i++){System.out.print("*");}System.out.println();bo =true;}else{if(bo){t++;bo=!bo;}//情况2for(int j=1;j<=4;j++){System.out.print(" ");}for(int i=1;i<=3;i++){System.out.print("*");}System.out.println();//System.out.println("    ***");}}}}


7.猴子吃桃问题,有一堆桃,每天吃剩余的一半还要多吃一个,结果第十天醒来一看,还剩下1个桃,求没有吃


之前有几个桃。

public class Demo12 {public static void main(String[] args) {// TODO Auto-generated method stubint num =1  ;for(int cishu=1;cishu<=9;cishu++){num = (num+1)*2;}System.out.println(num);}}


本题采用逆向思维要比之前的思维更简单,并不是所有循环都要用之前的套路,若循环操作前后有联系,找到


他们之间存在的关系有时候比第一种更为简便。(逆向思维)


8.1*2*3*4....*9*11*12*13..*n      当sum>2^25时, 输出sum的值。要求:用while语句操作。注意: 中间没有10的参与。


public class Demo15 {public static void main(String[] args) {// TODO Auto-generated method stub/* * 1*2*3*4....*9*11*12*13..*n  sum>2^25 输出sum */int n=1;int sum  =1;while(true){n++;if(n==10){continue;}sum=sum*n;if(sum>Math.pow(2, 25)){System.out.println(sum);break;}}}}


终于打完了, 以上项目案例如果多练习几遍,难度大的循环就掌握了。。。



你们的支持就是我的动力。。。大家一起加油!程序猿是敲出来的 ,不是看几遍就可以的。。。。










1 0
原创粉丝点击