• #《Java技术》第一次作业


    (一)学习总结

    1.在java中通过Scanner类完成控制台的输入,查阅JDK帮助文档,Scanner类实现基本数据输入的方法是什么?不能只用文字描述,一定要写代码,通过具体实例加以说明。
    需要导入java.util.Scanner,或者java.util.*(它是万能的)
    整型就是nextInt(),双精度就是把Int换成Double,next()就是读取输入的下一个(空格分开);
    特别注意字符串类型,没输入的方法;

    import java.util.Scanner;
    public class A {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int num = input.nextInt();
    System.out.println(num);
        }
    }
    

    2.Random类和Math类的random()方法都能产生随机数,这两种方式有什么区别,各有什么特点呢?查阅JDK帮助文档,并举例加以说明。
    Math.random()返回的只是从0.0到1.0之间的小数,如果要0到10,就先放大10倍,即0到10之间,这里还是小数,
    如果要整数,就强制转换int,要是想要50到100之间,然后再加上50即为50~100.
    最终代码:

    import java.util.*;
    public class A {
    public static void main(String[] args) {
    	 int x=(int)(Math.random()*10+50);
         System.out.println(x); //57
    	}
    }
    

    Random类产生的随机数,在其最大值范围内,按照概率均匀分布的。

    import java.util.*;
    public class A {
    public static void main(String[] args) {
    	Random rand = new Random(); 
        int x=rand.nextInt(50)+50; 
        System.out.println(x); //77
        }
    }
    

    3.运行下列程序,结果是什么?查阅资料,分析为什么。

    public class Test {
        public static void main(String args[]) { 
            double a = 0.1;
            double b = 0.1;
            double c = 0.1;
            if((a + b + c) == 0.3){
                System.out.println("等于0.3");
            }else {
                System.out.println("不等于0.3");
            }
        }     
    }
    

    正确的代码:

    import java.math.BigDecimal;
    public class A {
            public static void main(String args[]) { 
                double a = 0.1;
                double b = 0.1;
                double c = 0.1;
                if(MyMath.round(MyMath.add(a, b, c), 1) == 0.3){     
                    System.out.println("等于0.3");
                }else {
                    System.out.println("不等于0.3");
                }
            }     
        }
    class MyMath{
        public static double add(double d1,double d2,double d3){  
            BigDecimal b1=new BigDecimal(d1);
            BigDecimal b2=new BigDecimal(d2);
            BigDecimal b3=new BigDecimal(d3);
            return b1.add(b2).add(b3).doubleValue();
        }
        public static double round(double d,int len) {
        	BigDecimal b1=new BigDecimal(d);
            BigDecimal b2=new BigDecimal(1);
            return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue();
        }
     }
    

    根据书上的那个程序改的 其实并不懂

    (二)实验总结

    1.看商品猜价格

    import java.util.*;
    
    public class Cai {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner in = new Scanner(System.in);
            Scanner str2 = new Scanner(System.in);
            String str = new String("yes");
            String str1 = new String("yes");
            Random rand = new Random();
            int a, b, c, sum = 0, d = 0, sum1 = 0;
            System.out.println("你有五次猜价格的机会,好好把握吧");
            while (str.equals(str1)) {
                d++;
                sum = 0;
                System.out.println("请输入价格");
                b = rand.nextInt(100);
                go: for (a = 0; a <5; a++) {
                    System.out.println("你还有"+(5-a)+"次机会");
                    c = in.nextInt();
                    if (c > b) {
                        System.out.println("猜大了");
                    }
                    if (c < b) {
                        System.out.println("猜小了");
                    }
                    if (c == b) {
                        sum = 100 - a * 20;
                        System.out.println("猜对了你的得分是" + (100 - a * 20));
                        break go;
                    }
                }
                if (a == 5)
                    System.out.println("游戏结束你没有猜对数为" + b);
                sum1 = sum1 + sum;
                System.out.println("请确认是否还要继续是请输入yes否则no");
                str1 = str2.next();
            }
            System.out.println("你一共猜了" + d + "次你的总分是" + sum1);
        }
    }
    

    2.万年历

    import java.util.*;
    
    public class Wannianli {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("输入年份和月份,输出该月的日历,已知1900年1月1日是星期一");
            Scanner in = new Scanner(System.in);
            int year, mouth;
            year = in.nextInt();
            mouth = in.nextInt();
            printCalender(year, mouth);
        }
    
        public static boolean isLeap(int year) {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
                return true;
            else
                return false;
        }
    
        public static int dayS(int year, int mouth) {
            int a = 0;
            if (isLeap(year)) {
                if (mouth == 2) {
                    a = 29;
                }
                if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
                    a = 31;
                }
                if (mouth == 4 || mouth == 6 || mouth == 9 || mouth == 11) {
                    a = 30;
                }
    
            } else {
                if (mouth == 2) {
                    a = 28;
                }
                if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
                    a = 31;
                }
                if (mouth == 4 || mouth == 6 || mouth == 9 || mouth == 11) {
                    a = 30;
    
                }
    
            }
            return a;
        }
    
        public static int totalDays(int year, int mouth) {
            int sum = 0;
            for (int i = 1900; i < year; i++) {
                if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))
                    sum = sum + 366;
                else
                    sum = sum + 365;
            }
            for (int m = 1; m < mouth; m++) {
                sum = sum + dayS(year, m);
            }
            return sum;
        }
    
        public static void printCalender(int year, int mouth) {
            int sum1 = totalDays(year, mouth);// 月之前
            int sum2 = dayS(year, mouth);// 月的天数
            int sum3 = sum2;
            System.out.println("共" + sum1+"天");
            System.out.println("这个月有" + sum2+"天");
            System.out.println("星期一 	星期二	星期三	星期四	星期五	星期六	星期日");
            int a = sum1 % 7;// System.out.print(""+a);
            if (a == 7)
                a = 0;
            for (int m = 1; m <= a; m++)
                System.out.print("	");
            int flag = a;
            for (int i = 1; i <= sum3; i++) {
    
                if (flag % 7 == 0 && flag != 0)
                    System.out.println("");
                flag++;
                System.out.print(i + "	");
            }
    
        }
    }
    

    3.评分系统

    import java.util.*;
    
    public class Pingfen {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Random rand = new Random();
            int max = 0, min = 0;
            int score[] = new int[10];
            double man[][] = new double[5][2];
            for (int i = 0; i < 5; i++) {
                
                for (int j = 0; j < score.length; j++) {     //对每一个人
                    int in = rand.nextInt(10);
                    score[j] = in;
                }
                max = max(score);
                // System.out.println("" + max);
                min = min(score);
                // System.out.println("" + min);
                    System.out.print("第" + (i+1) + "人的评分是          ");
                    for (int n = 0; n < score.length; n++) {
    
                        System.out.print("  " + score[n]);
    
                    }
                System.out.println("");
                int flag = 0, flag1 = 0;
                for (int m = 0; m < score.length; m++) {
                    if (max == score[m] && flag == 0) {
                        score[m] = 0;
                        flag = 1;
                    }
                    if (min == score[m] && flag1 == 0) {
                        score[m] = 0;
                        flag1 = 1;
                    }
                }
                man[i][0] = avge(score);
                man[i][1] = i;
            }
            for (int j = 0; j < man.length - 1; j++)
                for (int i = 0; i < man.length - j - 1; i++) {
                    if (man[i][0] > man[i + 1][0]) {
                        double m = man[i][0];
                        man[i][0] = man[i + 1][0];
                        man[i + 1][0] = m;
                        double n = man[i][1];
                        man[i][1] = man[i + 1][1];
                        man[i + 1][1] = n;
                    }
    
                }
            for (int i = 0; i < man.length; i++) {
                System.out.printf("编号为%.0f 成绩为%.2f
    ", man[i][1] + 1, man[i][0]);
            }
        }
    
        public static int max(int x[]) {
            int max1 = x[0];
            for (int i = 0; i < x.length; i++) {
                if (max1 <= x[i]) {
                    max1 = x[i];
                }
            }
            return max1;
        }
    
        public static int min(int x[]) {
            int min1 = x[0];
            for (int i = 0; i < x.length; i++) {
                if (min1 >= x[i]) {
                    min1 = x[i];
                }
            }
            return min1;
        }
    
        public static double avge(int x[]) {
            int sum = 0;
            for (int i = 0; i < x.length; i++) {
                sum += x[i];
            }
            double ave = (double) sum / 8.0;
            return ave;
        }
    
    }
    

    1.看商品猜价格

    • 程序设计思路:因为要猜好几次,所以要使用循环for循环就可以,拓展里要选择是否进行下一次游戏,所以使用do while
    • 实验问题分析:在使用equals时出错
      解决方法:上网查一个简单的使用equals的代码,看看他怎么使用的
      2.万年历
    • 程序设计思路:判断闰年就是用和4取余等于0并且和100取余不等于0或者和400取余等于0,判断每个月有多少天 特别注意二月,算距离1900.1.1有多少天的时候 首先算距离1900有多少年 乘以365 在算中间有多少个闰年 算该月距离该年的1月1日有多少天 相加就好
    • 实验问题分析:
      问题1:计算天数出错
      解决方法:在算月距离该年1月1日的循环中 传参的时候误把月份的那个 写成输入的那个固定月份 只需改成i就好
      问题2:输出格式很乱 每月1号总是对不齐
      解决方法:写那个循环输出空格的时候 若是星期4 则循环输出3次就好
      3.评分系统
    • 程序设计思路:自定义5个人得的分数,求最大值 定义一个数组存每一行的第一个数 让他和改行的数比较 那个数大 就把它赋值给存的那个 求最小值类似 求平均值 先求和 在除以8 方法写成double型的
    • 实验问题分析:输出数组的时候 会提示越界
      解决方法:在循环那 改成数组名.length 这样循环次数就不会出错了

    (三)代码托管

    • 码云提交历史截图


  • 相关阅读:
    CSS边框,背景,边距,溢出
    数字电子技术课程设计之基于触发器的三位二进制同步减法计数器无效态000/110
    集成电路版图与工艺课程设计之用CMOS实现Y=AB+C电路与版图
    金属磁记忆传感器封装
    基于FPGA 的8b10b编解码电路前端电路设计
    Css颜色和文本字体
    Css中的选择器
    Css基本语法及页面引用
    65 插入排序
    63 散列表的查找
  • 原文地址:https://www.cnblogs.com/zj1220/p/8592488.html
Copyright © 2020-2023  润新知