• # Java第一次作业


    (一)学习总结

    1.Scanner类是从控制台输入差不多的意思就是说从键盘上输入,但是就像C语言那样有前提的需要有前提的声明import java.util.Scanner,这个类可以从键盘上输入所有类型的变量,当然是要在Java中允许的,从键盘写入的都是字符串当你需要是么样的类型是.next你想要的类型第一字母大写,

         import java.util.Scanner;
    public class Test {
    public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    int length=input.nextInt();//输入一个正整数
    System.out.println("输入的数字是:"+length);}
    }
    
    1. Random类产生随机数的方法就是普通的产生随机数的方法只要给定范围你就可以生成随机数
      Math的产生随机数的方法是math就只能产生0~1的double类型的小数你还要将它强制转换成int类型的数
      两种方法个有利弊,要看每个人的习惯来定,Random类里边还提供了很多对随机数处理的方法。
    import java.util.*;
    public class RandomNum{
    	 public static void main(String[] args){ 
    		 Random r1 = new Random(100);
    		 int a=0;
    		 System.out.println("r1产生的值:"+r1.nextInt()); 
    		a=(int)(Math.random()*100);
    		 System.out.println("Math.random产生的值:"+a); 
    		 }
    	 }
    

    结果是不等于0.3,在计算机里面定义的double类型其实他实际上他储存的并一定是你给定的数,所以结果会出错,计算机只能储存二进制信息,0.1在二进制转换成double时不是将所有的进制数转换所以会出现错误,
    改进方法:

    import java.math.BigDecimal;
    public class Test {
            public static void main(String args[]) { 
                double a = 0.1;
                double b = 0.1;
                double c = 0.1;
                if(Math.round(Math.add(a, b, c), 1) == 0.3){     
                    System.out.println("等于0.3");
                }else {
                    System.out.println("不等于0.3");
                }
            }     
        }
    class Math{
        public static double add(double a1,double a2,double a3){  
            BigDecimal b1=new BigDecimal(a1);
            BigDecimal b2=new BigDecimal(a2);
            BigDecimal b3=new BigDecimal(a3);
            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();
        }
    }
    

    这个东西我是很绝望的,一开始出来的是.3000000000004,我...然后仔细的看了一下书上的代码,明白了类自己还没去看所以有点蒙,原来他是可以控制长度的。
    4.总结
    Java是一个非常严谨的语言,刚开始学有些Cc++的臭毛病需要改还有真是一门新的语言有些东西不一样需要习惯一些东西,至于问题的话就有很多了

    (二).

    1.看商品猜价格

    package 猜价格;
    
    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.万年历

    package 万年历;
    
    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.评分系统

    package 评分系统;
    
    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.看商品猜价格
    这个题思路挺清晰的,就是刚开始写Java程序的时候输入的时候还有点蒙,忽然间不知道该怎么写,还有就是字符串的比较,刚开始以为Java没有char型的,后来知道了原来需要多一步可以间接生成char变量,小麻烦而已啦,设计思路就是小循环里五次输入判断大小,记上分数,大循环是否要继续,有一个字符串的你比较,最后输出计算总分。
    问题: 就是字符串的比较的时候需要提前声明他,
    2. 万年历 之前写过这个题,所以方法思路都有,就是有各种的判断,
    问题 也没啥,
    3. 评分系统
    评分系统的刚开始是想的用一个二维数组来表示这个分数,但是后来想了想,可以用一个一维数组,边生成边存储数据,同时找出最大值最小值,计算平均数放到另一个二维数组里面,然后进行冒泡排序,将这个人的编号同他的分数同时进行排序,输出结果就行。
    问题:我明明知道可以用sort进行排序但是就是不知道怎么用。

    (三)代码托管

  • 相关阅读:
    python json模块 pickli模块
    python sys模块
    python os模块
    python random模块使用
    react项目搭建
    PHP开发环境及搭建
    Vue2之页面 、js 、css分离
    Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.
    Uncaught Error: Bootstrap's JavaScript requires jQuery
    Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
  • 原文地址:https://www.cnblogs.com/saber114567/p/8552236.html
Copyright © 2020-2023  润新知