真的很简单的问题!如何创建一个区域& java中的周边函数? [英] Really simple problem! How to create an area & perimeter function in java?

查看:77
本文介绍了真的很简单的问题!如何创建一个区域& java中的周边函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。所以我需要在netbeans中编写一个代码来执行以下操作:



1.询问用户方块长度

2.调用一个名为 calculateArea 的函数方法,它返回正方形的区域

3.调用另一个为周边做同样事情的方法

4.在一个消息框中显示两个结果



非常感谢你!



< b>我尝试了什么:



我把:



/ *

*

* /



导入javax.swing.JOptionPane;



公共类计算{

public static void main(String [] args){



}



我真的不确定我在这里做什么

Hi. So I need to write a code in netbeans that does the following:

1. Asks the user for the length of the side of a square
2. Invoke a functional method named calculateArea that returns the area of the square
3. Invoke another method that does the same thing for the perimeter
4. Shows both results in one message box

Thank you so much!

What I have tried:

I put:

/*
*
*/

import javax.swing.JOptionPane;

public class Calculate {
public static void main(String[ ] args) {

}

I'm really not sure what I'm doing here

推荐答案

我不应该做你的家庭工作我没有太多Java经验,但有点提升可以帮助你我是这么认为的,



宣布这样的课程

I should not do your home work and also I don't have much experience in Java but a little boost up could help you I think so,

Declare a class like this
class HomeWork{
	
}





您需要两种方法,一种用于计算面积和除了外围...

方法只是函数所以声明两个函数,比如这个





You need two methods , one to calculate area and other to perimeter...
Methods are nothing but functions so declare two functions, like this

class HomeWork{
	void CalculateArea(double side)
	{
		System.out.println("Area:  "+ side*side);
	}
	void CalculatePerimeter(double side)
	{
		System.out.println("Perimeter: "+ 4*side);
	}
}





你可以看到这些功能正在变得像这样,功能(这里得到)一边)

因为区域只不过是边和边的平方只不过是边的总和。在这里,两个功能正在进行操作。



为了在主类中调用此函数,您需要一个对象(您看到该类只是一个蓝色的印花和一个物体被称为现实世界的东西)



的你有什么尝试我已经采取了代码



You can see that the functions are getting side like this, funtion(here it gets side)
since area is nothing but the square of side and perimeter is nothing but the sum of the sides. Here the tow functions are getting the side and doing the operations.

In order to call this function in your main class you need an object( you see that class is just a blue print and an object is called as a real world existing thing)

for "What Have you tried" I've taken the code

public class Calculate {
public static void main(String[ ] args) {

}





要求用户输入侧面并将其存储在变量中..





Ask the user to enter the side and store it in a variable..

public class Calculate {
public static void main(String[ ] args) {
System.out.println("Enter the side");
	Scanner obj = new Scanner(System.in);
	double s = obj.nextDouble();
}





现在你必须使用一个物体来执行操作





Now you have to make use of an object to perform the operation

HomeWork object = new HomeWork();
	object.CalculateArea(s);
	object.CalculatePerimeter(s);





所以有你的输出



以下是供您参考的主要代码



so there is your output

Here is the main code for your reference

public class Calculate {
   public static void main(String[] args) {
	System.out.println("Enter the side");
	Scanner obj = new Scanner(System.in);
	double s = obj.nextDouble();
	HomeWork object = new HomeWork();
	object.CalculateArea(s);
	object.CalculatePerimeter(s);
}
}





为了您的方便,请使用此完整代码





For your ease kindly use this complete code

import java.util.*;
class HomeWork{
	void CalculateArea(double side)
	{
		System.out.println("Area:  "+ side*side);
	}
	void CalculatePerimeter(double side)
	{
		System.out.println("Perimeter: "+ 4*side);
	}
}

public class Main {
   public static void main(String[] args) {
	System.out.println("Enter the side");
	Scanner obj = new Scanner(System.in);
	double s = obj.nextDouble();
	HomeWork object = new HomeWork();
	object.CalculateArea(s);
	object.CalculatePerimeter(s);
}
}





我现在的java知识从这里开始,请参考一些在线教程或询问你的导师(他们得到报酬来帮助你)。



请随时提出疑问。



My present knowledge in java ends here from here do refer some online tutorials or ask your Tutor( They get paid to help you) .

Please feel free to ask doubts.


这篇关于真的很简单的问题!如何创建一个区域&amp; java中的周边函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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