一个简单的java计算问题

Game-Over 2014-02-24 08:33:18
用Java计算3个长方形的面积和周长

import java.util.Scanner;

public class rectangle{

public static double getarea(double length, double width){
}
public static double getperimeter(double length, double width){
}
public static void main(String[]args){
Scanner input = new Scanner(System.in);
}}
...全文
159 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Game-Over 2014-02-24
  • 打赏
  • 举报
回复
引用 7 楼 ghostkngiht 的回复:

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double length1 = input.nextDouble();
        double width1 = input.nextDouble();
        double length2 = input.nextDouble();
        double width2 = input.nextDouble();
        double length3 = input.nextDouble();
        double width3 = input.nextDouble();
        System.out.println("1面积:" + getarea(length1, width1));
        System.out.println("1周长:" + getperimeter(length1, width1));
        System.out.println("2面积:" + getarea(length2, width2));
        System.out.println("2周长:" + getperimeter(length2, width2));
        System.out.println("3面积:" + getarea(length3, width3));
        System.out.println("3周长:" + getperimeter(length3, width3));
    }
谢谢。。我已经解开了。。我之前就是不明白,因为我用了同样的方法不知道哪一步错了,就run不了,所以我就认为这个不可行了。呵呵。。。谢谢了!
Game-Over 2014-02-24
  • 打赏
  • 举报
回复
引用 7 楼 ghostkngiht 的回复:

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double length1 = input.nextDouble();
        double width1 = input.nextDouble();
        double length2 = input.nextDouble();
        double width2 = input.nextDouble();
        double length3 = input.nextDouble();
        double width3 = input.nextDouble();
        System.out.println("1面积:" + getarea(length1, width1));
        System.out.println("1周长:" + getperimeter(length1, width1));
        System.out.println("2面积:" + getarea(length2, width2));
        System.out.println("2周长:" + getperimeter(length2, width2));
        System.out.println("3面积:" + getarea(length3, width3));
        System.out.println("3周长:" + getperimeter(length3, width3));
    }
对啊,这个就是我之前写的那个。所以说是没有简便的方法了是吗??
ghostkngiht 2014-02-24
  • 打赏
  • 举报
回复

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double length1 = input.nextDouble();
        double width1 = input.nextDouble();
        double length2 = input.nextDouble();
        double width2 = input.nextDouble();
        double length3 = input.nextDouble();
        double width3 = input.nextDouble();
        System.out.println("1面积:" + getarea(length1, width1));
        System.out.println("1周长:" + getperimeter(length1, width1));
        System.out.println("2面积:" + getarea(length2, width2));
        System.out.println("2周长:" + getperimeter(length2, width2));
        System.out.println("3面积:" + getarea(length3, width3));
        System.out.println("3周长:" + getperimeter(length3, width3));
    }
Game-Over 2014-02-24
  • 打赏
  • 举报
回复
引用 4 楼 ghostkngiht 的回复:

    public static double getarea(double length, double width) {
        return length * width;
    }

    public static double getperimeter(double length, double width) {
        return (length + width) * 2;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println("输入第" + (i + 1) + "个矩形的长和宽:");
            Scanner input = new Scanner(System.in);
            double length = input.nextDouble();
            double width = input.nextDouble();
            System.out.println("面积:" + getarea(length, width));
            System.out.println("周长:" + getperimeter(length, width));
        }
    }
你好,你的code我看到了,我想问下可不可以一起输入,然后一起print出来?
姜小白- 2014-02-24
  • 打赏
  • 举报
回复
将长,宽作为属性,将球周长,面积作为方法封装一个矩形Class出来,然后每次传参后,将参数转换为对象,通过对象去调用求周长和面积的方法。楼主看看java中类的概念吧,看完对这个应该就理解了。
ghostkngiht 2014-02-24
  • 打赏
  • 举报
回复

    public static double getarea(double length, double width) {
        return length * width;
    }

    public static double getperimeter(double length, double width) {
        return (length + width) * 2;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println("输入第" + (i + 1) + "个矩形的长和宽:");
            Scanner input = new Scanner(System.in);
            double length = input.nextDouble();
            double width = input.nextDouble();
            System.out.println("面积:" + getarea(length, width));
            System.out.println("周长:" + getperimeter(length, width));
        }
    }
Game-Over 2014-02-24
  • 打赏
  • 举报
