如何计算面积 [英] How to calculate areas

查看:102
本文介绍了如何计算面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序来计算两个几何形状的面积:三角形和圆形。你必须使用功能。以下是您应该创建的函数:

public static double area_triangle(int base,int height)//返回三角形的区域

public static double area_circle(int radius )//返回一个圆圈的区域



你的程序应该提供一个菜单(三角形或圆形)供用户选择要计算的形状,然后询问它们适当的值(长度,宽度,半径等)。然后它应该将这些值传递给适当的函数并显示结果区域。

请注意,您不能在函数内输入值,也不能在函数内显示值。所有输入和输出必须在main()中,并且值必须传递给函数并从它们返回。

对于area_circle(),你需要π的值;随意使用名为Math.PI的内置双变量。



我尝试过:



i尝试了这个并且卡住了



  import  java.util.Scanner; 

public class 区域{


public static void main(< span class =code-sdkkeyword> String [] args){
public static double area_triangle( int base, int height){

System.out.print( 菜单 );





扫描仪输入= 扫描仪(System.in);
base = input.nextInt();
height = input.nextInt();
area_triangle =身高*基数/ 2;



}


}

}

解决方案

好吧,似乎你在main方法中定义了area_triangle方法。您必须在main方法之外定义它,然后在main方法中调用它。尝试类似:

< pre> import java.util.Scanner; 

public class 区域{


public static void main(< span class =code-sdkkeyword> String [] args){
Area a = new Area();
System.out.print( Menu);
扫描仪输入= 扫描仪(System.in);
double base = input.nextInt();
double height = input.nextInt();
a.area_triangle(基数,高度);

}
public static double area_triangle( int base, int height){


double area_triangle_1 = height *(base / 2);
System.out.println(area_triangle_1);
return area_triangle_1;


}

}



希望这会有所帮助。

干杯


Write a program to calculate the area of two geometric shapes: triangles and circles. You must use functions. Here are the functions you should create:
public static double area_triangle( int base, int height ) // returns the area of a triangle
public static double area_circle( int radius ) // returns the area of a circle

Your program should present a menu (triangle or circle) for the user to choose which shape to calculate, then ask them for the appropriate values (length, width, radius, etc.). Then it should pass those values to the appropriate function and display the resulting area.
Notice that you must not input the values inside the functions, and you must not display the values inside the functions. All input and output must be in the main(), and values must be passed to the functions and returned from them.
You'll need the value of π for area_circle(); feel free to use the built-in double variable called Math.PI.

What I have tried:

i tried this and got stuck

import java.util.Scanner;

public class Area {
	

	public static void main(String[] args) {
		public static double area_triangle( int base, int height )  {
			
			System.out.print("Menu");

			
			
			
			
			Scanner input = new Scanner (System.in); 
			base=input.nextInt(); 
			height=input.nextInt(); 
			area_triangle=height*base/2; 
			
			
			
		}
			
	 
	}

}

解决方案

Well, it seems that you're defining the "area_triangle" method inside the "main" method. You'll have to define it outside the main method and then call it inside the main method. Try something like:

<pre>import java.util.Scanner;

public class Area {
	

	public static void main(String[] args) {
		Area a=new Area();
                System.out.print("Menu");
	        Scanner input = new Scanner (System.in); 
		double base=input.nextInt(); 
		double height=input.nextInt();
                a.area_triangle(base,height);
                			
		}
          public static double area_triangle( int base, int height )  {
			
			 
			double area_triangle_1=height*(base/2);
                        System.out.println(area_triangle_1);
                        return area_triangle_1; 
			
	 
	}

}


Hope this helps.
Cheers.


这篇关于如何计算面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