首页 > 编程语言 >java 实验总结

java 实验总结

时间:2023-05-24 13:55:26浏览次数:39  
标签:总结 java String int System 实验 println public out

(1)前言:前三次的题目集,大概囊括了最基础的输入输出、类的创建;字符串的各种操作(定位某个特点字符在字符串中的下标、提取某段需要的子字符串、对于某个字符或某个子字符串的格式判断等等)、类的交互、函数的创建与使用以及正则表达式的运用等等。题量不大,除却第一次有9个题以外,第二次有四题并且菜单题开始出现,第三次作业有七题附有一道非常难得菜单题;关于难度方面,第一次作业的9个题难度平均较低,属于基础知识点的运用考察,第二次则开始提高难度,且幅度略大,第三次在第二次的基础上继续提高了难度以及复杂性,其主要提高的是复杂性和繁琐度,致使总体解题难度上升幅度较大。

 

(2)设计与分析

 

1、第一次作业第一题:

 

 

 

 

此题目只需要进行一次简单的计算,根据输入的数据算出相应的BMI,并进行判断,根据相关范围给出相应输出即可。总体难度简单。

 

import java.util.Scanner;public class Main{

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        double W = input.nextDouble();

        double H = input.nextDouble();

        if(W<=0 || W>727 || H<=0 || H>2.72)

        {

            System.out.println("input out of range");

            return;

        }

        double temp = W/(H*H);

        if(temp<18.5)

            System.out.println("thin");

        if(temp>=18.5 && temp<24)

            System.out.println("fit");

        if(temp>=24 && temp<28)

            System.out.println("overweight");

        if(temp>=28)

            System.out.println("fat");

    }

}

 

 

以上代码能通过十个测试点中的八个,在数值边界区输入会出现答案误差的情况

 

 

 

2、第一次作业第二题

 

 

 

 

 

此题目也只是两个很简单的换算计算。总体难度简单。

 

import java.util.Scanner;public class Main{

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        double W = input.nextDouble();

        double H = input.nextDouble();

        double Wt = 0.45359237;

        double Ht = 0.0254;

        double We = 0.0;

        double He = 0.0;

        We = W/Wt;

        He = H/Ht;

        System.out.println((float)We + " " + (float)He);

    }

}

 

 

 无bug。

 

3、第一次作业第三题

 

 

 

 

 

此题目需要判断一个数列中每个元素的奇偶性,并将奇数加起来,输出奇数的总和,需要一个简单的if语句进行基础判断和for语句进行循环判断并求和。总体难度简单。

 

import java.util.Scanner;public class Main{

    public static void main(String[] args){

        int Wt = 0;

        Scanner input = new Scanner(System.in);

        for(int i=0; i<10; i++){

            int W = input.nextInt();

            if(W%2 != 0)

            Wt = Wt+W;

        }

        System.out.println(Wt);

    }

}

 

无bug。

 

4、第一次作业第四题

 

 

 

此题目需要定义多个变量名和多个数据的输入,并对每个数据进行相应的计算和判断,且部分数据在一次计算后需判断再进行二次计算。逻辑简单,只是数据较多略显繁复。总体难度较简单。

 

import java.util.Scanner;public class Main{

    public static void main(String[] args){

        int Wt = 0;

        Scanner input = new Scanner(System.in);

        int num = input.nextInt();

        int fang = input.nextInt();

        int ping = input.nextInt();

        double mian = input.nextDouble();

        double qi=0.0, ying=0.0, jiao=0.0, che=0.0;

        if(num == 1)

        {

            if(mian <= 90)

            {

                qi = 0.01 * ping * 10000;

            }

            if(mian > 90 && mian <= 144)

            {

                qi = 0.015 * ping * 10000;

            }

            if(mian > 144)

            {

                qi = 0.03 * ping * 10000;

            }

            ying = 0.0005 * fang * 10000;

            jiao = 3 * mian;

            che = 1.36 * mian;

        }

        else

        {

            qi = 0.03 * ping * 10000;

            ying = 0.0005 * fang * 10000;

            jiao = 3 * mian;

            che = 1.36 * mian;

        }

        System.out.println((float)qi+" "+(float)ying+" "+(float)jiao+" "+(float)che);

    }

}

 

 

无bug。

 

 

5、第一次作业第五题

 

 

 

 

此题目需对两个数据先后进行判断,并给出相应的输出并组合起来,且需要对于输入格式进行判断,当输入有误时需输出提醒“wrong format”。逻辑和语句都较简单。总体难度简单。

 

import java.util.Scanner;public class Main{

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        int zhongZu = input.nextInt();

        int zhiYe = input.nextInt();

        if(zhongZu == 1){

            if(zhiYe == 1)

            System.out.print("人类"+" "+"战士");

        else if(zhiYe == 2)

            System.out.print("人类"+" "+"法师");

        else if(zhiYe == 3)

            System.out.print("人类"+" "+"射手");

        else{

            System.out.print("Wrong Format");

            return;

        }

        }

        else if(zhongZu == 2){

            if(zhiYe == 1)

            System.out.print("精灵"+" "+"战士");

        else if(zhiYe == 2)

            System.out.print("精灵"+" "+"法师");

        else if(zhiYe == 3)

            System.out.print("精灵"+" "+"射手");

        else{

            System.out.print("Wrong Format");

            return;

        }

        }

        else if(zhongZu == 3){

            if(zhiYe == 1)

            System.out.print("兽人"+" "+"战士");

        else if(zhiYe == 2)

            System.out.print("兽人"+" "+"法师");

        else if(zhiYe == 3)

            System.out.print("兽人"+" "+"射手");

        else{

            System.out.print("Wrong Format");

            return;

        }

        }

        else if(zhongZu == 4){

            if(zhiYe == 1)

                System.out.print("暗精灵"+" "+"战士");

            else if(zhiYe == 2)

                System.out.print("暗精灵"+" "+"法师");

            else if(zhiYe == 3)

                System.out.print("暗精灵"+" "+"射手");

            else{

                System.out.print("Wrong Format");

                return;

            }

        }

        else{

            System.out.print("Wrong Format");

            return;

        }

    }

}

 

