JAVA实验一

(一)学习总结

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

 Java中做输入的方式:通过控制台输入数据,与C语言中的scanf不同需要使用Scanner对象来操作:添加java.util.Sanner到程序中,用Sanner类定义一个对象,通过此对   象的属性来实现对数据的输入,  
 Scanner word = new Scanner(System.in);
 定义整型变量:int a = word.nextInt();
 定义字符型变量:String a=word.next();
package test;
import java.util.Scanner;
public class test {
    public static void main(String[] args) {
        Scanner word=new Scanner(System .in);
        System.out.println("输入数字");
        int a=word.nextInt();
        System.out.println("输入字符");
        String b=word.next();
        System.out.println("字符:"+b+"   数字:"+a);
    }
}

2.Random类和Math类的random()方法都能产生随机数,这两种方式有什么区别,各有什么特点呢?查阅JDK帮助文档,并举例加以说明。

  java.util包中Random类——此类的实例用于生成伪随机数流。java.lang包中Math类random() 方法返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。

  提供用于执行任意精度整数算法 (BigInteger) 和任意精度小数算法 (BigDecimal) 的类。
package test;
import java.util.Random;
import java.util.Scanner;
public class test {
    public static void main(String[] args) {
        Random a=new Random();
        int arr=a.nextInt();
        double brr=Math.random(); 
        System.out.println("调用Random()类产生的随机数");
        System.out.println(arr);
        System.out.println("调用Math.random()方法产生的随机数");
        System.out.println(brr);
        }
}

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");
        }
    }     
}

1031970-20180317134855011-1708663082.png
修改代码:

package test;
import java.math.BigDecimal;
import java.math.MathContext;
public class test {
        public static void main(String args[]) { 
            double arr = 0.1,brr = 0.1,crr = 0.1;
            if((arr + brr + crr) -0.3>0){
                System.out.println("等于0.3");
            }
            else {
                System.out.println("不等于0.3");
            }
        }     
    }

1031970-20180317135246905-1281075792.png

使用Rundom方法确定精度范围,需要用到MathContext类。

4.本次学习要点中其他需要总结的内容:因人而异,根据自己的学习情况,记录难掌握或难理解的内容,以及学有心得的内容。还存在哪些问题,也可以提出来,对于同学在博客中提出的问题,大家可以积极讨论,互帮互学。

(二)实验总结

实验内容:

1.看商品猜价格

package first;
import java.util.Random;
import java.util.Scanner;
public class first {

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

1031970-20180317201457134-1959411993.png

程序设计思路:

首先根据Random函数产生随机数,再输入数据同随机数进行比较。
定义一个count用于记录猜测次数,再根据算法把最终得分计算出来。
使用do while循环判断是否进入下一轮循环

问题:

使用do while循环输入字符的时候总是报错

原因:

还停留在C的语法上,对JAVA的字符输入不熟悉

解决方案
首先对字符赋初始值String c=null;,再从do while循环中引用c.equals("y")

2.万年历

package rili;
import java.util.Scanner;
public class rili{

