[关闭]
@Hubertoo 2017-07-17T04:56:08.000000Z 字数 6195 阅读 625

Fundamental.Unit04

达内


知识体系

分支结构

什么是分支结构

循环结构

什么是循环结构

经典案例

收银台程序2.0

  1. package day01;
  2. import java.util.Scanner;
  3. /**
  4. * 在原有基础上加入满500打八折,并保证收取金额不足时提醒功能
  5. * @author zeyan
  6. *
  7. */
  8. public class Cashier2 {
  9. public static void main(String[] args) {
  10. Scanner console = new Scanner(System.in);
  11. //console 控制台
  12. System.out.println("请输入单价(¥):");
  13. int unitPrice = console.nextInt();
  14. System.out.println("请输入数量:");
  15. double amount = console.nextDouble();
  16. System.out.println("请输入收取金额(¥):");
  17. double money = console.nextDouble();
  18. console.close();
  19. //计算商品总价
  20. double totalPrice = 0.0;
  21. totalPrice = unitPrice * amount;
  22. //判断余额是否可以打折
  23. if(money < totalPrice){
  24. System.out.println("收取金额不足:");
  25. }else if(totalPrice >= 500){
  26. totalPrice *= 0.8;
  27. //计算找零
  28. double change = money - totalPrice;
  29. System.out.println("商品总价:" + totalPrice + ",应找零为:" + change);
  30. }else{
  31. //计算找零
  32. double change = money - totalPrice;
  33. System.out.println("商品总价:" + totalPrice + ",应找零为:" + change);
  34. }
  35. }
  36. }

完成成绩等级输出程序

  1. package day01;
  2. import java.util.Scanner;
  3. //使用分支结构完成学生成绩划分:优,良,中,差
  4. public class LevelOfScore {
  5. public static void main(String[] args) {
  6. Scanner scan = new Scanner(System.in);
  7. System.out.println("请输入学生分数(0-100):");
  8. double score = scan.nextDouble();
  9. //用if-else-if完成成绩等级划分
  10. if(score < 0 || score > 100){
  11. System.out.println("输入成绩有误");
  12. }else if(score >= 90){
  13. System.out.println("优秀");
  14. }else if(score >= 80){
  15. System.out.println("良好");
  16. }else if(score > 60){
  17. System.out.println("中等");
  18. }else{
  19. System.out.println("差,要加油哦");
  20. }
  21. }
  22. }

完成命令解析程序

  1. package day01;
  2. import java.util.Scanner;
  3. public class CommendPro {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. System.out.println("请选择功能:1.显示所有记录 ,2.查询登录记录,0.退出");
  7. //功能实现
  8. int command = -1;
  9. command = scan.nextInt();
  10. scan.close();
  11. /* Scanner 类实例化的时候需要一个InputStream流作为参数,Scanner的close就是关闭InputStream流的。
  12. * 输入流,不关闭,只会占用资源。输出流,不关闭,可能会造成最后一部分数据丢失。
  13. */
  14. switch(command){
  15. case 1:
  16. System.out.println("显示全部记录");
  17. break;
  18. case 2:
  19. System.out.println("查询登录记录");
  20. break;
  21. case 0:
  22. System.out.println("退出");
  23. break;
  24. default:
  25. System.out.println("输出错误");
  26. }
  27. }
  28. }

猜数字游戏

内置1~100的数字做为猜测结果,用户猜测一次数字,然后系统大小

  1. package day01;
  2. import java.util.Scanner;
  3. //实现的循环方式有很多种
  4. public class GuessNum {
  5. public static void main(String[] args) {
  6. Scanner scan = new Scanner(System.in);
  7. //随机生成范围数字
  8. int num = (int)(Math.random() * 100 + 1);
  9. System.out.println(num);
  10. //保存用户猜测次数,用于计分
  11. int count = 0;
  12. // while(true){
  13. // if(guessNum > num){
  14. // System.out.println("猜测数字大了");
  15. // count ++;
  16. // }else if(guessNum < num){
  17. // System.out.println("猜测数字小了");
  18. // count ++;
  19. // }else{
  20. // System.out.println("恭喜你 ,猜对了");
  21. // break;
  22. // }
  23. // }
  24. for(int i=0; i<10; i++){
  25. System.out.println("请输入你猜测的整数(1 ~ 100,输入-1结束):");
  26. int guessNum = scan.nextInt();
  27. //用于判断
  28. if(guessNum == -1){
  29. System.out.println("下次再来");
  30. System.out.println("你猜测次数是:" + count);
  31. break;
  32. }else if(guessNum < num){
  33. System.out.println("猜测数字小了");
  34. count ++;
  35. }else if(guessNum > num){
  36. System.out.println("猜测数字大了");
  37. count ++;
  38. }else{
  39. System.out.println("恭喜你 ,猜对了");
  40. int grade = 100 - count * 10;
  41. System.out.println("你的到分数是:" + grade);
  42. break;
  43. }
  44. }
  45. }
  46. }