无bug。

 

6、第一次作业第六题

 

 

此题目需要用到字符串的提取,以及对于某个字符或子字符串的判断,并根据判断结果给出相应的输出;输入格式错误时需输出提醒“wrong format”。总体逻辑清晰,进行子字符串的提取和内容判断略显繁复,总体难度一般。

 

import java.util.*;import java.text.*;public class Main{

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        String[] nums = null;

        nums = input.nextLine().split("");

        int[] bianHao = new int[nums.length];

        if(nums.length != 8)

        {

            System.out.println("Wrong Format");

            return;

        }

        for(int i=0; i<nums.length; i++)

        {

            bianHao[i] = Integer.valueOf(nums[i]);

        }

        int nianFen=0, xueYuan=0, banJi=0, xueHao=0;

        nianFen = 2000 + bianHao[0]*10 + bianHao[1];

        xueYuan = bianHao[2]*10 + bianHao[3];

        banJi = bianHao[4]*10 + bianHao[5];

        xueHao = bianHao[6]*10 + bianHao[7];

        if(xueYuan != 1 && xueYuan != 3 && xueYuan != 2 && xueYuan != 20)

        {

            System.out.println("Wrong Format");

            return;

        }

        else

        {

            System.out.println("入学年份:"+nianFen+"年");

            if(xueYuan == 1)

                System.out.println("学院:材料学院");

            if(xueYuan == 2)

                System.out.println("学院:机械学院");

            if(xueYuan == 3)

                System.out.println("学院:外语学院");

            if(xueYuan == 20)

                System.out.println("学院:软件学院");//             NumberFormat nf_banji = new DecimalFormat("00");//             NumberFormat nf_xuehao = new DecimalFormat("00");//             String str_banji = nf_banji.format(banji);//             String str_xuehao = nf_xuehao.format(xuehao);

            System.out.println("班级:" + String.format("%02d", banJi));

            System.out.println("学号:" + String.format("%02d", xueHao));

        }

    }

}

 

 

无bug。

 

7、第一次作业第七题

 

 

 

此题目需要运用for循环语句对于所给公式进行反复计算,并在满足给定条件后输出结果。总体难度简单

 

 

import java.util.*;public class Main{

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        float next = input.nextFloat();

        float last = input.nextFloat();

        if(next < 0 || last <= 0)

        {

            System.out.println("Wrong Format");

            return;

        }

        if(next == last)

        {

            last = next+1;

        }

        float nextGuess=0, lastGuess=last, n=next;

        while(true)

        {

            nextGuess = (lastGuess + n/lastGuess)/2;

            if(nextGuess>=lastGuess && nextGuess-lastGuess < 0.00001)

            {

                System.out.println(lastGuess);

                break;

            }

            if(nextGuess<=lastGuess && lastGuess-nextGuess < 0.00001)

            {

                System.out.println(lastGuess);

                break;

            }

            lastGuess = nextGuess;

        }

    }

}

 

 

无bug。

 

 

8、第一次作业第八题

 

 

 

 

此题目需要用到字符串的子字符串提取,以及子字符串内容的判断。需将输入字符串中的1和0筛选出来并总体输出。逻辑较简单,语句简单。总体难度一般。

 

import java.util.*;public class Main{

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        String str = input.nextLine();

        int isprime=0;

        for(int i=0; i<str.length(); i++)

        {

            if(str.charAt(i) == '-' && str.charAt(i+1) == '1')

            {

                isprime = 1;

            }

        }

        if(isprime == 0)

        {

            System.out.print("Wrong Format");

            return;

        }

        int i=0;

        while(true)

        {

            if(str.charAt(i) == '-' && str.charAt(i+1) == '1')

            {

                return;

            }

            if(str.charAt(i) == '1')

            {

                System.out.print(1);

            }

            if(str.charAt(i) == '0')

            {

                System.out.print(0);

            }

            i++;

        }

        

    }

}

 

 

无bug。

 

 

9、第一次作业第九题

 

 

 

 

此题目需要进行多重判断,需使用多个if和else语句嵌套,对三个输入数据进行判断,输出相应的三角形类型。逻辑较复杂,语句较繁复。总体难度一般偏难。

 

import java.util.Scanner;public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        double a=in.nextDouble();

        double b=in.nextDouble();

        double c=in.nextDouble();

        if(a>=1&&a<=200&&b>=1&&b<=200&&c>=1&&c<=200)

        {

            if(a+b>c&&a+c>b&&b+c>a)

            {

                if(a==b&&b==c)

                    System.out.println("Equilateral triangle");

                else

                {

                    if((a==b)||(b==c)|(a==c))

                    {

                        if((a*a+b*b-c*c<0.1)||(a*a+c*c-b*b<0.1)||(b*b+c*c-a*a<0.1))//不要用a*a+b*b==c*c

                            System.out.println("Isosceles right-angled triangle");

                        else

                            System.out.println("Isosceles triangle");

                    }

                    else

                    {

                        if((a*a+b*b-c*c<0.1)||(a*a+c*c-b*b<0.1)||(b*b+c*c-a*a<0.1))

                            System.out.println("Right-angled triangle");

                        else

                            System.out.println("General triangle");

                    }

                }

            }

            else

                System.out.println("Not a triangle");

        }

        else

            System.out.println("Wrong Format");

    }

}

 

