会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132437个问题
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 2612楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 2613楼

你好老师,关于折后价格,我想新建一个类,用组合的方法计算出折后价格再进行比较,写不下去了,蒙了,麻烦你用组合的方法补齐一下我的代码,谢谢

package com.luzhongxu.arrary;

public class Test08 {
    public static void main(String[] args) {
        shangpings x0 = new shangpings(1,"鼠标","BZ",99.21,0.9);
        shangpings x1 = new shangpings(2,"键盘","WO",403.00,0.7);
        shangpings x2 = new shangpings(3,"程序设计","BK",89.00,0.8);
        shangpings x3 = new shangpings(4,"西装","GQ",700.00,0.5);
        shangpings x4 = new shangpings(5,"手机","DM",900.00,0.3);
        shangpings[] x = new shangpings[5];
        x[0] = x0;
        x[1] = x1;
        x[2] = x2;
        x[3] = x3;
        x[4] = x4;
        for(int i = 0;i<x.length;i++){
            System.out.println(x[i]);

        }
    }
}
class shangpings{
     int id;
     String mingcheng;
    String xinghao;
     double price;
     double discount;

    shangpings(){}

    public shangpings(int id, String mingcheng, String xinghao, double price, double discount) {
        this.id = id;
        this.mingcheng = mingcheng;
        this.xinghao = xinghao;
        this.price = price;
        this.discount = discount;
    }

    @Override
    public String toString() {
        return "序号:"+getId()+"\t"+"名称:"+getMingcheng()+"\t"+"型号:"+getXinghao()+"\t"+"价格:"+getPrice()+"\t"+"折扣:"+getDiscount();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMingcheng() {
        return mingcheng;
    }

    public void setMingcheng(String mingcheng) {
        this.mingcheng = mingcheng;
    }

    public String getXinghao() {
        return xinghao;
    }

    public void setXinghao(String xinghao) {
        this.xinghao = xinghao;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

}
class zhehou  {   //zhehou= 折后价格
    shangpings sp = new shangpings();
    double zhehou;
    public void zhehou(double price,double discount,double zhehou){
        this.sp.price = price;
        this.sp.discount = discount;
        this.zhehou = zhehou;
        zhehou = price*discount;
        System.out.println(zhehou);

    }
}


JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 2614楼

数组错误.png

老师,这段代码没有报错,为什么嵌套循环用不了,是不是定义类表述一个二维数组之后,实际上算是一维数组,不能算二维数组,所以嵌套循环用不了,以下是原码

package com.luzhongxu.arrary;

public class Test08 {
    public static void main(String[] args) {
        shangping[][] x =new shangping[4][];
        shangping x0 = new shangping(1,"鼠标","BZ",99.21,0.9);
        shangping x1 = new shangping(2,"键盘","WO",403.00,0.7);
        shangping x2 = new shangping(3,"程序","BK",89.00,0.8);
        shangping x3 = new shangping(4,"西装","GQ",700.00,0.5);
        shangping x4 = new shangping(5,"手机","DM",900,0.4);

        x[4][0] = x0;
        x[4][1] = x1;
        x[4][2] = x2;
        x[4][3] = x3;
        x[4][4] = x4;

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

            for (int j = 0; j <x[i].length; j++) {
                System.out.print(x[i][j]);
            }
            System.out.println();
        }
    }
}
class shangping{
    private int id;
    private String mingcheng;
    private String xinghao;
    private double price;
    private double discount;

    shangping(){}

    public shangping(int id, String mingcheng, String xinghao, double price, double discount) {
        this.id = id;
        this.mingcheng = mingcheng;
        this.xinghao = xinghao;
        this.price = price;
        this.discount = discount;
    }

    @Override
    public String toString() {
        return "序号:"+getId()+"\t"+"名称:"+getMingcheng()+"\t"+"型号:"+getXinghao()+"\t"+"价格:"+getPrice()+"\t"+"折扣:"+getDiscount();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMingcheng() {
        return mingcheng;
    }

    public void setMingcheng(String mingcheng) {
        this.mingcheng = mingcheng;
    }

    public String getXinghao() {
        return xinghao;
    }

    public void setXinghao(String xinghao) {
        this.xinghao = xinghao;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }
}

shangping.png

JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 2615楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 2616楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 2617楼
JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 2618楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 2620楼
JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 2621楼

一、代码

package com.bjsxt;


import java.awt.*;

import javax.swing.*;


public class BallGame extends JFrame {


    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");

    Image desk = Toolkit.getDefaultToolkit().getImage("imges/desk.png");


    double x =200;

    double y =200;


    double degree = 3.14/3;  //弧度。 3.14=180°。      60度。

    //绘制窗口

    public void paint(Graphics g) {

        System.out.println("窗口被画了一次!");

        g.drawImage(desk,0,0,null);

        g.drawImage(ball,(int) x,(int) y,null);



        x = x + 10*Math.cos(degree);

        y = y + 10*Math.sin(degree);


        //碰到上下边界

        if(y>501-40-30||y<40+40){

            degree = 3.14 - degree;

        }


        //碰到右边界

        if(x>856-40-30||x<40){

            degree = 3.14 - degree;

        }

    }


    //创建窗口

    void launchFrame() {

        setSize(856, 501);

        setLocation(100, 100);

        setVisible(true);



        //实现动画,每秒绘制窗口25次

        while(true){

            repaint();


            try {

                Thread.sleep(40);//1s =1000ms;大约1秒绘制1000/40=25次

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    }






    public static void main(String[] args){

         System.out.println("我的小游戏开始了!");

         BallGame  game = new BallGame();

         game.launchFrame();

    }


}

二、用了懒加载还是不行


blob.png

JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 2622楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 2623楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 2624楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637