将变量调用回main

时间:2019-03-15 23:24:13

标签: java methods

我应该在方法中找到五角大楼的区域,然后将其回调回main,以便可以将其打印到.txt文件中。我使所有东西都能完美地工作,然后当我尝试将其分解并将面积方程式放入方法中时,我不断遇到此错误: The method pentegon(int) in the type ABhw4part4 is not applicable for the arguments ()

在代码double area = pentegon();

(ABhw4part4是项目名称)

当我尝试将方程式的答案返回主变量时,似乎有些错误

我遇到另一个与字符串和字符有关的问题

我将不胜感激

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class ABhw4part4 {

public static void main(String[] args) throws IOException {

/* Write a method to calculate the area of a pentagon.

Call this method from MAIN and print the result.
Create a file and write all data to the file.

Expected Output:

Input the number of sides: 5
Input the side: 6
The area of the pentagon is 61.93718642120281
Expected file content:
Number of sides: 5
Side: 6*/

File myFile = new File("C://temp//ABhw4part4.txt");


if (myFile.createNewFile() == true) {
System.out.println("file is created");
} else {
System.out.println("file already exists");
}
PrintWriter writer = new PrintWriter(myFile);
Scanner sides = new Scanner (System.in);
int nos, sl;


System.out.println("enter the number of sides");
nos = sides.nextInt();
writer.println(nos);

System.out.println("enter the length of the sides");
sl = sides.nextInt();
writer.println(sl);


double area = pentegon();
System.out.println("area of the pentagon="+ area);
writer.println(area);
writer.close();

}
static double pentegon(int sl) {
double A = (5 * Math.pow(sl, 2)) / (4 * Math.tan(Math.PI /5));
return A;
}
}

1 个答案:

答案 0 :(得分:0)

您需要将参数传递给方法pentagon。现在你这样称呼它

double area = pentegon();

应该在哪里这样称呼

double area = pentegon(sl);