无bug。

 

10、第二次作业第一题

 

 

 

 

  此题可以说打开我们对于Java面向对象编程的认识的大门,题目给到的所有类,对象各施其职完成了对点菜这一任务的解答。同时在输入,输出方面同时也有不小的难度,需要再输入获取信息中下功夫,对于字符串的理解也加深了。总的来说这相对还是简单的,只是与之前的编程思想有了很大的不同。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        //类名称 对象数组名[] = new 类名称[长度];

        Dish d[] = new  Dish[2];

        d[0] = new Dish("麻婆豆腐",12);

        d[1] = new Dish("油淋生菜",9);//error:                                        d[0].name="麻婆豆腐";d[0].unit_price=12;//                                              d[1].name="油淋生菜";d[1].unit_price=9;//text:                                         System.out.println(d[1].name+" "+d[1].unit_price);

        Scanner scanner  = new Scanner(System.in);

        String useless =  scanner.nextLine();

        String useless1 =  scanner.nextLine();

        String a =  scanner.nextLine();

        int i = 0;

        int s = 0;

        Order o[] = new Order[1000];

        int p1 = 0;//         Order p1 = new Order();

        int p = 0;

        while(a.indexOf("end")==-1)

        {

            if(a.indexOf("delete")!=-1)

            {

                int x = Integer.valueOf(a.substring(0,1));

                if(x > i)

                System.out.println("delete error;");

                else

                s = s - o[x-1].price;

            }

            else

            {

                String[] result = a.split(" ",4) ;

                int j;

                for(j=0;j<2;j++)

                {

                    if(a.indexOf(d[j].name)!=-1)

                    {

                        p = d[j].unit_price;break;

                    }

                }

                if(j==2)

                    System.out.println(result[1]+" does not exist");

                else

                {

                    int n = Integer.valueOf(result[2]);

                    int m = Integer.valueOf(result[3]);

                    if(n==1)

                        p1 = p*m;

                    if(n==2)

                    {

                        double loss = p*1.5*m + 0.5;

                        p1 =(int) loss;

                    }

                    if(n==3)

                        p1 = p*2*m;

                    o[i] = new Order(result[1],p1);

                    s  = s + p1;

                    i++;

                    System.out.println(i+" "+result[1]+" "+p1);

                }

            }

            a =  scanner.nextLine();

        }

        System.out.print(s);

        return;

    }

}class Dish

{

    String name;

    int unit_price;

    public Dish(){

    }

    public Dish(String name,int unit_price)

    {

        this.name = name;

        this.unit_price = unit_price;

    }

}class Order

{

    String name;

    int price;//     public Order{//     }

    public Order(String name,int price)

    {

        this.name = name;

        this.price = price;

    }

}

 

无bug。

 

11、第二次作业第二题

 

 

 

输入输出样例不给出。

 

此题的复杂成都相较于之前的题目直线上升,需要许多的类相互配合合作,才能完成复杂的要求和各种各样的点菜情况。输入输出的难度很大,逻辑性一般但对于初学者的语法考验很大,无法很好的完成,因此没有得到满分。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        //类名称 对象数组名[] = new 类名称[长度];

        Dish d[] = new  Dish[2];

        d[0] = new Dish("麻婆豆腐",12);

        d[1] = new Dish("油淋生菜",9);//error:                                        d[0].name="麻婆豆腐";d[0].unit_price=12;//                                              d[1].name="油淋生菜";d[1].unit_price=9;//text:                                         System.out.println(d[1].name+" "+d[1].unit_price);

        Scanner scanner  = new Scanner(System.in);

        String useless =  scanner.nextLine();

        String useless1 =  scanner.nextLine();

        String a =  scanner.nextLine();

        int i = 0;

        int s = 0;

        Order o[] = new Order[1000];

        int p1 = 0;//         Order p1 = new Order();

        int p = 0;

        while(a.indexOf("end")==-1)

        {

            if(a.indexOf("delete")!=-1)

            {

                int x = Integer.valueOf(a.substring(0,1));

                if(x > i)

                System.out.println("delete error;");

                else

                s = s - o[x-1].price;

            }

            else

            {

                String[] result = a.split(" ",4) ;

                int j;

                for(j=0;j<2;j++)

                {

                    if(a.indexOf(d[j].name)!=-1)

                    {

                        p = d[j].unit_price;break;

                    }

                }

                if(j==2)

                    System.out.println(result[1]+" does not exist");

                else

                {

                    int n = Integer.valueOf(result[2]);

                    int m = Integer.valueOf(result[3]);

                    if(n==1)

                        p1 = p*m;

                    if(n==2)

                    {

                        double loss = p*1.5*m + 0.5;

                        p1 =(int) loss;

                    }

                    if(n==3)

                        p1 = p*2*m;

                    o[i] = new Order(result[1],p1);

                    s  = s + p1;

                    i++;

                    System.out.println(i+" "+result[1]+" "+p1);

                }

            }

            a =  scanner.nextLine();

        }

        System.out.print(s);

        return;

    }

}class Dish

{

    String name;

    int unit_price;

    public Dish(){

    }

    public Dish(String name,int unit_price)

    {

        this.name = name;

        this.unit_price = unit_price;

    }

}class Order