    public static void main(String[] args) {
        System.out.println("1.请输入年份:");
        rili.one();
        System.out.println("2.请输入年份和月份:");
        rili.two();
        System.out.println("3.请输入年份和月份:");
        rili.three();
        System.out.println("4.请输入年份和月份用空格分开:");
        rili.four();
    }
    public static void one(){     //判断平年闰年
        Scanner word=new Scanner(System.in);
        int y=word.nextInt();
        if(y%4==0)
        {
            System.out.println("该年为闰年!");
        }
        else {
            System.out.println("该年为平年!");
        }
    }
    public static void two(){      //某月有几天
        Scanner word=new Scanner(System.in);
        int day;
        int year=word.nextInt();
        int month=word.nextInt();
        switch(month) {
        case 2:
            if(year%4==0&&(year%100!=0)||year%400==0) {
                day=29;
            }
            else {
                day=28;
            }
            break;
        case 4:
        case 6:
        case 9:
        case 11:day=30;break;
        default:day=31;
        }
        System.out.printf("该月有%d天\n",day);
    }
    public static void three(){     //某年某月据1900年1月1日有几天
        Scanner word=new Scanner(System.in);
        int year=word.nextInt();
        int month=word.nextInt();
        int i,day=0;
        int[] daymonth=new int[] {31,28,31,30,31,30,31,31,30,31,30,31};
        for(i=1900;i<year;i++) {
            if(year%4==0&&(year%100!=0)||year%400==0)
            {
                day+=366;
            }
            else {
                day+=365;
            }
        }
        if(year%4==0&&(year%100!=0)||year%400==0) {
            daymonth[1]=29;
        }
        for(i=0;i<month-1;i++) {
            day+=daymonth[i];
        }
        System.out.printf("该月份据1900年1月1日有%d天!\n",day);
    }
    public static void four(){          //万年历
        Scanner word=new Scanner(System.in);
        int year1=word.nextInt();
        int month1=word.nextInt();
        int year,day = 0,maxday = 0,day1,i,j = 0,maxday1 = 0,month;
       while(year1<1900||month1<1||month1>12)
        {
            System.out.println("Error  请重新输入 \n");
            year1=word.nextInt();
            month1=word.nextInt();
        }
        for(year=1900;year<year1;year++)
        {
            if(year%4==0&&year%100!=0||year%400==0)
            {
                day=day+366;
            }
            else
            {
                day=day+365;
            }
        } 
       
        for(month=1;month<month1;month++)
        {
            if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            {
                maxday=maxday+31;
            }
            else if(month==4||month==6||month==9||month==11)
            {
                maxday=maxday+30;
            }
            else if(month==2)
            {
                if(year1%4==0&&year1%100!=0||year1%400==0)
                {
                    maxday=maxday+29;
                }
                else
                {
                    maxday=maxday+28;
                }
            }
        }
        day1=day+maxday;
        System.out.printf("\n            ********%d年%d月********\n",year1,month1);
        System.out.println("星期日                    星期一                 星期二                星期三               星期四               星期五             星期六");
       switch(day1%7) {
            case 0: System.out.printf("\t");j=1;break;
            case 1: System.out.printf("\t\t");j=2;break;
            case 2: System.out.printf("\t\t\t");j=3;break;
            case 3: System.out.printf("\t\t\t\t");j=4;break;
            case 4: System.out.printf("\t\t\t\t\t");j=5;break;
            case 5: System.out.printf("\t\t\t\t\t\t");j=6;break;
            case 6: j=0;break;
        }
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        {
            maxday1=31;
        }
        else if(month==4||month==6||month==9||month==11)
        {
            maxday1=30;
        }
        else if(month==2)
        {
            if(year1%4==0&&year1%100!=0||year1%400==0)
            {
                maxday1=29;
            }
            else
            {
                maxday1=28;
            }
        }
        for(i=1;i<=maxday1;i++)
        {
            System.out.printf("  %2d    ",i);
            j++;
            if(j%7==0)
            {
                System.out.printf("\n");
            }
        }
        System.out.printf("\n");

    }
}

1031970-20180317201751746-1502089087.png

程序设计思路:

1.输入年份,根据 year%4==0&&(year%100!=0)||year%400==0 判断平年闰年。
2.输入年份、月份,根据 year%4==0&&(year%100!=0)||year%400==0 判断平年闰年,其中闰年2月29,平年2月28天。判断是那个月份输出天数。
3.输入年份、月份, year%4==0&&(year%100!=0)||year%400==0 判断平年闰年,把1900年到输入的年份总天数求出,再加上当年的月天数。
4.输入年份、月份, year%4==0&&(year%100!=0)||year%400==0 判断平年闰年,把1900年到输入的年份总天数求出,再加上当年的月天数day1,day1%7使用switch语句 控制输出格式打印出万年历。

问题:

1.不符合条件查漏出错
2.万年历格式不对
3.JAVA的运算符+

原因:

1.输入的时候二次定义,最开始的时候定义int year1=word.nextInt();,循环中又定义int year1=word.nextInt();
2.对于System.out.println理解不到位,System.out.println("");本身就有换行的意思
System.out.println("");用于字符串的输出
3.JAVA中的+可用于连接两个字符串

解决方案:

1.循环中再次输入的时候,直接输入year1=word.nextInt();即可不用定义。
2.在应用switch语句时将System.out.println("");改为
switch(day1%7) {
case 0: System.out.printf("\t");j=1;break;
case 1: System.out.printf("\t\t");j=2;break;
case 2: System.out.printf("\t\t\t");j=3;break;
case 3: System.out.printf("\t\t\t\t");j=4;break;
case 4: System.out.printf("\t\t\t\t\t");j=5;break;
case 5: System.out.printf("\t\t\t\t\t\t");j=6;break;
case 6: j=0;break;
}
可控制格式问题

3.评分系统

package third;
import java.util.*;
public class third {

