| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 777 人关注过本帖
标题:初学Java编程,这里对圆柱按体积排序的地方出了问题,怎么修改
只看楼主 加入收藏
一条沙丁鱼
Rank: 1
等 级:新手上路
威 望:1
帖 子:44
专家分:7
注 册:2015-4-5
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
初学Java编程,这里对圆柱按体积排序的地方出了问题,怎么修改
package Java;
public class Circle
{
    int r;

    Circle(int r)
    {
        this.r = r;
    }

    // 返回直径
    public double getDiameter()
    {

        return 2 * r;
    }

    // 返回周长
    public double getCircumference()
    {
        return 2 * 3.14 * r;
    }

    // 返回面积
    public double getArea()
    {
        return Math.PI * r * r;

    }
}
package Java;

import
import java.util.Scanner;

public class Main
{
    static Cylinder cy[];

    public static void main(String[] args) throws IOException
    {
        double data[] = new double[3];
        cy = new Cylinder[3];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < cy.length; i++)
        {
            System.out.println("请输入圆柱的半径:");
            int r = sc.nextInt();
            System.out.println("请输入圆柱的高:");
            int h = sc.nextInt();
            cy[i] = new Cylinder(r, h);
        }
        for (int i = 0; i < cy.length; i++)
        {
            data[i] = cy[i].getVolume();
        }
        findCy();
        sort(data);

    }

    public static void sort(double data[])
    {
        double t = 0;
        for (int i = 0; i < data.length; i++)
        {
            for (int j = 0; j < data.length - i - 1; j++)
            {
                if (data[j] > data[j + 1])
                {
                    t = data[j];
                    data[j] = data[j + 1];
                    data[j + 1] = t;
                }
            }
        }
        System.out.println("排序结果为");
        for (int i = 0; i < data.length; i++)
        {
            System.out.println(data[i]);
        }

    }

    public static void findCy()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的高");
        int h = sc.nextInt();
        System.out.println("请输入要查找的半径:");
        int r = sc.nextInt();

        boolean flag = false;
        for (int i = 0; i < cy.length; i++)
        {
            if (r == cy[i].getr() && h == cy[i].getHeight())
            {
                System.out.println("第" + (i +1)+ "个圆柱满足条件");
                flag = true;
            }
        }
        if (flag == false)
        {
            System.out.println("没有找到这样的圆柱");
        }
    }
}
搜索更多相关主题的帖子: package public double return import 
2015-05-17 16:31
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:5 
import
import java.util.Scanner;
 class Circle
{
    int r;

    Circle(int r)
    {
        this.r = r;
    }

    // 返回直径
    public double getDiameter()
    {

        return 2 * r;
    }

    // 返回周长
    public double getCircumference()
    {
        return 2 * 3.14 * r;
    }

    // 返回面积
    public double getArea()
    {
        return Math.PI * r * r;

    }
}
class Cylinder extends Circle{
    int h;
    public Cylinder(int r, int h) {
        super(r);
        this.h=h;
    }

    public double getVolume() {
        return h*this.getArea();
    }

    public int getHeight() {
        return h;
    }

    public int getr() {
        return r;
    }

}

public class Main
{
   
    static Cylinder cy[];

    public static void main(String[] args) throws IOException
    {
        double data[] = new double[3];
        cy = new Cylinder[3];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < cy.length; i++)
        {
            System.out.println("请输入圆柱的半径:");
            int r = sc.nextInt();
            System.out.println("请输入圆柱的高:");
            int h = sc.nextInt();
            cy[i] = new Cylinder(r, h);
        }
        for (int i = 0; i < cy.length; i++)
        {
            data[i] = cy[i].getVolume();
        }
        findCy();
        sort(data);

    }

    public static void sort(double data[])
    {
        double t = 0;
        for (int i = 0; i < data.length; i++)
        {
            for (int j = 0; j < data.length - i - 1; j++)
            {
                if (data[j] > data[j + 1])
                {
                    t = data[j];
                    data[j] = data[j + 1];
                    data[j + 1] = t;
                }
            }
        }
        System.out.println("排序结果为");
        for (int i = 0; i < data.length; i++)
        {
            System.out.println(data[i]);
        }

    }

    public static void findCy()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的高");
        int h = sc.nextInt();
        System.out.println("请输入要查找的半径:");
        int r = sc.nextInt();

        boolean flag = false;
        for (int i = 0; i < cy.length; i++)
        {
            if (r == cy[i].getr() && h == cy[i].getHeight())
            {
                System.out.println("第" + (i +1)+ "个圆柱满足条件");
                flag = true;
            }
        }
        if (flag == false)
        {
            System.out.println("没有找到这样的圆柱");
        }
    }
}