{

    String name;

    int price;//     public Order{//     }

    public Order(String name,int price)

    {

        this.name = name;

        this.price = price;

    }

}

 

这些点无法很好的达到题目的要求。

 

12、第二次作业第三题

 

 

该题相较于点菜题比较简单,输入输出都不复杂,只是对于细节的把控要求较高。

 

import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner scanner  = new Scanner(System.in);

        String a =  scanner.nextLine();

        if( a.length()!=10)

        System.out.println(a+"无效!");

        else{

        int y = Integer.valueOf(a.substring(0,4));

        int m = Integer.valueOf(a.substring(5,7));

        int d = Integer.valueOf(a.substring(8,10));

        if(judge(y,m,d)==0)

            System.out.println(a+"无效!");

        else

        {   if(LeapYear(y) == 1)

            System.out.println(a+"是闰年.");

            Prt(y,m,d,a);

        }

        }

        String an = scanner.nextLine();

        if(an.length()!=21)

            //System.out.println(an+"无效!");        {

           int p = an.indexOf(" ");

           int p1 = an.length();

           String a1 =  an.substring(0,p);

           String a2 =  an.substring(p+1,p1);

           System.out.println(a1+"或"+a2+"中有不合法的日期.");

        }

        else

        {

            int y = Integer.valueOf(an.substring(0,4));

            int m = Integer.valueOf(an.substring(5,7));

            int d = Integer.valueOf(an.substring(8,10));

            int y1 = Integer.valueOf(an.substring(11,15));

            int m1 = Integer.valueOf(an.substring(16,18));

            int d1 = Integer.valueOf(an.substring(19,21));

            String a1 =  an.substring(0,10);

            String a2 =  an.substring(11,21);

            if(judge(y,m,d)==0||judge(y1,m1,d1)==0)

            {   System.out.println(a1+"或"+a2+"中有不合法的日期.");

                return ;}

            if(compare(y,m,d,y1,m1,d1)==0)

                System.out.println(a2+"早于"+a1+",不合法!");

            if(compare(y,m,d,y1,m1,d1)==1)

            {

                int ca = Prt1(y,m,d);

                int b = Prt1(y1,m1,d1);

                int s = 0;

                for(int j = y;j < y1 ;j++)

                {

                    if(LeapYear(j)==1)

                        s = s +366;

                    else

                        s = s +365;

                }

                s = s+b-ca;

                int cy = y1-y;

                int cm = m1-m;

                System.out.println(a2+"与"+a1+"之间相差"+s+"天,所在月份相差"+cm+",所在年份相差"+cy+".");

            }

        }

    }public static int compare(int year,int month,int day,int year1,int month1,int day1)

{

    int a = 1;

    if(year > year1)

        return 0;

    if(year == year1 && month > month1)

        return 0;

    if(year == year1 && month == month1 && day > day1)

        return 0;

    return a;

}public static int judge(int year , int month , int day)

{

    if(month == 1 && day >31)

        return 0;

    if(month == 3 && day >31)

        return 0;

    if(month == 4 && day >=31)

        return 0;

    if(month == 5 && day >31)

        return 0;

    if(month == 6 && day >=31)

        return 0;

    if(month == 7 && day >31)

        return 0;

    if(month == 8 && day >31)

        return 0;

    if(month == 9 && day >=31)

        return 0;

    if(month == 10 && day >31)

        return 0;

    if(month == 11 && day >=31)

        return 0;

    if(month == 12 && day >31)

        return 0;

    if(LeapYear(year)==1 && month == 2 && day >29)

        return 0;

    if(LeapYear(year)==0 && month == 2 && day >=29)

        return 0;

    return 1;

}public static int LeapYear(int year)

{

    if(year%4==0 && year%100!=0)

        return 1;

    if(year%400==0)

        return 1;

    return 0;

}public static void Prt(int year , int month , int day ,String s)

{

    int[] x = new int[12];

     x[0] = 31;

     x[1] = 28;x[2] = 31;x[3] = 30;x[4] = 31;x[5] = 30;x[6] = 31;x[7] = 31;x[8] = 30;

     x[9] = 31;x[10] = 30;x[11] = 31;

    if(LeapYear(year)==1)

        x[1] = 29;

    int s1 = 0;

    for(int i=0;i < month - 1 ;i++)

        s1 = s1+x[i];

    s1 = s1 + day;

    int c ;

    //c = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D

    c = (year-1 + (year-1)/4 - (year-1)/100 + (year-1)/400 + s1)%7;

     //Y是年份数,D是这一天在这一年中的累积天数,也就是这一天在这一年中是第几天。

    if ( c == 0)

        c = 7;

    System.out.println(s+"是当年第"+s1+"天,当月第"+day+"天,当周第"+c+"天.");

}public static int Prt1(int year , int month , int day)

{

    int[] x = new int[12];

     x[0] = 31;

     x[1] = 28;x[2] = 31;x[3] = 30;x[4] = 31;x[5] = 30;x[6] = 31;x[7] = 31;x[8] = 30;

     x[9] = 31;x[10] = 30;x[11] = 31;

    if(LeapYear(year)==1)

        x[1] = 29;

    int s1 = 0;

    for(int i=0;i < month - 1 ;i++)

        s1 = s1+x[i];

    s1 = s1 + day;

    return s1;

}

}

 

无bug。

 

13、第二次作业第四题

 

本题在语法上没有难度,重在考查我们的算法知识,难度不大。

 

import java.io.*;import java.util.*;

 public class Main {

    public static void main(String args[]) throws IOException{

        StreamTokenizer re = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        re.nextToken();

        int T = (int)re.nval;

        int a[] = new int[10010];

        int b[] = new int[100];

        int i,j,maxx = 0;

        for(i=0;i<T;i++)

        {

            re.nextToken();

            a[i] = (int)re.nval;

            if(maxx < a[i]) maxx = a[i];

        }

        b[0] = 1;b[1] = 1;b[2] = 2;b[3] = 4;b[4] = 8;

        for(i=5;i<=maxx;i++) b[i] = 2 * b[i - 1] - b[i - 5];

        for(i=0;i<T;i++)

            System.out.println(b[a[i]]);

    }

}

 

无bug。

 

13、第三次作业第一题

 

 

 

 

 

 

 

这一题相当的复杂光看题干就能理解这是一道多么复杂的题目,题目的类非常的繁多,要做到有条理的将它们组合到一起十分的有难度。

 

import javax.management.StandardMBean;import java.util.Scanner;import java.time.LocalDate;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class Main {

    public static void main(String args[]) {//        准备菜单

        Menu menu=new Menu();

        Dish d;

        Scanner in=new Scanner(System.in);

        String s1;

        Order[] order=new Order[100];

        String name;

        int tn;

        String date1,date2;

        int portion;

        int num;

        s1=in.next();

        while(!s1.equals("end"))

        {

            if(!s1.equals("table")&&(s1.charAt(0)<'0'||s1.charAt(0)>'9'))

            {

                int price;

                price=in.nextInt();

                d=new Dish(s1,price);

                menu.add(d);

                s1=in.next();

            }

            if(s1.equals("table"))

            {

                tn=in.nextInt();

                date1=in.next();

                date2=in.next();

                order[tn]=new Order(menu);

                //String s2;

                s1=in.next();

                while(s1.charAt(0)>='1'&&s1.charAt(0)<='9')

                {

                    int orderNum=Integer.parseInt(s1.substring(0,s1.length()));

                    name=in.next();

                    if(name.equals("delete"))

                    {

                        order[tn].delARecordByOrderNum(orderNum);

                        s1=in.next();

                    }

                    else

                    {

                        portion=in.nextInt();

                        num=in.nextInt();

                        order[tn].addARecord(tn,date1,date2,orderNum,name,portion,num);

                        s1=in.next();

                    }

                }

            }

        }

       order[1].getTotalPrice();

    }

}

//菜品类:对应菜谱上一道菜的信息。class Dish {

    String name;// 菜品名称

    float unit_price; // 单价

    Dish(String name,float price)

    {

        this.name=name;

        this.unit_price=price;

    }

    float getPrice(int portion)// 计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)    {

        float bl[]= {1,1.5f,2};

        return unit_price*bl[portion-1];

    }

}

//菜谱类:对应菜谱,包含饭店提供的所有菜的信息。class Menu {

    public Dish[] dishs=new Dish[1000000];// 菜品数组,保存所有菜品信息

    static int cnt=0;

    public void add(Dish dish)//菜单加一道菜    {

        dishs[cnt++]=dish;

    }

    Dish searthDish(String dishName)// 根据菜名在菜谱中查找菜品信息,返回Dish对象。    {

        Dish find=null;

        for(int i=cnt-1;i>=0;i--)

        {

            if(dishName.equals(dishs[i].name))

            {

                find=dishs[i];//返回最新的

                break;

            }

        }

        return find;

    }

}

//点菜记录类:保存订单上的一道菜品记录class Record {

    int orderNum;

    Dish d;// 菜品

    int portion;// 份额(1/2/3代表小/中/大份)

    int num;

    int f=0;

    int tn;

    String date1,date2;

    int week;

    int time=0;

    public Record(int tn,String date1,String date2,int orderNum,Dish d,int portion,int num) throws ParseException {

        this.tn=tn;

        this.date1=date1;

        this.date2=date2;

        this.orderNum=orderNum;

        this.d=d;

        this.portion=portion;

        this.num=num;

        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

        calendar.setTime(sdf.parse(date1));

        int i=calendar.get(Calendar.DAY_OF_WEEK);

        if(i==0)

            this.week=7;

        else

            this.week=i-1;

        String[] dt2=date2.split("[/]");

        int h1=Integer.parseInt( dt2[0]);

        int m1=Integer.parseInt( dt2[1]);

        int s1=Integer.parseInt( dt2[2]);

        this.time=h1*3600+m1*60+s1;

    }

    float getPrice()// 计价,计算本条记录的价格    {

            return ((int)(d.getPrice(portion)+0.5))*num;

    }

}

//订单类:保存用户点的所有菜的信息。class Order {

    Record[] records=new Record[1000000];// 保存订单上每一道的记录    Menu menu;

    static int cnt2=0;

    void getTotalPrice()// 计算订单的总价    {

        boolean f1=true;

        double k=1.0;

        int week=records[0].week;

        int time=records[0].time;

        if(week==7)

        {

            k=1.0;

            if(time<10*3600+30*60||time>14*3600+30*60)

            {

                f1=false;

            }

        }

        else

        {

            if(time>=17*3600&&time<=20*3600+30*60)

            {

                k=0.8;

            }

            else if(time>=10*3600+30*60&&time<=14*3600+30*60)

            {

                k=0.6;

            }

            else

                f1=false;

        }

        float total=0;

        for(int i=0;i<cnt2&&f1;i++)

        {

            if(records[i].f==0)

                total+=records[i].getPrice();

        }

        if(f1) System.out.println("table "+records[0].tn+": "+(int)(total*k+0.5));

        else System.out.println("table "+records[0].tn+" out of opening hours");

    }

    public Order(Menu menu)

    {

        this.menu=menu;

    }

    //根据菜名点菜

    Record addARecord(int tn,String date1,String date2,int orderNum,String dishName, int portion,int num)

    {

        Record r= null;

        try {

            r = new Record(tn,date1,date2,orderNum,menu.searthDish(dishName),portion,num);

        } catch (ParseException e) {

            throw new RuntimeException(e);

        }

        if(r.d==null)

        {

            System.out.println(dishName+" does not exist");

        }

        else

        {

            records[cnt2++]=r;

            if(cnt2==1){

                System.out.println("table "+records[0].tn+": ");

                System.out.println(r.orderNum+" "+r.d.name+" "+(int)(r.getPrice()+0.5));

            }

            else

                System.out.println(r.orderNum+" "+r.d.name+" "+(int)(r.getPrice()+0.5));

        }

        return records[cnt2];

    }

    void delARecordByOrderNum(int orderNum)//根据序号删除一条记录    {

        int i=0;

        for(i=0;i<cnt2;i++)

        {

            if(records[i].orderNum==orderNum)

            {

                //if(records[i].f==1)

                //{

                //    System.out.println("delete error;");

                // }

                records[i].f=1;

                return ;

            }

        }

        if(i==cnt2)

            System.out.println("delete error;");

    }

}

 

 

 

我对于本题时段的不同有不同的价格方面没有做好,对于带点菜的处理也很差很多地方的测试点都无法通过。

 

13、第三次作业第二题

 

 

本题更多的是对于java库函数的考察,要求我们对于Set方法 Hash算法有一定的了解,并且能够正确的运用到题目中,这会大大提高我们的编程效率和程序的简洁性,可读性。

 

import java.util.*;

public class Main {

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        int n= sc.nextInt();

        sc.nextLine();

        String[] strings=sc.nextLine().split(" ");

        sc.close();

        Set<String> set=new HashSet<>();

        for (int i = 0; i < n; i++) {

            set.add(strings[i]);

        }

        if (set.size()==n) System.out.println("NO");

        else System.out.println("YES");

    }

}

 

无bug。

 

13、第三次作业第三题

 

 

 

本体对于知识点的考查方向与上一题相似,题目在利用了Java库函数时难度较小。

 

import java.util.*;public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int f = sc.nextInt();

        int flag=0;

        String str = sc.nextLine();

        while(sc.hasNext())

        {

            String s = sc.nextLine();

            str = str + s;

        }

        String[] arr = str.split(" ");

        LinkedHashSet<String> list = new LinkedHashSet<String>();

        for (int i = 0; i < arr.length; i++) {

            list.add(arr[i]);

        }

        for (String w : list) {

                if(flag==0)

                {

                    System.out.print(w);

                    flag = 1;

                }

                else System.out.print(" "+w);

        }

    }

}

 

无bug。

 

13、第三次作业第四题

 

本体的考察主要针对排序这一知识点,但是在掌握了Java的众多库函数后这个题目相较而言比较简单,编程上没有什么难点,语法也没有什么难点。

 

import java.util.*;

public class Main {

 

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        Scanner scanner = new Scanner(System.in);

        String str = scanner.nextLine().replaceAll("[,.]", "");

        String[] strr = str.split(" ");

        TreeMap<String, Integer> tMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

 

        for (String s : strr) {

            tMap.put(s, s.length());//            System.out.println(s);        }

 

        List<Map.Entry<String, Integer>> tList = new ArrayList<>(tMap.entrySet());

        tList.sort((o1, o2) -> -(o1.getValue() - o2.getValue()));

        

        Iterator<Map.Entry<String, Integer>> iter = tList.iterator();

        for(int i=0;i<tList.size();i++) {

            Map.Entry<String, Integer> entry = iter.next();

            System.out.println(entry.getKey());

        }

    }

}

 

无bug。

 

13、第三次作业第五题

 

 

 

本题是针对学生对于this 和 字符串的处理的考查,难度较小,也没有逻辑和语法上的难度,是中规中矩的一道题。

 

import java.util.*;

public class Main{

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        //调用无参构造方法,并通过setter方法进行设值

        String sid1 = sc.next();

        String name1 = sc.next();

        int age1 = sc.nextInt();

        String major1 = sc.next();

        Student student1 = new Student();

        student1.setSid(sid1);

        student1.setName(name1);

        student1.setAge(age1);

        student1.setMajor(major1);

        //调用有参构造方法

        String sid2 = sc.next();

        String name2 = sc.next();

        int age2 = sc.nextInt();

        String major2 = sc.next();

        Student student2 = new Student(sid2, name2, age2, major2);

        //对学生student1和学生student2进行输出        student1.print();

        student2.print();

    }

}

/* 请在这里填写答案 */

class Student{

    String sid;

    String name;

    int age;

    String major;

    public Student() {

        

    }

    public Student(String sid, String name, int age, String major) {

        this.sid = sid;

        this.name = name;

        this.major = major;

        this.age = age;

    }

    public void print() {

        System.out.println("学号:"+sid+",姓名:"+name+",年龄:"+age+",专业:"+major);

    }

    public void setSid(String sid1){

        this.sid = sid1;

    }

    public void setMajor(String major1) {

        this.major = major1;

    }

    public void setAge(int age1) {

        this.age = age1;

    }

    public void setName(String name1) {

        this.name = name1;

    }

}

 

无bug。

 

13、第三次作业第六题

 

 

本题是对于浮点数处理的考查,没有什么难度。

 

import java.util.*;

public class Main {

    public static void main(String[] args)

    {

        int Du;

        int Fen;

        float Miao;

        Scanner input = new Scanner(System.in);

        Du = input.nextInt();

        Fen = input.nextInt();

        Miao = input.nextFloat();

        double Ans = Du*1.0 + Fen/60.0 + Miao/3600;

        System.out.print(Du+"°"+Fen+"′"+Miao+"″ = ");

        System.out.printf("%.6f",Ans);

    }

}

 

无bug。 

 

13、第三次作业第七题

 

 

 

本题是让学生对于nextLine, split, parsenlt, of, isAfter, isBefore, until等方法的集中考查很好的训练了我们对于Java这一编程语言的熟练度,让我们更好的认识了java语言的强大功能性,便利性,本题还进一步加深了我们在作业二第三题中训练过的对于日期的处理方式。

 

import java.util.*;

public class Main{

    public static void main(String[] args)

    {

        Scanner input = new Scanner(System.in);

        String[] Date1 = input.nextLine().split("-");

        String[] Date2 = input.nextLine().split("-");

        int[] Array1 = new int[Date1.length];

        int[] Array2 = new int[Date1.length];

        int isprime = 1;

        for(int i=0; i<Date1.length; i++)

        {

            // Character ch=Date1.charAt(i);//将字符串的每一个字符存到Character中

            Array1[i]=Integer.parseInt(Date1[i]);

            Array2[i]=Integer.parseInt(Date2[i]);

        }

        if(Array1[0] < Array2[0])

        {

            isprime = 0;

            System.out.println("第一个日期比第二个日期更早");

        }

        if(Array1[0] > Array2[0])

        {

            System.out.println("第一个日期比第二个日期更晚");

        }

        if(Array1[0] == Array2[0])

        {

            if(Array1[1] < Array2[1])

            {

                isprime = 0;

                System.out.println("第一个日期比第二个日期更早");

            }

            if(Array1[1] > Array2[1])

            {

                System.out.println("第一个日期比第二个日期更晚");

            }

            if(Array1[1] == Array2[1])

            {

                if(Array1[2] < Array2[2])

                {

                    isprime = 0;

                    System.out.println("第一个日期比第二个日期更早");

                }

                if(Array1[2] > Array2[2])

                {

                    System.out.println("第一个日期比第二个日期更晚");

                }

            }

        }

        int[] rDate = {31,29,31,30,31,30,31,31,30,31,30,31};

        int[] pDate = {31,28,31,30,31,30,31,31,30,31,30,31};

        int ZaoNian;

        int ZaoYue;

        int ZaoRe;

        int WanNian;

        int WanYue;

        int WanRe;

        if(isprime == 0)

        {

            ZaoNian = Array1[0];

            ZaoYue = Array1[1];

            ZaoRe = Array1[2];

            WanNian = Array2[0];

            WanYue = Array2[1];

            WanRe = Array2[2];

        }

        else

        {

            ZaoNian = Array2[0];

            ZaoYue = Array2[1];

            ZaoRe = Array2[2];

            WanNian = Array1[0];

            WanYue = Array1[1];

            WanRe = Array1[2];

        }

        int cot=0;

        for(int i=ZaoNian; i<WanNian; i++)

        {

            if(i%4 == 0 && i%100 != 0 || i%400 == 0)

            {

                cot += 366;

            }

            else

            {

                cot += 365;

            }

        }

        int yearCot1=0;

        int yearCot2=0;

        if(ZaoNian%4 == 0 && ZaoNian%100 != 0 || ZaoNian%400 == 0)

        {

            for(int i=0; i<ZaoYue-1; i++)

            {

                yearCot1 += rDate[i];

            }

            yearCot1 += ZaoRe;

        }

        else

        {

            for(int i=0; i<ZaoYue-1; i++)

            {

                yearCot1 += pDate[i];

            }

            yearCot1 += ZaoRe;

        }

        if(WanNian%4 == 0 && WanNian%100 != 0 || WanNian%400 == 0)

        {

            for(int i=0; i<WanYue-1; i++)

            {

                yearCot2 += rDate[i];

            }

            yearCot2 += WanRe;

        }

        else

        {

            for(int i=0; i<WanYue-1; i++)

            {

                yearCot2 += pDate[i];

            }

            yearCot2 += WanRe;

        }

        cot = cot + yearCot2 - yearCot1;

        System.out.println("两个日期间隔"+cot+"天");

        int week;

        week = cot/7;

        System.out.println("两个日期间隔"+week+"周");

    }

}

 

 无bug。

 

(3)踩坑心得

1、在第一次大作业中其实各方面的知识运用都还是比较基础的考察,各题目也不需要很多面向对象的设计思维,面向过程的编程思维便可解决。除却部分边界值考察时会出现bug以外,基本都能通过全部测试点。总体难度也是偏低,属于刚开始的入门题目。大部分题目在经过两三次修改后便可排除第一次编写的所有bug。

2、第二次大作业的难度显著上升,主要体现在第二题中通信协议的数据判断。其中需要用到大量的字符和子字符串提取、某一字符下标判断以及字符内容判断。其中要使用charAt、substring、equals、indexOf等字符串操作,如

1

int t = data.indexOf('0')

  

1

if(data.charAt(i)==0)

  

1

if(data.charAt(j)==1)

  

1

data.substring(count1,count1+12)

  

在题目要求的各个格式判断中例如起始位、结束位以及奇偶校验位,语句较为复杂,容易出现很多bug,且可能出现循环一直不终止的情况而导致运行超时。

3、第三次大作业第一个题目的难度较高,其中各种判断非常复杂,有很多非法输入需要判断排除,且有很多判断是相互嵌套的,这就导致在编写题目的时候的逻辑非常复杂,稍有不慎就会出现很多bug,而且题目需使用多个类的设计和子函数实现功能,互相之间调用时有时会产生混淆而导致错误。第三次大作业的第一个题与之前的点菜题有很多联系,这就会在各种耦合时大幅提高复杂性。其实总的来说,主要是各种输入格式的判断非常复杂,有非常多类型的非法输入,这就需要编写很多判断语句,大幅提高了逻辑复杂性和语句繁复度。

 

(4)总结

截至目前的三次作业,难度逐渐提高,复杂性也愈来愈高,对于面向对象程序设计思维要求也愈发增大。不过在三次作业之后,我在第一次作业中初步了解了Java的基础语法例如

1

import java.util.Scanner;//类似于C语言头文件

  

1

Scanner input = new Scanner(System.in);//输入

  

1

2

3

System.out.print();

System.out.println();

System.out.printf();//三种输出语句

  以及

1

2

3

int i = input.nextInt();

double j = input.nextDouble();

String s = input.nextLine();

  等

  定义变量和从控制台赋值的语句。

在第二次作业中学到了很多对于字符串的操作的函数例如

1

2

3

4

String s;

s.charAt();

s.substring();

s.indexOf();

  等

  在第三次作业中学到了类的设计、子函数的运用、部分正则表达式的运用以及更复杂的字符串操作运用。

我在之后的学习中需要更加注意培养面向对象程序设计思维,提高逻辑思维能力,避免出现看见一大堆题目要求时茫然且不知如何下手,或者编写很多语句相互嵌套之后出现bug而无法发现错误的情况。

 

 

 

标签:总结,java,String,int,System,实验,println,public,out
From: https://www.cnblogs.com/Misaka20001/p/17428074.html

相关文章

  • PTA题目集1~3的总结性Blog
    一、前言:我通过学习面向对象程序设计这门课程,深入掌握了Java语言的知识。截至目前,三个PTA作业,这些作业主要涉及Java的结构、类的运用、以及一些方法的使用,其中类的应用是重点。这三次作业的难度逐渐加大,同时作业量也在逐步增加。最令我印象深刻的是点菜,每一次都让我心如焦土,无可......
  • 让java目录能导出.xml配置文件
    在maven中配置<!--插件配置--><build><resources><resource><directory>src/main/java</directory><!--包含了src/main/java目录下的所有xml资源配置文件--><includes......
  • javascript的 this 详解以及apply与call的用法意义及区别
    [color=red][b]关于JavaScript中apply与call的用法意义及区别[/b][/color][url]http://www.cnitblog.com/yemoo/archive/2007/11/30/37070.aspx[/url][color=red][b]JAVASCRIPTTHIS详解[/b][/color]在面向对象编程语言中,对于this关键字我们是非常熟悉的。比如C++、C#和Java等都......
  • Redis的数据类型总结
    1:StringString有三种编码方式:int(整数型,直接以RedisObject存储)、raw(大于等于32位,使用sds进行存储)、内存结构为*ptr指向一个sdshdr,需要申请两次内存,可以修改!)embstr(小于32位),其中embstr只需要一次内存分配,数据比较小的时候使用,但他是只读的,如果需要修改会变为raw再执行修改2:Li......
  • BeanUtils使用总结
    [color=red][size=x-large]Commons-BeanUtils学习笔记[/size][/color[color=red][b]1、BeanUtils一共分4个包:[/b][/color][b]org.apache.commons.beanutilsorg.apache.commons.beanutils.convertersorg.apache.commons.beanutils.localeorg.apache.commons.beanutils.loc......
  • ROS2指令总结
    查看功能ros2nodelistros2topiclistros2servicelistros2actionlist查看节点信息:ros2nodeinfo<node_name>话题ros2topicecho<topic_name>查看话题的详细信息:ros2topicinfo<topic_name>查看话题的数据类型:ros2topiclist-t查看数据类型的具......
  • JAVA代理
    java代理静态代理和动态代理简介优点缺点静态代理在不修改目标对象的基础上,通过扩展类对目标对象进行增强和扩展。静态代理对客户(测试类)隐藏了被代理类接口(目标类接口)的具体实现类,在一定程度上实现了解耦合,同时提高了安全性。静态代理类需要实现目标类(被代理类)的接......
  • java将wkt面数据转geojson和elasticsearch的shape数据
    wkt面数据转geojsonimportcom.alibaba.fastjson.JSONException;importcom.alibaba.fastjson.JSONObject;importorg.locationtech.jts.geom.Coordinate;importorg.locationtech.jts.geom.Geometry;importorg.locationtech.jts.io.ParseException;importorg.location......
  • 如何实现java8 list按照元素的某个字段去重
    list按照元素的某个字段去重1234567@Data@AllArgsConstructor@NoArgsConstructorpublicclassStudent{privateIntegerage;privateStringname;}测试数据12345List<Student>studentList=Lists.newArrayList();studentL......
  • 实验4 函数与异常处理编程
    实验任务一task1:程序源代码:1print(sum)2sum=423print(sum)45definc(n):6sum=n+17print(sum)8returnsum910sum=inc(7)+inc(7)11print(sum)运行结果:实验任务二task2-1源代码:1deffunc1(a,b,c,d,e,f):2......