m0_74423385 2024-05-19 18:22 采纳率: 60%
浏览 11
已结题

Java-Oj-桌布的计算

题目:
桌布的计算
设计要求:必须使用面向对象的程序设计思想完成设计。
(1)某商家提供桌布定制服务,因为是定制的,所以桌布的价格按照桌布的面积计算。不同型号的桌布单价不同,单价由商家提供。顾客定制时需提供桌布的宽与长,桌布的型号,依据桌布的面积计算桌布价格。如果桌布的型号输入错误,就认为是无效对象,显示“type 值 not exists”。
(2)某顾客依据个人的需要定制了多块桌布,请列出该顾客的订单所有内容,并显示总件数与总价格。
提示:使用Scanner对象reader调用方法reader.hasnextLine(),判断是否以EOF结束,键盘输入以Ctrl+Z表示结束。
输入:商家桌布型号与面积单价,若干块桌布的型号、长度、宽度。
输出:若干块桌布的信息(型号、长度、宽度、桌布价格),总件数,总价格,其中,长度、宽度、价格保留一位小数。
样例输入:
J001:25 T001:100 L003:72
J001 1.2 0.8
J002 0.6 0.6
L003 1 0.6
样例输出:
type J002 not exists
J001 1.2 0.8 24.0
L003 1.0 0.6 43.2
2 67.2

我的代码

package test4;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner reader=new Scanner(System.in);
        
        Tablecloth tc[]=new Tablecloth[100];
        Customer cu[]=new Customer[100];
        
        
        String str=reader.nextLine();
        String[] a=str.split(" ");
        for(int i=0;i<a.length;i++) {
            String info[]=a[i].split(":");    
            tc[i]=new Tablecloth();
            tc[i].style=a[0];
            double p=Double.parseDouble(info[1]);
            tc[i].price=p;
        }
        
        int k=0;
        while(reader.hasNextLine()) {
            str=reader.nextLine();
            String[] b=str.split(" ");        
            cu[k]=new Customer();
            cu[k].style=b[0];
            cu[k].height=Double.parseDouble(b[1]);
            cu[k].width=Double.parseDouble(b[2]);
            k++;            
        }
        
        double w=0;//总价格
        double v = 0;//单价
        int c=0;//满足条件的个数
    
        for(int i=0;i<k;i++) {
            int f=0;//判断是否存在
            for(int j=0;j<a.length;j++) {
                if(cu[i].style.equals(tc[j].style)) {
                    f=1;
                    v=tc[j].price*(cu[i].height*cu[i].width);
                        c++;
                    System.out.printf("%s %.1f %.1f %.1f",cu[i].style+cu[i].height+cu[i].width+v);
                }            
            }
            if(f==0) System.out.println("type"+" "+cu[i].style+" not exists");
            else w=w+v;
        }
        
        System.out.printf("%d %.1f",c,w);    
        
    }

}


class Customer{
    String style;
    double height;
    double width;
    
}

class Tablecloth{
    String style;
    double price;
        
}

我真心感觉我写的完全没有错,但是就是输不出来,到底里错了,请指出我的错误,让我的代码能够运行(要在我的代码基础上改!),谢谢!

  • 写回答

22条回答 默认 最新

  • Mr.Guoguo 2024-05-21 17:08
    关注
    System.out.printf("%s %.1f %.1f %.1f",cu[i].style+cu[i].height+cu[i].width+v);
    

    你的代码这么改应该就能运行了

    System.out.printf("%s %.1f %.1f %.1f",cu[i].style,cu[i].height,cu[i].width,v);
    

    但是你的题目要求面向对象,封装、继承、多态一样不占,给你参考一下我的

    
    ```java
    
    
    import java.util.*;
    
    public class TTT {
        public static void main(String[] args) {
            Map<String, TableCloth> map = new HashMap<>();
            Set<String> notExistsStyle = new HashSet<>();
            List<Customer> customers = new ArrayList<>();
            Scanner reader=new Scanner(System.in);
            String str=reader.nextLine();
            String[] a=str.split(" ");
            for (String s : a) {
                String[] info=s.split(":");
                TableCloth tableCloth = new TableCloth(info[0],Double.parseDouble(info[1]));
                map.put(info[0], tableCloth);
            }
            while(reader.hasNextLine()) {
                str=reader.nextLine();
                String[] b=str.split(" ");
                if (map.containsKey(b[0])){
                    Customer customer = new Customer(map.get(b[0]), Double.parseDouble(b[1]), Double.parseDouble(b[2]));
                    customers.add(customer);
                }else{
                    notExistsStyle.add(b[0]);
                }
            }
            for (String s : notExistsStyle) {
                System.out.println("type"+" "+s+" not exists");
            }
            double sum = 0;
            for (Customer customer : customers) {
                double value = customer.getValue();
                System.out.printf("%s %.1f %.1f %.1f\n",customer.getStyle(),customer.getHeight(),customer.getWidth(), value);
                sum = sum + value;
            }
            System.out.printf("%d %.1f\n",customers.size(),sum);
        }
    }
    
    class TableCloth{
        private String style;
        private double price;
    
        public String getStyle() {
            return style;
        }
    
        public void setStyle(String style) {
            this.style = style;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        public TableCloth(String style, double price) {
            this.style = style;
            this.price = price;
        }
    }
    
    class Customer extends TableCloth{
        private double height;
        private double width;
    
        public Customer(TableCloth tableCloth, double height, double width) {
            super(tableCloth.getStyle(), tableCloth.getPrice());
            this.height = height;
            this.width = width;
        }
    
        public double getHeight() {
            return height;
        }
    
        public void setHeight(double height) {
            this.height = height;
        }
    
        public double getWidth() {
            return width;
        }
    
        public void setWidth(double width) {
            this.width = width;
        }
    
        public double getValue(){
            return super.getPrice() * this.width * this.height;
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(21条)

报告相同问题?

问题事件

  • 系统已结题 5月31日
  • 已采纳回答 5月23日
  • 创建了问题 5月19日

悬赏问题

  • ¥30 设计一个图形用户界面来控制你机械臂的运动
  • ¥30 3d打印机无法识别到SD卡,如何解决?(相关搜索:格式化)
  • ¥15 RPG游戏架构设计和开发方法
  • ¥15 python 计算股权结构
  • ¥30 为什么会失败呢,该如何调整
  • ¥15 前端返回pdf时不显示内容
  • ¥50 如何在不能联网影子模式下的电脑解决usb锁
  • ¥20 服务器redhat5.8网络问题
  • ¥15 如何利用c++ MFC绘制复杂网络多层图
  • ¥20 要做柴油机燃烧室优化 需要保持压缩比不变 请问怎么用AVL fire ESE软件里面的 compensation volume 来使用补偿体积来保持压缩比不变