首页 > 其他分享 >题目集1-3总结报告

题目集1-3总结报告

时间:2023-05-24 23:25:16浏览次数:36  
标签:题目 String 总结报告 int System 菜品 input public

前言:

一 

第一次题目集所涉及的知识点有 1.从键盘读取数值并参与运算。2.if句和if-else句的判断。3.数值类型的转换。4.数组的创建。5.for语句的循环。6.switch语句的运用。.运用String中的substring方法获取子字符串和equalsIgnoreCase方法比较字符串。8.数组的简单处理。

题量较多,但知识点比较基础,难度不大。

二   

第二次题目集所涉及的知识点有1.为对象定义类。2.创建对象。3.通过引用变量

访问对象。4.this的引用。6.数据域的封装。7.Java日期类的使用。8.运用BufferedReader方法提高运行效率。

题量不多,但比第一次难度有所加大,初步开始了面向对象。

三   

第三次题目集所涉及的知识点有1.ArrayList类的使用。2.集合Set中的LinkedHashSet。3.toString方法的使用。4.List<String>的使用。5.数据域的封装。6.LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则。7.Integer类中parseInt()等方法的用法。

题量较多,对各种类中的方法的运用加大

总的来说,题目难度逐步提高,循环渐进,对学习很有价值。

设计与分析:

第一次题目集

身体质量指数(BMI)测算

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        //输入weight和height
        double weight = input.nextDouble();
        double height = input.nextDouble();
        //计算BMI
        double bmi = weight/(height*height);
        //判断BMI
        if(bmi<=0||weight>727||height>2.72)
            System.out.println("input out of range");
        else if(bmi<18.5)
            System.out.println("thin");
        else if(bmi<24)
            System.out.println("fit");
        else if(bmi<28)
            System.out.println("overweight");
        else
            System.out.println("fat");
    }
}

长度质量计量单位换算

 
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        //输入kg和meter
        double kg = input.nextDouble();
        double meter = input.nextDouble();
        //计算poundage和meter
        double poundage = kg/0.45359237;
        double ench = meter/0.0254;
        //输出poundage和meter
        System.out.print((float)poundage+" "+(float)ench);
    }
}

奇数求和

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        //定义number数组
        int[] number = new int[10];
        int sum=0;
        
        for(int i=0;i<10;i++){
            number[i] = input.nextInt();
            //判断奇数
            if(number[i]%2!=0)
                sum+=number[i];
        }
        //输出sum
        System.out.print(sum);
    }
}

房产税费计算

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        double deed;
        double stamp_duty;
        double transaction_fees;
        float mapping_fees;
        //输入数据
        Scanner input = new Scanner(System.in);
        int count = input.nextInt();
        int payment = input.nextInt();
        int price = input.nextInt();
        float area = input.nextFloat();
        //判断购房次数和面积
        if(count==1){
            if(area<=90)
                deed = price*0.01*10000;
            else if(area>90&&area<=144)
                deed = price*0.015*10000;
            else
                deed = price*0.03*10000;
        }
        else
            deed = price*0.03*10000;
        //计算数据
        stamp_duty = payment*0.0005*10000;
        transaction_fees = area*3;
        mapping_fees = area*1.36f;
        //输出数据
        System.out.print(deed+" "+stamp_duty+" "+transaction_fees+" "+mapping_fees);
    }
    }

游戏角色选择

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        //输入race和role
        Scanner input = new Scanner(System.in);
        int race = input.nextInt();
        int role = input.nextInt();
        //判断非法输入
    if(race>=1&&race<=4&&role>=1&&role<=3){
        //输出种族
        switch(race){
            case 1:System.out.print("人类 ");break;
            case 2:System.out.print("精灵 ");break;
            case 3:System.out.print("兽人 ");break;
            case 4:System.out.print("暗精灵 ");break;
//            default:System.out.print("Wrong Format");
        }
        //输出角色
       switch(role){
            case 1:System.out.print("战士");break;
            case 2:System.out.print("法师");break;
            case 3:System.out.print("射手");break;
//            default:System.out.print("Wrong Format");
       }
    }
        else
                   System.out.print("Wrong Format");
    }
    }

学号识别

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        String a;
        String year;
        String college;
        String clas;
        String num;
        //输入学号
        Scanner input = new Scanner(System.in);
        a = input.next();
        //判断非法输入
        if(8!=a.length()) {
            System.out.print("Wrong Format");
            return;
        }
        //获取信息
        year = a.substring(0,2);
        college = a.substring(2,4);
        clas = a.substring(4,6);
        num = a.substring(6,8);
        //判断学院
if(college.equalsIgnoreCase("01")) {
            college = "材料学院";
        }else if(college.equalsIgnoreCase("02")) {
            college = "机械学院";
        }else if(college.equalsIgnoreCase("03")) {
            college = "外语学院";
        }else if(college.equalsIgnoreCase("20")) {
            college = "软件学院";
        }else {
            System.out.print("Wrong Format");
            return;
        }
        //输出信息
        System.out.print("入学年份:20"+year+"年\n"+ "学院:"+college+"\n"+ "班级:"+clas+"\n"+ "学号:"+num);
    }
}

巴比伦法求平方根近似值

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        //输入n和lastGuess
                Scanner input = new Scanner(System.in);
        float n = input.nextFloat();
        float lastGuess = input.nextFloat();
        float nextGuess;
        //判断非法输入
        if(n>0&&lastGuess>0){
            nextGuess = (lastGuess+n/lastGuess)/2;
            while(Math.abs(nextGuess-lastGuess)>=0.00001){
                lastGuess = nextGuess;
                nextGuess = (lastGuess+n/lastGuess)/2;
            }
            System.out.print((float)lastGuess);
        }
        else
            System.out.print("Wrong Format");
    }
}

二进制数值提取

import java.util.Scanner;
public class Main {
        public static void main(String[] args){
            Scanner input = new Scanner (System.in);
            String a = input.nextLine();
            String b = "";
            for(int i=0;i<a.length();i++){
                //判断0和1
//                    if(a.charAt(i)!='-'&&a.charAt(i+1)!=1)
                        if(a.charAt(i)=='0'||a.charAt(i)=='1')
                            //将符合的数存入b
                            b+=a.charAt(i);
//                    System.out.print(a.charAt(i));
                //判断结束符
                else if(a.charAt(i)=='-'){
                    if(a.charAt(i+1)=='1'){
                        System.out.print(b);
                        return;
                    }
                }
            }
            System.out.print("Wrong Format");
        }
}

判断三角形类型

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
                Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.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.print("Equilateral triangle");
                else{
                    //是否等腰
                    if(a==b||a==c||b==c){
                        if(a*a+b*b-c*c<0.01||a*a+c*c-b*b<0.01||b*b+c*c-a*a<0.01)
                    System.out.print("Isosceles right-angled triangle");
                    else
                    System.out.print("Isosceles triangle");
                }
                else {
                    //是否直角
                    if(a*a+b*b-c*c<0.01||a*a+c*c-b*b<0.01||b*b+c*c-a*a<0.01)
                    System.out.print("Right-angled triangle");
                    else
                    System.out.print("General triangle");
                }
            }
        }
            else
                System.out.print("Not a triangle");
        }
        else
            System.out.print("Wrong Format");
    }
}

由于第一次的题目比较简单,知识点也很基础,不做分析

 

第二次题目集

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

import java.util.Scanner;
public class Main {

    public static int getDays(int year, int month, int day) {
        int[] months;
        months = new int[]{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
        int days;
        days = months[month - 1] + day;
        if (isLeapYear(year) && month >= 3) {
            days = days + 1;
        }
        return days;
    }

    public static int week(int year, int month, int day) {
        int num;
        if (month == 1 || month == 2) {
            month += 12;
            year--;
        }
        int a = year / 100;
        int b = year % 100;
        num = (a / 4) - 2 * a + b + (b / 4) + (13 * (month + 1) / 5) + day - 1;
        return num % 7;
    }

    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }


    private int year;
    private int month;
    private int day;

    public Main (int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public boolean checkValidity()//检测输入的年、月、日是否合法
    {
        if (month < 1 || month > 12 || day < 1 || day > 31) {
            return false;
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            if (day > 30)
                return false;
        }
        //二月份
        else if (month == 2) {
            //判断是否是闰年
            if (isLeapYear(year)) {
                if (day > 29) return false;
            } else if (day > 28) return false;
        } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            if (day > 31) return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year1, year2, year3;
        int month1, month2, month3;
        int day1, day2, day3;
        String data1 = input.nextLine();
        String data2 = input.next();
        String data3 = input.next();



        if ( data1.length() == 10) {
            year1 = Integer.parseInt(data1.substring(0, 4));
        month1 = Integer.parseInt(data1.substring(5, 7));
        day1 = Integer.parseInt(data1.substring(8, 10));
        Main dateOne = new Main(year1, month1, day1);
            
            int days1, dayWeek;
            days1 = getDays(year1, month1, day1);
            dayWeek = week(year1, month1, day1);
            if (dayWeek == 0)
                dayWeek = 7;
            if (isLeapYear(year1))
                System.out.println(data1+"是闰年.");
            System.out.println(data1 + "是当年第" + days1 + "天,当月第" + day1 + "天,当周第" + dayWeek + "天.");
            
        } else if(data1.length() != 10){
            System.out.println(data1 + "无效!");
            
        }

        if(data2.length() == 10){
        year2 = Integer.parseInt(data2.substring(0, 4));
        month2 = Integer.parseInt(data2.substring(5, 7));
        day2 = Integer.parseInt(data2.substring(8, 10));
        Main dateTwo = new Main(year2, month2, day2);

        year3 = Integer.parseInt(data3.substring(0, 4));
        month3 = Integer.parseInt(data3.substring(5, 7));
        day3 = Integer.parseInt(data3.substring(8, 10));
        Main dateThree = new Main(year3, month3, day3);


        if (  data3.length() == 10 && dateTwo.checkValidity() && dateThree.checkValidity()) {
            int days2, days3;
            days2 = getDays(year2, month2, day2);
            days3 = getDays(year3, month3, day3);
            if (year2 > year3) {
                System.out.println(data3 + "早于" + data2 + ",不合法!");
                
            } 
            else if(month2 > month3 && year2 == year3)
                System.out.println(data3 + "早于" + data2 + ",不合法!");
            else if(year2==year3&&month2==month3&&day2>day3)
                System.out.println(data3 + "早于" + data2 + ",不合法!");
            else {
                int dayNumber, monthNumber, yearNumber;
                yearNumber = year3 - year2;
                dayNumber = yearNumber * 365 + days3 - days2 + yearNumber / 4;
                monthNumber = month3 - month2;
                System.out.println(data3 + "与" + data2 + "之间相差" + dayNumber + "天,所在月份相差" + monthNumber + ",所在年份相差" + yearNumber+".");
            }
            
        }
        }
            else {
            System.out.println(data2 + "或" + data3 + "中有不合法的日期.");
        }
    }
}

这题需要用到Integer.parseInt()将输入的字符串转化为日期。还有几个算法1.获取该天是周几2.判断输入日期是否合法,和判断是否为闰年

 

小明走格子

import java.util.*;
import java.io.*;
public class Main{
 public static void main(String[] args) throws IOException{

     StreamTokenizer a = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
     a.nextToken(); int n = (int)a.nval;
     long [] dis=new long[100000];
     int[] step=new int[10000];
     int i,j;
     
for(i=0;i<n;i++){
    a.nextToken();
    step[i]=(int)a.nval;
    }
     dis[0]=1;
     dis[1]=1;
     dis[2]=2;
     dis[3]=4;
     dis[4]=8;
    for(j=0;j<n;j++){
        if(step[j]>=5){
      for(i=5;i<=step[j];i++){

            dis[i]=2 * dis[i - 1] - dis[i - 5];

        }

            System.out.printf("%d\n",dis[step[j]]);
        }
        else System.out.printf("%d\n",dis[step[j]]);
        }

}
}

这题有运行内存要求,需要用到用到速读StreamTokenizer re = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));,和优化算法简化循环b[i] = b[i-1] + b[i-2] + b[i - 3] + b[i - 4] == > b[i] = 2 * b[i - 1] - b[i - 5]

第三次题目集

 有重复的数据

import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int n= input.nextInt();
        input.nextLine();
        String[] a=input.nextLine().split(" ");
        input.close();
        Set<String> set = new HashSet<>(Arrays.asList(a).subList(0, n));
        if (set.size()==n) System.out.println("NO");
        else System.out.println("YES");
    }
}

 

自己建一个输入的类,加速输入,然后用HashSet存入所有数据,因为Set会自动去重,所以比较n与Set的size就可以判断是否有重复数据了

去掉重复的数据

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int sign=0;
        StringBuilder a = new StringBuilder(input.nextLine());
        while(input.hasNext())
        {
            String s = input.nextLine();
            a.append(s);
        }
        String[] b = a.toString().split(" ");
        LinkedHashSet<String> list = new LinkedHashSet<>();
        Collections.addAll(list, b);
        for (String c : list) {
            if(sign==0)
            {
                System.out.print(c);
                sign = 1;
            }
            else System.out.print(" "+c);
        }
    }
}

这个题目本身并不是很难,就是让你判断给出的n个数有没有重复出现的,但是scanner

读入太慢,首先我们想到BufferReader也就是使用缓冲来加快读入,那么从键判读如显然就不能够在使用scanner了,我们使用InputStreamReader。

单词统计与排序

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();
        String[] words = text.split("[,. ]+");
        Set<String> set = new HashSet<>(Arrays.asList(words));
        List<String> list = new ArrayList<>(set);
        list.sort((s1, s2) -> {
            if (s1.length() != s2.length()) {
                return s2.length() - s1.length();
            } else {
                return s1.compareToIgnoreCase(s2);
            }
        });
        for (String word : list) {
            System.out.println(word);
        }
    }
}

这题需要用到String中的split将英文文本转化为分段的字符串,并将空格和逗号去除,最后就可以做出来了

 

面向对象编程(封装性)

 
import java.util.Scanner;
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;
    String Major;
    int Age;
    Student(){

    }
    public setSid(String sid1){
        this.Sid=sid1;
    }
    public void setName(String name1){
        this.Name=name1;
    }
    public void setAge(int age1){
        this.Age=age1;
    }
    public void setMajor(String major1){
        this.Major=major1;
    }
    public Student(String sid2,String name2,int age2,String major2){
        this.Sid=sid2;
        this.Name=name2;
        this.Age=age2;
        this.Major=major2;
    }
    public  void print(){
        System.out.println("学号:"+Sid+",姓名:"+Name+",年龄:"+Age+",专业:"+Major);
    }
}

这题主要是this的引用,将学生的信息传入到Student中

 

GPS测绘中度分秒转换

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int angle,minute;
        double second;
        double sum;
        angle=input.nextInt();
        minute=input.nextInt();
        second=input.nextDouble();
        sum=angle+minute/60.0+second/3600.0;
        System.out.print(angle+"°"+minute+"′"+second+"″"+" = ");
        System.out.printf("%.6f",sum);
    }
}

这题的考的是格式化输出结果,没有什么难度

判断两个日期的先后,计算间隔天数、周数
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;

public class Main {

    public static int getDays(int year, int month, int day) {
        int[] months = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
        int days = months[month - 1] + day;
        if (isLeapYear(year) && month >= 3) {
            days++;
        }
        return days;
    }

    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }

    private final int year;
    private final int month;
    private final int day;

    public Main(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public boolean checkValidity() {
        if (month < 1 || month > 12 || day < 1 || day > 31) {
            return false;
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            return day <= 30;
        } else if (month == 2) {
            if (isLeapYear(year)) {
                return day <= 29;
            } else {
                return day <= 28;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year2, year3;
        int month2, month3;
        int day2, day3;


        String a = input.nextLine();
        String b = input.nextLine();
        String[] date2 = a.split("-");
        String[] date3 = b.split("-");

        year2 = Integer.parseInt(date2[0]);
        month2 = Integer.parseInt(date2[1]);
        day2 = Integer.parseInt(date2[2]);
        Main dateTwo = new Main(year2, month2, day2);

        year3 = Integer.parseInt(date3[0]);
        month3 = Integer.parseInt(date3[1]);
        day3 = Integer.parseInt(date3[2]);
        Main dateThree = new Main(year3, month3, day3);
        LocalDate dates1 = LocalDate.of(year2, month2, day2);
        LocalDate dates2 = LocalDate.of(year3, month3, day3);
        long day = dates1.until(dates2, ChronoUnit.DAYS);
        long week = dates1.until(dates2, ChronoUnit.WEEKS);
        if(dates1.isBefore(dates2) ){
            System.out.println("第一个日期比第二个日期更早");
            System.out.println("两个日期间隔" + Math.abs(day) + "天" );
            System.out.println("两个日期间隔" + Math.abs(week) + "周" );
        }
            
        else{
            System.out.println("第一个日期比第二个日期更晚");
            System.out.println("两个日期间隔" + Math.abs(day) + "天" );
            System.out.println("两个日期间隔" + Math.abs(week) + "周" );
        }           
        input.close();     
    }
}

 

 

菜单合集

菜单一

某饭店提供4种菜,每种菜品的基础价格如下:
西红柿炒蛋 15
清炒土豆丝 12
麻婆豆腐 12
油淋生菜 9

设计点菜计价程序,根据输入的订单,计算并输出总价格。
订单由一条或多条点菜记录组成,每条记录一行,最后以"end"结束
每条点菜记录包含:菜名、份额两个信息。
份额可选项包括:1、2、3,分别代表小、中、大份)

不同份额菜价的计算方法:
小份菜的价格=菜品的基础价格。
中份菜的价格=菜品的基础价格1.5。
小份菜的价格=菜品的基础价格
2。
如果计算出现小数,按四舍五入的规则进行处理。

参考以下类的模板进行设计:
菜品类:对应菜谱上一道菜的信息。
Dish {
String name;//菜品名称
int unit_price; //单价
int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
}

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品数组,保存所有菜品信息
Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
}

点菜记录类:保存订单上的一道菜品记录
Record {
Dish d;//菜品
int portion;//份额(1/2/3代表小/中/大份)
int getPrice()//计价,计算本条记录的价格
}