    public static void main(String[] args) {
    System.out.println("请评委给5位选手在1-10分之内打分!\n");
    int i,j;
    int[][] arr={{9,6,8,8,7,5,6,9,7,8},{9,8,7,9,8,7,9,8,8,5},{6,5,4,9,8,7,8,6,9,7},{8,5,9,6,7,9,6,8,7,8},{9,8,5,6,7,9,8,7,5,6}};
    double[] ave = new double[5];
    System.out.println("5位选手成绩分别为:");
    for(i=0;i<5;i++){
        for(j=0;j<10;j++){
            System.out.printf("%5d",arr[i][j]);
        }
        System.out.printf("\n");
    }
    int[] max=maxscore(arr);
    System.out.println("5位选手最高成绩分别为:");
    for(i=0;i<max.length;i++)
    {
        System.out.printf("%5d",max[i]);
    }
    int[] min=minscore(arr);
    System.out.println("\n5位选手最低成绩分别为:");
    for(i=0;i<min.length;i++)
    {
        System.out.printf("%5d",min[i]);
    }
    int[] sum=sumscore(arr);
    System.out.println("\n5位选手去掉最高分最低分的总成绩分别为:");
    for(i=0;i<sum.length;i++)
    {
        System.out.printf("%5d",sum[i]);
    }
    float[] aver=ave(sum);
    System.out.println("\n5位选手平均成绩分别为:");
    for(i=0;i<aver.length;i++)
    {
        System.out.printf("%5.1f  ",aver[i]);
    }
    
    }
    public static int[] maxscore(int arr[][]){      //最大
        int i,j;
        int[] brr=new int[5];
        for(i=0;i<5;i++){
            brr[i]=arr[i][0];
            for(j=0;j<10;j++){
                     if(brr[i]<arr[i][j])
                      brr[i]=arr[i][j];
            }
        }
        return brr;
    }
    public static int[] minscore(int arr[][]){     //最小
        int i,j;
        int[] crr=new int[5];
        for(i=0;i<5;i++){
            crr[i]=arr[i][0];
            for(j=0;j<10;j++){
                 if(crr[i]>arr[i][j])
                    crr[i]=arr[i][j];
            }
        }
        return crr;
    }
    public static int[] sumscore(int arr[][]){      //求和
        int i,j;
        int[] sum=new int[5];
        int[] max=maxscore(arr);
        int[] min=minscore(arr);
        for(i=0;i<5;i++){
            for(j=0;j<10;j++) {
                sum[i]+=arr[i][j];
            }
                sum[i]-=(max[i]+min[i]);
        }
        return sum;
    }
    public static float[] ave(int sum[]) {      //平均
        int i;
        float[] ave=new float[5];
        for(i=0;i<sum.length;i++) {
            ave[i]=(float) (sum[i]/3.0);
        }
        return ave;
    }
}

1031970-20180317201846471-1965848714.png

程序设计思路:

1.首先对二维数组初始化,即每位选手成绩。
2.求最大最小值时首先定义一个一位数组,使其等于每行第一个数依次比较大小,找到最大的或最小的存入数组。
3.求和取平均时先把每位选手所有成绩相加,再减去最大值最小值,取平均值(注意变量float)。

问题:
1.注意定义变量
2.数组输出的格式不对
3.JAVA中二维数组的使用

原因:

平均值是小数应定义成浮点型
数组输出时应是max[i]
使用数组时先为改为分配空间,再为低维分配空间。

解决方法:
调用数组时要注意声名是什么类型的 ,以及不要忘了中括号。

(三)代码托管

1031970-20180317223027547-463372613.png

转载于:https://www.cnblogs.com/333xar/p/8583928.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值