回复
引用 2 楼 magi1201 的回复:
[quote=引用 1 楼 u012945394 的回复:] 我写了一个特别麻烦的,但是我想问下有没有简单的方法。 import java.util.Scanner; public class rectangle{ public static double getarea(double length, double width){ return length*width; } public static double getarea1(double length1, double width1){ return length1*width1; } public static double getarea2(double length2, double width2){ return length2*width2; } public static double getperimeter(double length, double width){ return 2*length+2*width; } public static double getperimeter1(double length1, double width1){ return 2*length1+2*width1; } public static double getperimeter2(double length2, double width2){ return 2*length2+2*width2; } public static void main(String[]args){ Scanner input = new Scanner(System.in); System.out.println("Please enter rectangle1's length: "); double length=input.nextDouble(); System.out.println("Please enter rectangle1's width: "); double width=input.nextDouble(); System.out.println("Please enter rectangle2's length: "); double length1=input.nextDouble(); System.out.println("Please enter rectangle2's width: "); double width1=input.nextDouble(); System.out.println("Please enter rectangle3's length: "); double length2=input.nextDouble(); System.out.println("Please enter rectangle3's width: "); double width2=input.nextDouble(); while(length != 0 ){ if (length==length){ System.out.println("The rectangle1's area is: "+getarea(length, width)+", perimeter is: "+getperimeter(length,width)); } if (length==length){ System.out.println("The rectangle2's area is: "+getarea1(length1, width1)+", perimeter is: "+getperimeter1(length1,width1)); } if (length==length){ System.out.println("The rectangle3's area is: "+getarea2(length2, width2)+", perimeter is: "+getperimeter2(length2,width2)); break;} } }}
封装为对象,每次给传参就是了。楼上写了这么多重复的方法没必要。[/quote] 对不起。。我是新手,没有明白你说的封装和传参什么意思。可以解释一下么?
姜小白- 2014-02-24
  • 打赏
  • 举报
回复
引用 1 楼 u012945394 的回复:
我写了一个特别麻烦的,但是我想问下有没有简单的方法。 import java.util.Scanner; public class rectangle{ public static double getarea(double length, double width){ return length*width; } public static double getarea1(double length1, double width1){ return length1*width1; } public static double getarea2(double length2, double width2){ return length2*width2; } public static double getperimeter(double length, double width){ return 2*length+2*width; } public static double getperimeter1(double length1, double width1){ return 2*length1+2*width1; } public static double getperimeter2(double length2, double width2){ return 2*length2+2*width2; } public static void main(String[]args){ Scanner input = new Scanner(System.in); System.out.println("Please enter rectangle1's length: "); double length=input.nextDouble(); System.out.println("Please enter rectangle1's width: "); double width=input.nextDouble(); System.out.println("Please enter rectangle2's length: "); double length1=input.nextDouble(); System.out.println("Please enter rectangle2's width: "); double width1=input.nextDouble(); System.out.println("Please enter rectangle3's length: "); double length2=input.nextDouble(); System.out.println("Please enter rectangle3's width: "); double width2=input.nextDouble(); while(length != 0 ){ if (length==length){ System.out.println("The rectangle1's area is: "+getarea(length, width)+", perimeter is: "+getperimeter(length,width)); } if (length==length){ System.out.println("The rectangle2's area is: "+getarea1(length1, width1)+", perimeter is: "+getperimeter1(length1,width1)); } if (length==length){ System.out.println("The rectangle3's area is: "+getarea2(length2, width2)+", perimeter is: "+getperimeter2(length2,width2)); break;} } }}
封装为对象,每次给传参就是了。楼上写了这么多重复的方法没必要。
Game-Over 2014-02-24
  • 打赏
  • 举报
回复
我写了一个特别麻烦的,但是我想问下有没有简单的方法。 import java.util.Scanner; public class rectangle{ public static double getarea(double length, double width){ return length*width; } public static double getarea1(double length1, double width1){ return length1*width1; } public static double getarea2(double length2, double width2){ return length2*width2; } public static double getperimeter(double length, double width){ return 2*length+2*width; } public static double getperimeter1(double length1, double width1){ return 2*length1+2*width1; } public static double getperimeter2(double length2, double width2){ return 2*length2+2*width2; } public static void main(String[]args){ Scanner input = new Scanner(System.in); System.out.println("Please enter rectangle1's length: "); double length=input.nextDouble(); System.out.println("Please enter rectangle1's width: "); double width=input.nextDouble(); System.out.println("Please enter rectangle2's length: "); double length1=input.nextDouble(); System.out.println("Please enter rectangle2's width: "); double width1=input.nextDouble(); System.out.println("Please enter rectangle3's length: "); double length2=input.nextDouble(); System.out.println("Please enter rectangle3's width: "); double width2=input.nextDouble(); while(length != 0 ){ if (length==length){ System.out.println("The rectangle1's area is: "+getarea(length, width)+", perimeter is: "+getperimeter(length,width)); } if (length==length){ System.out.println("The rectangle2's area is: "+getarea1(length1, width1)+", perimeter is: "+getperimeter1(length1,width1)); } if (length==length){ System.out.println("The rectangle3's area is: "+getarea2(length2, width2)+", perimeter is: "+getperimeter2(length2,width2)); break;} } }}

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