订单类:保存用户点的所有菜的信息。
Order {
Record[] records;//保存订单上每一道的记录
int getTotalPrice()//计算订单的总价
Record addARecord(String dishName,int portion)
//添加一条菜品信息到订单中。
}

 

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
//        准备菜
//        准备菜单
        Menu menu = new Menu();
        Dish d = new Dish("西红柿炒蛋", 15);
        menu.add(d);
        d = new Dish("清炒土豆丝", 12);
        menu.add(d);
        d = new Dish("麻婆豆腐", 12);
        menu.add(d);
        d = new Dish("油淋生菜", 9);
        menu.add(d);
        Order order = new Order(menu);
        String dishName = in.next();
        while (!dishName.equals("end")){
            int portion = in.nextInt();
            order.addARecord(dishName, portion);
            dishName = in.next();

        }
        System.out.println(Math.round(order.getTotalPrice()));

    }
}


//菜品类:对应菜谱上一道菜的信息。
class Dish {
    String name;// 菜品名称
    int unit_price; // 单价
    Dish(String name,int 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;// 菜品数组,保存所有菜品信息

    public void add(Dish dish)//菜单加一道菜
    {
        int ln = 0;
        if (dishs != null)
            ln = dishs.length;
        Dish[] tmp = new Dish[ln + 1];
        if (ln > 0)
            System.arraycopy(dishs, 0, tmp, 0, ln);
        tmp[ln] = dish;
        dishs = tmp;
    }

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

        for (int i=0;i<dishs.length;i++) {
            if (dishs[i].name.equals(dishName))
                return dishs[i];
        }
        return null;
    }
}

//点菜记录类:保存订单上的一道菜品记录
class Record {
    Dish d;// 菜品
    int portion;// 份额(1/2/3代表小/中/大份)

    public Record(Dish d, int portion) {
        this.d = d;
        this.portion = portion;
    }

    float getPrice()// 计价,计算本条记录的价格
    {
        return d.getPrice(portion);
    }
}

//订单类:保存用户点的所有菜的信息。
class Order {
    Record[] records;// 保存订单上每一道的记录
    Menu menu;

    float getTotalPrice()// 计算订单的总价
    {
        float total = 0;
        if (records == null)
            return 0;
        for (int i=0;i<records.length;i++) {
            total += records[i].getPrice();
        }
        return total;
    }


    public Order(Menu menu) {
        this.menu = menu;
    }

    //根据菜名点菜
    Record addARecord(String dishName, int portion) {
        Dish dish = menu.searchDish(dishName);
        Record record = null;
        if (dish != null) {
            record = new Record(dish, portion);
            int ln = 0;
            if (records != null)
                ln = records.length;
            Record[] tmp = new Record[ln + 1];
            if (ln > 0)
                System.arraycopy(records, 0, tmp, 0, ln);
            tmp[ln] = record;
            records = tmp;
        }
        else
            System.out.println(dishName+" "+"does not exist");
        return record;
    }
}

首先先创建Dish,Menu,Record,Order四个类,Dish为菜品类,包括菜品名称和单价,和计算菜品价格的方法。Menu类为菜谱类,包含饭店提供的所有菜的信息,创建菜品数组,保存所有菜品信息,里面有两个方法,一个是将菜品输入数组,一个是根据菜名在菜谱中查找菜品信息。Record类是点菜记录类,保存订单上的一道菜品记录,和计算一份菜的价格。Order类为订单类,保存用户点的所有菜的信息。最后计算所有菜的总价。

这到题给出了类和成员变量,极大的降低了难度,对类的运用有很好的引导作用。

菜单二

 

设计点菜计价程序,根据输入的信息,计算并输出总价格。

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

菜单由一条或多条菜品记录组成,每条记录一行

每条菜品记录包含:菜名、基础价格 两个信息。


订单分:点菜记录和删除信息。每一类信息都可包含一条或多条记录,每条记录一行。
点菜记录包含:序号、菜名、份额、份数。
份额可选项包括:1、2、3,分别代表小、中、大份。

删除记录格式:序号 delete

标识删除对应序号的那条点菜记录。

不同份额菜价的计算方法:
小份菜的价格=菜品的基础价格。
中份菜的价格=菜品的基础价格1.5。
小份菜的价格=菜品的基础价格
2。
如果计算出现小数,按四舍五入的规则进行处理。

参考以下类的模板进行设计:
菜品类:对应菜谱上一道菜的信息。

Dish {    
   String name;//菜品名称    
   int unit_price;    //单价    
   int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)    }
 

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

 
Menu {
   Dish[] dishs ;//菜品数组,保存所有菜品信息
   Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
   Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}
 

点菜记录类:保存订单上的一道菜品记录

 
Record {
   int orderNum;//序号\
   Dish d;//菜品\
   int portion;//份额(1/2/3代表小/中/大份)\
   int getPrice()//计价,计算本条记录的价格\
}
 

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

Order {
   Record[] records;//保存订单上每一道的记录
   int getTotalPrice()//计算订单的总价
   Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。
   delARecordByOrderNum(int orderNum)//根据序号删除一条记录
   findRecordByNum(int orderNum)//根据序号查找一条记录
}

 

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
//        准备菜
//        准备菜单
        Menu menu = new Menu();
        String name1 = input.next();
        int price1 = input.nextInt();
        String name2 = input.next();
        int price2 = input.nextInt();

        Dish d = new Dish(name1,price1);
        menu.add(d);
        d = new Dish(name2,price2);
        menu.add(d);
        Order order = new Order(menu);

        String orderNum = input.next();
        while (!orderNum.equals("end")){
            String dishName = input.next();
            int portion = input.nextInt();
            int num = input.nextInt();
            order.addARecord(orderNum,dishName, portion,num);
            orderNum = input.next();

        }
        System.out.println(Math.round(order.getTotalPrice()));

    }
}


//菜品类:对应菜谱上一道菜的信息。
class Dish {
    String name;// 菜品名称
    int unit_price; // 单价

    public String getName() {
        return name;
    }
    Dish(String name,int 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;// 菜品数组,保存所有菜品信息

    public void add(Dish dish)//菜单加一道菜
    {
        int ln = 0;
        if (dishs != null)
            ln = dishs.length;
        Dish[] tmp = new Dish[ln + 1];
        if (ln > 0)
            System.arraycopy(dishs, 0, tmp, 0, ln);
        tmp[ln] = dish;
        dishs = tmp;
    }

    Dish searchDish(String dishName)// 根据菜名在菜谱中查找菜品信息,返回Dish对象。
    {
        if (dishs == null)
            return null;
        for (int i=0;i<dishs.length;i++) {
            if (dishs[i].name.equals(dishName))
                return dishs[i];
        }
        return null;
    }
}

//点菜记录类:保存订单上的一道菜品记录
class Record {
    private String orderNum;//序号\
    private Dish d;//菜品\
    private int portion;//份额(1/2/3代表小/中/大份)\
    private int num;



    public Record(String orderNum, Dish d, int portion, int num) {
        this.orderNum = orderNum;
        this.d = d;
        this.portion = portion;
        this.num = num;
    }

    float getPrice()// 计价,计算本条记录的价格
    {
        return d.getPrice(portion)*this.num;
    }

    public Dish getD() {
        return d;
    }

    public String getOrderNum() {
        return orderNum;
    }
}

//订单类:保存用户点的所有菜的信息。
class Order {
    Record[] records;// 保存订单上每一道的记录
    Menu menu;

    float getTotalPrice()// 计算订单的总价
    {
        float total = 0;
        if (records == null)
            return 0;
        for (int i=0;i<records.length;i++) {
            total += records[i].getPrice();
        }
        return total;
    }

    public Order(Menu menu) {

        this.menu = menu;
    }

    //根据菜名点菜
    Record addARecord(String orderNum,String dishName, int portion,int num) {
        Dish dish = menu.searchDish(dishName);
        Record record = null;
        if (dish != null) {
            record = new Record(orderNum, dish, portion, num);
            float price = record.getPrice();
            System.out.println(record.getOrderNum() + " " + record.getD().getName() + " " + (int)price);
            int ln = 0;
            if (records != null)
                ln = records.length;
            Record[] tmp = new Record[ln + 1];
            if (ln > 0)
                System.arraycopy(records, 0, tmp, 0, ln);
            tmp[ln] = record;
            records = tmp;
        }
        else
            System.out.println(dishName+" "+"does not exist");
        return record;
    }
}

这题在菜单一的基础上加入删除功能,增加delARecordByOrderNum方法和findRecordByNum方法

有一点的难度

 

菜单三

 

设计点菜计价程序,根据输入的信息,计算并输出总价格。

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

菜单由一条或多条菜品记录组成,每条记录一行

每条菜品记录包含:菜名、基础价格 两个信息。

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

桌号标识独占一行,包含两个信息:桌号、时间。

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

删除记录格式:序号 delete

标识删除对应序号的那条点菜记录。

如果序号不对,输出"delete error"

代点菜信息包含:桌号 序号 菜品名称 份额 分数

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

折扣的计算方法(注:以下时间段均按闭区间计算):

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

周末全价,营业时间:9:30-21:30

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

参考以下类的模板进行设计:菜品类:对应菜谱上一道菜的信息。

Dish {

String name;//菜品名称

int unit_price; //单价

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

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

Menu {

Dish\[\] dishs ;//菜品数组,保存所有菜品信息

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

Dish addDish(String dishName,int unit_price)//添加一道菜品信息

}

点菜记录类:保存订单上的一道菜品记录

Record {

int orderNum;//序号\\

Dish d;//菜品\\

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

int getPrice()//计价,计算本条记录的价格\\

}

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

Order {

Record\[\] records;//保存订单上每一道的记录

int getTotalPrice()//计算订单的总价

Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

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

findRecordByNum(int orderNum)//根据序号查找一条记录

}

### 输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

最后一条记录以“end”结束。

### 输出格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

1、桌号,格式:table+英文空格+桌号+”:”

2、按顺序输出当前这一桌每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价

本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。

输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

最后一条记录以“end”结束。

输出格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

1、桌号,格式:table+英文空格+桌号+“:”+英文空格

2、按顺序输出当前这一桌每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价

本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
//        准备菜
//        准备菜单
        Menu menu = new Menu();
        String name1 = input.next();
        int price1 = input.nextInt();
        String name2 = input.next();
        int price2 = input.nextInt();

        Dish d = new Dish(name1,price1);
        menu.add(d);
        d = new Dish(name2,price2);
        menu.add(d);
        Order order = new Order(menu);

        String orderNum = input.next();
        while (!orderNum.equals("end")){
            String dishName = input.next();
            int portion = input.nextInt();
            int num = input.nextInt();
            order.addARecord(orderNum,dishName, portion,num);
            orderNum = input.next();

        }
        System.out.println(Math.round(order.getTotalPrice()));

    }
}


//菜品类:对应菜谱上一道菜的信息。
class Dish {
    String name;// 菜品名称
    int unit_price; // 单价

    public String getName() {
        return name;
    }
    Dish(String name,int 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;// 菜品数组,保存所有菜品信息

    public void add(Dish dish)//菜单加一道菜
    {
        int ln = 0;
        if (dishs != null)
            ln = dishs.length;
        Dish[] tmp = new Dish[ln + 1];
        if (ln > 0)
            System.arraycopy(dishs, 0, tmp, 0, ln);
        tmp[ln] = dish;
        dishs = tmp;
    }

    Dish searchDish(String dishName)// 根据菜名在菜谱中查找菜品信息,返回Dish对象。
    {
        if (dishs == null)
            return null;
        for (int i=0;i<dishs.length;i++) {
            if (dishs[i].name.equals(dishName))
                return dishs[i];
        }
        return null;
    }
}

//点菜记录类:保存订单上的一道菜品记录
class Record {
    private String orderNum;//序号\
    private Dish d;//菜品\
    private int portion;//份额(1/2/3代表小/中/大份)\
    private int num;



    public Record(String orderNum, Dish d, int portion, int num) {
        this.orderNum = orderNum;
        this.d = d;
        this.portion = portion;
        this.num = num;
    }

    float getPrice()// 计价,计算本条记录的价格
    {
        return d.getPrice(portion)*this.num;
    }

    public Dish getD() {
        return d;
    }

    public String getOrderNum() {
        return orderNum;
    }
}

//订单类:保存用户点的所有菜的信息。
class Order {
    Record[] records;// 保存订单上每一道的记录
    Menu menu;

    float getTotalPrice()// 计算订单的总价
    {
        float total = 0;
        if (records == null)
            return 0;
        for (int i=0;i<records.length;i++) {
            total += records[i].getPrice();
        }
        return total;
    }

    public Order(Menu menu) {

        this.menu = menu;
    }

    //根据菜名点菜
    Record addARecord(String orderNum,String dishName, int portion,int num) {
        Dish dish = menu.searchDish(dishName);
        Record record = null;
        if (dish != null) {
            record = new Record(orderNum, dish, portion, num);
            float price = record.getPrice();
            System.out.println(record.getOrderNum() + " " + record.getD().getName() + " " + (int)price);
            int ln = 0;
            if (records != null)
                ln = records.length;
            Record[] tmp = new Record[ln + 1];
            if (ln > 0)
                System.arraycopy(records, 0, tmp, 0, ln);
            tmp[ln] = record;
            records = tmp;
        }
        else
            System.out.println(dishName+" "+"does not exist");
        return record;
    }
}

 

踩坑心得

对结果的输出总是有错误,因为没有转化数据类型,和看题不仔细有一些关系,但是和题目表述也有关系

主要困难以及改进建议

运算超时,不能很好的处理,对算法的优化不太行,需要专门学习一下怎么简化算法

总结

经过这几次的练习对Java的运用有了简单的认识,但还是不熟练,对于复杂的题目,还是不能很好构建框架。

学会了1.为对象定义类。2.创建对象。3.通过引用变量访问对象。4.this的引用。6.数据域的封装。7.Java日期类的使用。等等

菜单题对我来说还是很有难度,在以后的学习中,需要进一步加强对Java类与对象的理解和使用,掌握更多的编程技巧和设计模式,提高编程能力和效率。还要就是应该多查些资料看些比较优秀的java代码,从而对自己的面向对象思维进行合理建构。

 

 

标签:题目,String,总结报告,int,System,菜品,input,public
From: https://www.cnblogs.com/gdkyyt/p/17429836.html

相关文章

  • pta题目集1-3
    前言:在完成这三个题目集之前,我对面向对象程序这几个字还没什么深入了解,甚至以为java和上学期学习的c语言没什么区别(落泪)。但当我真正着手这些题目时,才真正窥见一丝java的妙用!由于我的无知,第二,三次菜单计价程序都做的十分糟糕,以下二,三次菜单计价程序代码是和同学探讨思路之后自己......
  • Java PTA第1~3次题目集总结
    一.前言1.第一次题目集的知识点主要就是让我们初步意识到java与c语言不同的输入输出关键词用法以及一些相对固定的代码块(比如publicstaticvoidmain(String[]args){});题量相对较多但在承受范围之内;难度较为简单。2.第二次题目集的知识点除了跟第一次一样的之外,在7-1菜单题目里......
  • 前三次题目总结
    1,前言前三次题目集涉及的知识点主要包括输入输出的考察,循环的应用以及if-else的运用,类的创建,方法的调用,封装等等;题量适中,第一次九道入门的题目,第二次四道,第三次七道;难度而言第一次题目集由于有着C语言的基础还是较为简单的,第二次作业开始难度就上来了,如果平时没有花时间在学习jav......
  • 题目集1~3的总结性
    一、前言:总结三次题目集的知识点、题量、难度等情况题目集1:知识点:计算,单位换算,键盘录入,数组题量:9难度:中等题目集2:知识点:键盘录入,数组,类的创建,对象的创建和使用,java日期类,递归方法,条件语句和循环语句的使用题量:4难度:偏难题目集3:知识点:键盘录入,数组,类的创......
  • PTA前三次题目集总结BLOG
    一、前言本学期开展了面向对象程序设计这门课程,开始了Java语言的学习。现对前三次作业的题目集做概括分析: 1.第一次作业共九道题目,难度一般,均为基础题目,主要涉及到的主要是对java语法的认识,以及顺序结构、循环结构、选择结构等知识,这些与C语言并无太大区别,所以完成起来较为顺......
  • 题目集1~3总结
    一.前言Java编程语言是当今最流行的编程语言之一,由于其跨平台性、面向对象性和安全性等特点,受到广泛的应用。作为一名计算机专业的学生,在学习Java编程语言时,我们需要完成多个作业来巩固所学知识。在前三次Java作业中,我们已经学习了Java的基础知识和常用技术,通过完成这些作业,我们......
  • Blog-1-PTA题目集1~3
    前言:题目集1中共有9道题目,对应的题目及难度分别为:1.身体质量指数(BMI)测算(一星)2.长度质量计量单位换算(一星)3.奇数求和(一星)4.房产税费计算2022(一星)5.游戏角色选择(一星)6.学号识别(一星)7.巴比伦法求平方根近似值(一星)8.二进制数值提取(一星)9.判断三角形类型(一星)题目集2中共有4......
  • oop题目集1~3的总结性Blog
    1)前言第一次作业:7-1主要是从控制台输入问题,和嵌套循环。7-2的要点是数据的精确度,严格的输出。7-3是数组的调用,循环的使用。7-4要点在与if和else的使用和标准的输入和输出。7-5主要是switch方法的使用7-6主要是substring的调用和字符串长度的判断length()和.equals()的使用7-7主要......
  • 题目集1~3的总结
    1.前言在这三次的题目集中,主要涉及到了面向对象的相关知识点,包括类、对象、构造函数、封装、继承和多态等。我们学习了很多有关面向对象编程和软件设计的知识。这三次的题目集中,题目量和难度都有一定的增加。难度最大的题目集3中,我们需要设计一个完整的菜单计价系统,这一份题目集......
  • 前三次pta题目集分析
    (1)前言:我们是这个学期才开始学Java,上个学期学的是C,坦白说,我的C学的不好,然后这个学期的Java开始也是很吃力,编写代码很不熟练,这次pta的三个题目集,难度是在一步步增加的。第一次的题目一共是有9题,都算比较简单,知识点都是基础一些的计算等基本知识,我也拿到了满分。第二次的题目集......