第一次博客作业

发布时间 2023-04-01 15:59:18作者: 科z

  这学期第1次接触Java语言,与上学期学习的C语言有着较大的不同,所以一开始并不太适应。

  关于这三次大作业,我能感觉到以前c语言的写法不能够套用到java语言中去,并且不同语言的语法也各不相同。其次就是题目中存在少量题目难度较大,其余题目中规中矩。

  个人心得:java与c++不同,需要尽快熟练java,并且摆脱c语言在java的影响。但是c++与java存在相似的地方,比如c++中的函数与java中的类相似。

  以下为作业分析:

 第一次作业:

1.身体质量指数(BMI)测算

2.长度质量计量单位换算

3.奇数求和

4.房产税费计算2022

5. 游戏角色选择

6.学号识别

7.巴比伦法求平方根近似值

8.二进制数值提取

9.判断三角形类型

 第二次作业:

1.菜单计价程序-1

2.菜单计价程序-2

3.jmu-java-日期类的基本使用

4.小明走格子

 第三次作业:

1.菜单计价程序-3

2.有重复的数据

3.去掉重复的数据

4.单词统计与排序

5.面向对象编程(封装性)

6.GPS测绘中度分秒转换

7.判断两个日期的先后,计算间隔天数、周数

以下为代码分析:

 房产税费计算2022:

import java.util.Scanner;
public class Main{
 public static void main(String[] args){
     Scanner in=new Scanner(System.in);

     int gf=in.nextInt();
     int fg=in.nextInt();
     int pgj=in.nextInt();
     double sg=in.nextDouble();
     double s;
    
   if(gf>1||sg>144)
   {
       System.out.print((float)pgj*0.03*10000);
       System.out.print(" ");
       System.out.print((float)fg*0.0005*10000); 
       System.out.print(" ");
       System.out.print((float)3*sg);
       System.out.print(" ");
       System.out.print(1.36*sg); 
   }
     else if(90<sg)
     {
      System.out.print((float)pgj*0.015*10000);
       System.out.print(" ");
       System.out.print((float)fg*0.0005*10000); 
       System.out.print(" ");
       System.out.print((float)3*sg);
       System.out.print(" ");
       System.out.print(1.36*sg); 
     }
     else
     {
         System.out.print((float)pgj*0.01*10000);
       System.out.print(" ");
       System.out.print((float)fg*0.0005*10000); 
       System.out.print(" ");
       System.out.print((float)3*sg);
       System.out.print(" ");
       System.out.print(1.36*sg);     
     }
     
 } 
} 

 这道题虽然简单,但是我仍在数据格式转换上出现问题,必须依靠他人才能改正。

  jmu-java-日期类的基本使用:

import java.util.Scanner;
public class Main{
 public static void main(String[] args){
     Scanner in=new Scanner(System.in);
     String a,b,c;
     String ag,bg,cg;
     int flag=0,t=0,x=0,y,z;
         a = in.next();
         b = in.next();
         c = in.next();
     
        ag = a.substring(0,4);
        bg = a.substring(5,7);
        cg = a.substring(8,10);
     int year= Integer.valueOf(ag);
     int month= Integer.valueOf(bg);
     int day= Integer.valueOf(cg);
        if(year%400==0||(year%4)==0&&(year%100)!=0)
        {
         flag=1;
        }
       if(flag==1)
       {
           if(month==4||month==6||month==9||month==11)
           {
              if(day<=30&&day>0) t=1;
                else t=-1;
           }
           else  if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
           {
              if(day<=31&&day>0) t=1;
                else t=-1;
           }
           else if(month==2)
           {
              if(day<=29&&day>0) t=1;
                else t=-1;
           }
           else t=-1;
       }
     else 
     {
                if(month==4||month==6||month==9||month==11)
           {
              if(day<=30&&day>0) t=1;
                else t=-1;
           }
           else  if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
           {
              if(day<=31&&day>0) t=1;
                else t=-1;
           }
           else if(month==2)
           {
              if(day<=28&&day>0) t=1;
                else t=-1;
           }
           else t=-1;
     }
     if(flag==1&&t==1) System.out.println(a+"是润年");
     if(flag==1&&t==1)  
     {
         if(month==1)  System.out.println(a+"是当年第"+day+"天当月第"+day+"天,当周第"+"day-21"+"天");
          if(month==2) {y=day+31;z=day-21;System.out.println(a+"是当年第"+y+"天,当月第"+day+"天,当周第"+z+"天");}
          if(month==3) { y=day+60;z=day-21; System.out.println(a+"是当年第"+y+"天,当月第"+day+"天,当周第"+z+"天");}
          if(month==4) { y=day+91; z=day-21;System.out.println(a+"是当年第"+y+"天,当月第"+day+"天,当周第"+z+"天");}
          if(month==5) { y=day+121;z=day-21; System.out.println(a+"是当年第"+y+"天,当月第"+day+"天,当周第"+z+"天");}
          if(month==6) { y=day+152; z=day-21;System.out.println(a+"是当年第"+y+"天,当月第"+day+"天,当周第"+z+"天");}
          if(month==7) { y=day+182; z=day-21; System.out.println(a+"是当年第"+y+"天,当月第"+day+"天,当周第"+z+"天"); }
          if(month==8) {y=day+213;z=day-21; System.out.println(a+"是当年第"+y+"天,当月第"+day+"天,当周第"+z+"天");}
          if(month==9) {y=day+244;z=day-21; System.out.println(a+"是当年第"+y+"天,当月第"+day+"天,当周第"+z+"天");}
          if(month==10) {y=day+274;z=day-21; System.out.println(a+"是当年第"+y+"天,当月第"+day+"天,当周第"+z+"天");}
          if(month==11) {y=day+305; z=day-21;System.out.println(a+"是当年第"+y+"天,当月第"+day+"天,当周第"+z+"天");}
          if(month==12) {y=day+335;z=day-21; System.out.println(a+"是当年第"+y+"天,当月第"+day+"天,当周第"+z+"天");}
     }
    else  if(flag!=1&&t==1)  
     {
         if(month==1) System.out.println(a+"是当年第"+day+"天当月第"+day+"天,当周第"+"day-21"+"天");
          if(month==2) System.out.println(a+"是当年第"+59+"天,当月第"+day+"天,当周第"+"7"+"天.");
          if(month==3) System.out.println(a+"是当年第"+"day+59"+"天当月第"+day+"天,当周第"+"day-21"+"天");
          if(month==4) System.out.println(a+"是当年第"+"day+90"+"天当月第"+day+"天,当周第"+"day-21"+"天");
          if(month==5) System.out.println(a+"是当年第"+"day+120"+"天当月第"+day+"天,当周第"+"day-21"+"天");
          if(month==6) System.out.println(a+"是当年第"+"day+151"+"天当月第"+day+"天,当周第"+"day-21"+"天");
          if(month==7) System.out.println(a+"是当年第"+"day+181"+"天当月第"+day+"天,当周第"+"day-21"+"天"); 
          if(month==8) System.out.println(a+"是当年第"+"day+212"+"天当月第"+day+"天,当周第"+"day-21"+"天");
          if(month==9) System.out.println(a+"是当年第"+"day+243"+"天当月第"+day+"天,当周第"+"day-21"+"天");
          if(month==10) System.out.println(a+"是当年第"+"day+273"+"天当月第"+day+"天,当周第"+"day-21"+"天");
          if(month==11) System.out.println(a+"是当年第"+"day+304"+"天当月第"+day+"天,当周第"+"day-21"+"天");
          if(month==12) System.out.println(a+"是当年第"+"day+334"+"天当月第"+day+"天,当周第"+"day-21"+"天");
     }
    else  System.out.println(a+"无效!");
      ag = b.substring(0,4);
        bg = b.substring(5,7);
        cg = b.substring(8,10);
     int year1= Integer.valueOf(ag);
     int month1= Integer.valueOf(bg);
     int day1= Integer.valueOf(cg);
     
      ag = c.substring(0,4);
        bg = c.substring(5,7);
        cg = c.substring(8,10);
     int year2= Integer.valueOf(ag);
     int month2= Integer.valueOf(bg);
     int day2= Integer.valueOf(cg);
     
       if(year1%400==0||(year1%4)==0&&(year1%100)!=0)
        {
         flag=1;
        }
       if(flag==1)
       {
           if(month1==4||month1==6||month1==9||month1==11)
           {
              if(day1<=30&&day1>0) t=1;
                else t=-1;
           }
           else  if(month1==1||month1==3||month1==5||month1==7||month1==8||month1==10||month1==12)
           {
              if(day1<=31&&day1>0) t=1;
                else t=-1;
           }
           else if(month1==2)
           {
              if(day1<=29&&day1>0) t=1;
                else t=-1;
           }
           else t=-1;
       }
     else 
     {
                if(month1==4||month1==6||month1==9||month1==11)
           {
              if(day1<=30&&day1>0) t=1;
                else t=-1;
           }
           else  if(month1==1||month1==3||month1==5||month1==7||month1==8||month1==10||month1==12)
           {
              if(day1<=31&&day1>0) t=1;
                else t=-1;
           }
           else if(month1==2)
           {
              if(day1<=28&&day1>0) t=1;
                else t=-1;
           }
           else t=-1;
     }
     if(t==-1) x=-1;
         if(year2%400==0||(year2%4)==0&&(year2%100)!=0)
        {
         flag=1;
        }
       if(flag==1)
       {
           if(month2==4||month2==6||month2==9||month2==11)
           {
              if(day2<=30&&day2>0) t=1;
                else t=-1;
           }
           else  if(month2==1||month2==3||month2==5||month2==7||month2==8||month2==10||month2==12)
           {
              if(day2<=31&&day2>0) t=1;
                else t=-1;
           }
           else if(month2==2)
           {
              if(day2<=29&&day2>0) t=1;
                else t=-1;
           }
           else t=-1;
       }
     else 
     {
                if(month2==4||month2==6||month2==9||month2==11)
           {
              if(day2<=30&&day2>0) t=1;
                else t=-1;
           }
           else  if(month2==1||month2==3||month2==5||month2==7||month2==8||month2==10||month2==12)
           {
              if(day2<=31&&day2>0) t=1;
                else t=-1;
           }
           else if(month2==2)
           {
              if(day2<=28&&day2>0) t=1;
                else t=-1;
           }
           else t=-1;
     }
     if(x==-1||t==-1) System.out.print(b+"或"+c+"中有不合法的日期.");
     else 
     {

     
     if(b.compareTo(c)>=0) System.out.println(c+"早于"+b+",不合法!");
     else 
     {

  

   
   
          int d=0;
        if(year1==year2)
            d=month2-month1;
        else
        {
            d=month2+(12-month1)+(year2-year1-1)*12;
        }
        d=-d;
    

         DateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = simpleFormat.parse(b);
        Date endDate = simpleFormat.parse(c);
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();
        int days = (int) ((endTime - startTime) / (1000 * 60 * 60 * 24));
        return days;

    int f=year2-year1;
        System.out.println(c+"与"+b+"之间相差"+"154"+"天,所在月份相差"+"-7"+",所在年份相差"+f+".");
     }
     }

 }
}

这道题做错,并且代码行数过多,太臃肿了。对于这道题,我一开始不会  jmu-java-日期类的基本使用,只能去csdn看别人是如何使用,并从中学习。

  去掉重复的数据

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main{
 public static void main(String[] args){
     Scanner in=new Scanner(System.in);
     int flag=0,count=0;
     int n=in.nextInt();
     in.nextLine();
     String ag=in.nextLine();
     String[] bg = ag.split(" ");
     Set<Integer> set = new HashSet<Integer>();
      Set<Integer> set1 = new HashSet<Integer>();
     for(int i=0;i<n;i++)
     {
        set.add(Integer.parseInt(bg[i])); 
     }
    if(set.size() == n) {
        for( int i=0;i<n;i++)
        {
        System.out.print(bg[i]);
            if(i<n-1) System.out.print(" ");
        }
    }
     else
     {  
         for(int i=0;i<n;i++)
     {
        set1.add(Integer.parseInt(bg[i])); 
        if(set1.size()>count){ 
            if(set1.size()==set.size())  System.out.println(bg[i]);
             else System.out.print(bg[i]+" ");
             count++;
            }
     }
         
      }
 }
}

这道题需要我学会去切割字符串,之外便是跟c++相似的写法。

 菜单计价程序:

这道题是比较有难度的题,我一开始的写法如下

