• JAVA《第一次作业》


    (一)学习总结:
    1.在java中通过Scanner类完成控制台的输入,查阅JDK帮助文档,Scanner类实现基本数据输入的方法是什么?不能只用文字描述,一定要写代码,通过具体实例加以说明。

    package study;
    
    import java.util.Scanner;
    
    public class Study {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
            int x;
            Scanner input = new Scanner(System.in);
            x = input.nextInt();
            System.out.println(x);
    	}
    
    }
    

    2.Random类和Math类的random()方法都能产生随机数,这两种方式有什么区别,各有什么特点呢?查阅JDK帮助文档,并举例加以说明。
    (1):Math类的random()只能返回0-1之间的double数值,如果需要20-30需要放大10倍变为0-10之间的double数值,之后再加上10并强制转换成int数值。
    (2):Random类是工作Random类的相关方法,有多种类型的随机值的获取方法,如nextBoolean()、 nextInt()等。
    3.3.运行下列程序,结果是什么?查阅资料,分析为什么。
    结果为不等于0.3。
    原因是因为计算机计算double类型数据时会产生误差。
    (二)实验总结:
    1.看商品猜价格

    package study;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class Study {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int count,score,i;
            String c=null;
            Scanner input=new Scanner(System.in);
            do
            {
                Random arr=new Random(); 
                int arr1 = arr.nextInt(100)+1;
                count=1;
                System.out.println ("请输入商品价格,最多可以猜5次!");
                int price=input.nextInt();
                for(i=0;i<5;i++)
                {
                    if (price>arr1)
                    {
                         System.out.println ("太大了,请重新输入!");  
                         count++;
                    }
                    else  if (price<arr1)
                    {
                         System.out.println("太小了,请重新输入!");
                         count++;
                    }
                    else if (price==arr1)
                    {
                        System.out.println("恭喜你,答对了!");
                        break;
                    }
                    price=input.nextInt();
                    
                   if (count==5)
                   {
                       System.out.printf("5次都没猜对,正确结果是%d
    ",arr1);
                       break;
                   }
                }
                score=100-count*10;
                System.out.printf ("一共猜错了%d 次,您的得分为%d",count,score);
                System.out.println ("继续下一轮游戏吗?Y(y)orN(n)");
                c=input.next();
                }
            while(c.equals("y"));
    	}
    
    }
    

    2.万年历

    package calender;
    
    import java.util.Scanner;
    
    public class Calender {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		int i,h=0,n,year,year1,month,zong = 0,sum,sum1,sum2;
            Scanner input = new Scanner(System.in);
    	    while (true) {
                System.out.println("请输入年份");
                year = input.nextInt();
                System.out.println("请输入月份");
                month = input.nextInt();
                if ((year < 1900 )||( month < 1) ||( month > 12)) {
                    System.out.println("输入错误,请重新输入");
                } else {
                   
                }
    	    sum1=0;
    	    sum2=0;
    	    year1=year;
    	    for(year1=year1-1;year1>=1900;year1--)
    	    {
    	        if(year1%4==0&&year1%100!=0||year1%400==0)
    	        {
    	            sum1=sum1+366;
    	        }
    	        else
    	        {
    	            sum1=sum1+365;
    	        }
    	    }
    	    for(i=1;i<month;i++)
    	    {
    	        if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
    	        {
    	            sum2=sum2+31;
    	        }
    	        else if(i==4||i==6||i==9||i==11)
    	        {
    	            sum2=sum2+30;
    	        }
    	        else if(i==2)
    	        {
    	            if(year%4==0&&year%100!=0||year%400==0)
    	            {
    	                sum2=sum2+29;
    	            }
    	            else
    	            {
    	                sum2=sum2+28;
    	            }
    	        }
    	    }
    	    sum=sum1+sum2;
    	    System.out.println(year+"年"+month+"月");
    	    System.out.println("星期日	星期一	星期二	星期三	星期四	星期五	星期六
    ");
    	    switch(sum%7)
    	    {
    	        case 0:System.out.printf("	");h=2;break;
    	        case 1:System.out.printf("		");h=3;break;
    	        case 2:System.out.printf("			");h=4;break;
    	        case 3:System.out.printf("				");h=5;break;
    	        case 4:System.out.printf("					");h=6;break;
    	        case 5:System.out.printf("						");h=7;break;
    	        case 6:h=1;break;
    	    
    	    }
    	
    	    if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
    	    {
    	        zong=31;
    	    }
    	    else if(month==4||month==6||month==9||month==11)
    	    {
    	        zong=30;
    	    }
    	    else if(month==2)
    	    {
    	        if(year%4==0&&year%100!=0||year%400==0)
    	        {
    	            zong=29;
    	        }
    	        else 
    	        {
    	            zong=28;
    	        }
    	    }
    	    for(n=1;n<=zong;n++,h++)
    	    {
    	    	System.out.printf("  %2d    ",n);
    	        if(h%7==0)
    	        {
    	        	System.out.printf("
    ");
    	        }
    	    }
    	    System.out.printf("
    ");
    }
    }
    }
    
  • 相关阅读:
    9.11 eventbus
    9.10,,,实现new instanceof apply call 高阶函数,偏函数,柯里化
    9.9 promise实现 写完了传到gitee上面了,这里这个不完整
    9.5cors配置代码
    9.5 jsonp 实现
    9.5 http tcp https总结
    9.3 es6 class一部分 and es5 class 发布订阅
    8.30 cookie session token jwt
    8.30vue响应式原理
    warning: LF will be replaced by CRLF in renard-wx/project.config.json. The file will have its original line endings in your working directory
  • 原文地址:https://www.cnblogs.com/kbx1602/p/8598282.html
Copyright © 2020-2023  润新知