随机加法运算器

  1. package day01;
  2. import java.util.Scanner;
  3. public class RandomAddtion {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. //用来存放两个随机整数
  7. int[] arr = new int[2];
  8. //用来显示第几道题目,同时用来得出分数
  9. int count = 0;
  10. for(int i=1; i<=10; i++){
  11. arr[0] = (int)(Math.random() * 100 + 1);
  12. arr[1] = (int)(Math.random() * 100 + 1);
  13. //出题
  14. System.out.println("第(" + i + ")题目是:"+ arr[0] + " + "+ arr[1] + " = ? " +",共10道题,输入-1结束");
  15. int num = arr[0] + arr[1];
  16. System.out.println("请输入你的计算结果");
  17. int result = scan.nextInt();
  18. //用来判断结果
  19. if(result == num){
  20. count ++;
  21. }else if(result == -1){
  22. break;
  23. }
  24. }
  25. System.out.println("你的得分是:" + count*10);
  26. }
  27. }

课后作业

1. 输三个int值中最大值

  1. package day01;
  2. import java.util.Random;
  3. public class MaxOfThree {
  4. public static void main(String[] args) {
  5. Random rand = new Random();
  6. //int数组存放三个比较的数
  7. int[] arr = new int[3];
  8. //随机生成三个数,并赋值
  9. for(int i=0; i<arr.length; i++){
  10. arr[i] = rand.nextInt(100);
  11. System.out.print(arr[i] + ",");
  12. }
  13. //找出最大数
  14. //开始把下面的i写成了1,导致测试出错
  15. for(int i=1; i<arr.length; i++){
  16. if(arr[0] < arr[i]){
  17. arr[0] = arr[i];
  18. // int t = -1;
  19. // t = arr[0];
  20. // arr[0] = arr[i];
  21. // arr[i] =t;
  22. }
  23. }
  24. System.out.println("最大值是:" + arr[0]);
  25. }
  26. }

2. 排序三个数中最大数

  1. package day01;
  2. import java.util.Random;
  3. public class SortOfThree {
  4. public static void main(String[] args) {
  5. Random rand = new Random();
  6. //int数组存放三个比较的数
  7. int[] arr = new int[3];
  8. //随机生成三个数,并赋值
  9. for(int i=0; i<arr.length; i++){
  10. arr[i] = rand.nextInt(100);
  11. System.out.print(arr[i] + ",");
  12. }
  13. //普通比较排序,开始本来使用冒泡排序,没有写成功
  14. if(arr[0] > arr[1]){
  15. int t = arr[0];
  16. arr[0] = arr[1];
  17. arr[1] = t;
  18. }
  19. if(arr[0] > arr[2]){
  20. int t = arr[0];
  21. arr[0] = arr[2];
  22. arr[2] = t;
  23. }
  24. if(arr[1] > arr[2]){
  25. int t = arr[1];
  26. arr[1] = arr[2];
  27. arr[2] = t;
  28. }
  29. //遍历数字并输出
  30. for(int i : arr){
  31. System.out.println();
  32. System.out.println(i);
  33. }
  34. }
  35. }

3.输入年份和月份,输出该月的天数

  1. package day01;
  2. import java.util.Scanner;
  3. public class FindDays {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. System.out.println("请输入年份(如:2001):");
  7. int year = scan.nextInt();
  8. System.out.println("请输入月份(如:11):");
  9. int month = scan.nextInt();
  10. switch(month){
  11. case 1:
  12. System.out.println(year + "年," + month + "月有31天");
  13. break;
  14. case 3:
  15. System.out.println(year + "年," + month + "月有31天");
  16. break;
  17. case 5:
  18. System.out.println(year + "年," + month + "月有31天");
  19. break;
  20. case 7:
  21. System.out.println(year + "年," + month + "月有31天");
  22. break;
  23. case 8:
  24. System.out.println(year + "年," + month + "月有31天");
  25. break;
  26. case 10:
  27. System.out.println(year + "年," + month + "月有31天");
  28. break;
  29. case 12:
  30. System.out.println(year + "年," + month + "月有31天");
  31. break;
  32. case 4:
  33. System.out.println(year + "年," + month + "月有30天");
  34. break;
  35. case 6:
  36. System.out.println(year + "年," + month + "月有30天");
  37. break;
  38. case 9:
  39. System.out.println(year + "年," + month + "月有30天");
  40. break;
  41. case 11:
  42. System.out.println(year + "年," + month + "月有30天");
  43. break;
  44. case 2:
  45. if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
  46. System.out.println(year + "年," + month + "月有28天");
  47. }else{
  48. System.out.println(year + "年," + month + "月有29天");
  49. }
  50. }
  51. }
  52. }

4.注意for循环中特殊情况for(int i=0,j=0,k=0; i < 2 && j< 3 && k <4; i++,j++,k++){}

  1. package day01;
  2. public class Test {
  3. public static void main(String[] args) {
  4. for(int i=0,j=0,k=0; i < 2 && j< 3 && k <4; i++,j++,k++){
  5. System.out.println(i);
  6. System.out.println(j);
  7. System.out.println(k);
  8. }
  9. }
  10. }
  11. //结果是0 0 0 1 1 1,是可以这样写的

5.计算9 + 99 + 999 + ... + 9999 9999 99

  1. package day01;
  2. //最后结果可能出错出错
  3. public class Addtion999 {
  4. public static void main(String[] args) {
  5. //使用int超出范围
  6. double sum = 0;
  7. int count = 1;
  8. int num = 0;
  9. for(int i=0; i<=10; i++){
  10. count *= 10;
  11. num = count -1;
  12. sum += num;
  13. }
  14. System.out.println(sum);
  15. }
  16. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注