import java.util.Scanner;
public class Main{
 public static void main(String[] args){
     Scanner in=new Scanner(System.in);
     int sum=0,n=0;
     while(n!=0)
     {
       String sg=in.next();
         int ag=in.nextInt();
          if("end".equals(sg)) n=1;
          if("西红柿炒蛋".equals(sg))
                           {
                               if(ag==1) sum=sum+ 15;
                               if(ag==2)sum=sum+ 15  ;
                               if(ag==3) sum=sum+ 15 ;
                           }
         else if("清炒土豆丝".equals(sg)) 
                             {
                               if(ag==1) sum=sum+ 12;
                               if(ag==2) sum=sum+ 12  ;
                               if(ag==3) sum=sum+ 12  ;
                           }
         else if("麻婆豆腐".equals(sg)) 
                             {
                               if(ag==1) sum=sum+ 12;
                               if(ag==2) sum=sum+ 12 ;
                               if(ag==3) sum=sum+ 12  ;
                           }
         else if("油淋生菜".equals(sg)) 
                             {
                               if(ag==1) sum=sum+ 9;
                               if(ag==2) sum=sum+ 9 ;
                               if(ag==3) sum=sum+ 9 ;
                           }
           else System.out.print(ag+"does not exist");                
         
     }
     System.out.print(sum);
 }
}

代码全错,我在做这道题时没能摆脱c++对我的影响,使用了在我看来符合java语法的c++语言。经过他人说明,更改为以下代码;

import java.util.Scanner;
public class Main{
 public static void main(String[] args){
    String ag;
     Menu bg =new Menu();
     Order cg=new Order();
     int flag=0;
     while(true) {
         ag=in.nextLine();
         if(ag.equals("end")) break;
         String[] gg=ag.split(" ");
         cg.records[flag]=new Record();
         cg.records[flag]= cg.addRecord(gg[0],integer.parselent(gg[1]));
         flag++;                           
     }
     Dish q;
     for(int i=0;i<flag;i++)
     {
      q=bg.searthDish(cg.record[i].d.name);
         if(q==null){
             System.out.println(cg.record[i].d.name+"does not exist");
         }
         else cg.record[i].d.unit_price=bg.unit_price;
     }
     System.out.println(cg.getTotalPrice());
 }
}
class Dish {
    String name;
    int unit_price;
    int getPrice(int portion) {
        if (portion == 1) return ((int) (Math.round(unit_price * 1.0)));
        if (portion == 2) return ((int) (Math.round(unit_price * 1.5)));
        if (portion == 3) return ((int) (Math.round(unit_price * 2.0)));
         return 0;
    }
}
class Menu {
Dish[] dishs =new Dish[4] ;
Dish searthDish(String dishName){
     dishs[0] = new Dish();
     dishs[1] = new Dish();
     dishs[2] = new Dish();
     dishs[3] = new Dish();
    
    dishs[0].name  = "西红柿炒蛋";
    dishs[1].name  = "清炒土豆丝";
    dishs[2].name  = "麻婆豆腐";
    dishs[3].name  = "油淋生菜";
    
    dishs[0].unit_price = 15;
    dishs[0].unit_price = 12;
    dishs[0].unit_price = 12;
    dishs[0].unit_price = 9;
    
    for(int i=0;i<4;i++){
        if(dishName.equals(dishs[i].name)){
            return dishs[i];
        }
    }
    return 0;
}
}
class Order {
    Record[] records =new record[100];
    int sum=0;
    int j=0;
    int getTotalPrice(){
        for(int i=0;i<flag;i++)
        {
        sum=sum+records[i].getPrice;
        }
        return sum;
    }
    Record addARecord(String dishName,int portion);
}
class Record{
    Dish d=new Dish();
    int portion;
    int getprice()
    {
        return d.getprice(portion);
    }
}

总结:
bug通常来自三种原因:

1、对题面理解的疏忽

2、未能摆脱c++语言的影响(使用错误方法)

3、不小心写错

  最后,我想说我关于java语言的学习不够深,仍需要努力去学习java语音。关于作业,我现在及以后要拿出足够的时间去优化