剑栈风樯各苦辛,别时冰雪到时春
2015-05-17 17:10
一条沙丁鱼
Rank: 1
等 级:新手上路
威 望:1
帖 子:44
专家分:7
注 册:2015-4-5
收藏
得分:0 
回复 2楼 林月儿
额,可能是我没有表达清楚我的意思,这样子按体积大小进行比较然后排序后,只是把两个圆柱的体积值进行了交换。就是说,假如第一个圆柱的底面半径和高分别是1,1;第二个圆柱的底面半径和高分别是1,2;计算的第一个圆柱的体积为3.14(约值),第二个为6.28;然后data[]存储了这两个值,进行排序时,6.28>3.14,进行交换,于是第一个圆柱的体积为6.28了,这样不是不对吗?请问是这样运行的吗?如果是,怎么修改?
2015-05-20 15:02
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:15 
import
import java.util.Scanner;
 class Circle
{
    int r;

    Circle(int r)
    {
        this.r = r;
    }

    // 返回直径
    public double getDiameter()
    {

        return 2 * r;
    }

    // 返回周长
    public double getCircumference()
    {
        return 2 * 3.14 * r;
    }

    // 返回面积
    public double getArea()
    {
        return Math.PI * r * r;

    }
}
class Cylinder extends Circle{
    int h;
    public Cylinder(int r, int h) {
        super(r);
        this.h=h;
    }

    public double getVolume() {
        return h*this.getArea();
    }

    public int getHeight() {
        return h;
    }

    public int getr() {
        return r;
    }

}

public class Main
{
   
    static Cylinder cy[];

    public static void main(String[] args) throws IOException
    {
        double data[] = new double[3];
        cy = new Cylinder[3];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < cy.length; i++)
        {
            System.out.println("请输入圆柱的半径:");
            int r = sc.nextInt();
            System.out.println("请输入圆柱的高:");
            int h = sc.nextInt();
            cy[i] = new Cylinder(r, h);
        }
        findCy();
        sort(cy);

    }

    public static void sort(Cylinder data[])
    {
        Cylinder t;
        for (int i = 0; i < data.length; i++)
        {
            for (int j = 0; j < data.length - i - 1; j++)
            {
                if (data[j].getVolume() > data[j + 1].getVolume())
                {
                    t = data[j];
                    data[j] = data[j + 1];
                    data[j + 1] = t;
                }
            }
        }
        System.out.println("排序结果为");
        for (int i = 0; i < data.length; i++)
        {
            System.out.println(data[i].r+"-"+data[i].h+"-"+data[i].getVolume());
        }

    }

    public static void findCy()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的高");
        int h = sc.nextInt();
        System.out.println("请输入要查找的半径:");
        int r = sc.nextInt();

        boolean flag = false;
        for (int i = 0; i < cy.length; i++)
        {
            if (r == cy[i].getr() && h == cy[i].getHeight())
            {
                System.out.println("第" + (i +1)+ "个圆柱满足条件");
                flag = true;
            }
        }
        if (flag == false)
        {
            System.out.println("没有找到这样的圆柱");
        }
    }
}

剑栈风樯各苦辛,别时冰雪到时春
2015-05-20 16:41
一条沙丁鱼
Rank: 1
等 级:新手上路
威 望:1
帖 子:44
专家分:7
注 册:2015-4-5
收藏
得分:0 
好的,谢谢,我调试看看
2015-05-22 09:47
快速回复:初学Java编程,这里对圆柱按体积排序的地方出了问题,怎么修改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018417 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved