Java 클래스 소개에서 파일을 읽고 파일에서 수학 연산을 수행 한 다음 다른 파일로 출력한다고 가정합니다. 나는 파일 I/O 부분을 가지고있는 것 같다. 내가 겪고있는 문제는 수학 연산 단계에 있습니다. 우리는 SimpleMath라는 클래스를 받았고 그 클래스에서 메소드를 호출한다고 가정합니다. 내가 SimpleMath 클래스의 방법 geometryCircle 및 convertTemperature를 호출 할 때 내가 가진 한동일한 자바 프로젝트에서 다른 클래스의 열거 형 값 호출
import java.text.DecimalFormat;
public class SimpleMath {
// Operation enumeration
public enum eOperation {
PERIMETER, AREA
}
// Conversion enumeration
public enum eScale {
CELSIUSFAHRENHEIT, FAHRENHEITCELSIUS
}
// computeHypotenuse method
public static double computeHypotenuse(double adjacent, double opposite) {
// Pythagorean theorem
return Math.sqrt(adjacent * adjacent + opposite * opposite);
}
// solveQuadratic method
public static double solveQuadratic(double a, double b, double c) {
// Quadratic formula
double minusRoot = (-b - Math.sqrt((b * b) - (4.0 * a * c)))/(2.0 * a);
double plusRoot = (-b + Math.sqrt((b * b) - (4.0 * a * c)))/(2.0 * a);
return (plusRoot >= minusRoot ? plusRoot : minusRoot);
}
// convertTemperature method
public static double convertTemperature(eScale scale, double degrees) {
// Scale conversion
if (scale == eScale.CELSIUSFAHRENHEIT)
return (((9.0/5.0) * degrees) + 32.0);
else
return ((5.0/9.0) * (degrees - 32.0));
}
// geometryCircle method
public static double geometryCircle(eOperation op, double radius) {
// Basic geometry
if (op == eOperation.PERIMETER)
return (Math.PI * radius * 2.0);
else
return (Math.PI * radius * radius);
}
}
문제는 내 코드입니다 : 여기에 지정된 클래스입니다. 두 가지 방법은 클래스에 들어오는 변수를 호출 할 때 enum 변수를 사용하기 때문입니다. 내가 작업하고있는 P5 클래스를 수행하는 것처럼 보이더라도 클래스간에 이러한 enum 변수를 전달하는 데 문제가있는 것으로 나타났습니다.
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import SimpleMath.eOperation;
import SimpleMath.eScale;
public class P5
{
// These are the input variables used
double Adjacent,Opposite,CoEff0, CoEff1, CoEff2, Fahrenheit,Radius;
// These are the variables used for output values
double Hypotenuse, Root, Celsius, Area;
// This is the main method of class P5 main method that tells the other methods in this class where to find their variables.
public static void main(String[] args)
{
// Instantiates the Class P5 into variable p5.
P5 p5=new P5();
// Initiates the readFile method while passing it the arguments that are stored in the class.
p5.readFile(args[0]);
// Initiates the computeMath method with no added variables.
p5.computeMath();
// Initiates the writeFile method and passes the desired name, which is stored as a part of the class, of the new file being written to.
p5.writeFile(args[1]);
}
/* This method tells the computer to read in a file,
* which is specified in the RunConfigurations window under the Arguments tab in the file for this class,
* and then it assigns the values pulled from the file to the appropriate input variables
*/
private void readFile(String InputFile)
{
try
{
// This tells the computer how and where to look for the designated input file.
Scanner ReadFile = new Scanner(new File(InputFile));
// These set up variables so that all of the lines in the files can be assigned later.
String Line1 = ReadFile.nextLine();
String Line2 = ReadFile.nextLine();
String Line3 = ReadFile.nextLine();
String Line4 = ReadFile.nextLine();
String Line5 = ReadFile.nextLine();
String Line6 = ReadFile.nextLine();
String Line7 = ReadFile.nextLine();
// These are the variables, and here they are being assigned values from the file that is being read.
Adjacent = Double.valueOf(Line1);
Opposite = Double.valueOf(Line2);
CoEff0 = Double.valueOf(Line3);
CoEff1 = Double.valueOf(Line4);
CoEff2 = Double.valueOf(Line5);
Fahrenheit = Double.valueOf(Line6);
Radius = Double.valueOf(Line7);
// This lets the user see what values have been written to the variables, mostly for error checking purposes.
System.out.println("Adjacent: " + Adjacent);
System.out.println("Opposite: " + Opposite);
System.out.println("CoEff0: " + CoEff0);
System.out.println("CoEff1: " + CoEff1);
System.out.println("CoEff2: " + CoEff2);
System.out.println("Fahrenheit: " + Fahrenheit);
System.out.println("Radius: " + Radius);
// This closes the designated file and disconnects it from the stream.
ReadFile.close();
}
catch (IOException e)
{
System.out.println("Cannot Read File: " + InputFile);
System.exit(0);
}
}
// This method takes the values found in the ReadFile method and conducts mathematical operations on them.
private void computeMath()
{
// This calls the
Hypotenuse = SimpleMath.computeHypotenuse(Adjacent,Opposite);
Root = SimpleMath.solveQuadratic(CoEff0, CoEff1, CoEff2);
Celsius = SimpleMath.convertTemperature(eScale.FAHRENHEITCELSIUS, Fahrenheit);
Area = SimpleMath.geometryCircle(eOperation.AREA, Radius);
}
/* This method takes all of the variables values determined throughout the program
* and writes them to a file that is specified in the RunConfigurations window under
* the Arguments tab for this class.
*/
private void writeFile(String OutputFile)
{
try
{
// This initiates the PrintWriter class which allows the program to write to a file.
PrintWriter Output = new PrintWriter(new File(OutputFile));
// These are the lines of text in which the program is writing in the designated file.
Output.println("Adjacent = " + Adjacent);
Output.println("Opposite = " + Opposite);
Output.println("Coefficent 0 = " + CoEff0);
Output.println("Coefficent 1 = " + CoEff1);
Output.println("Coefficent 2 = " + CoEff2);
Output.println("Fahrenheit = " + Fahrenheit);
Output.println("Radius Of Circle = " + Radius);
// This closes the designated file and disconnects it from the stream.
Output.close();
}
catch(IOException e)
{
System.out.println("Cannot Write File: " + OutputFile);
System.exit(0);
}
}
}
그래서 어떤 도움이,이 코드는이 방식을 행동하는 이유에조차 추측 좋은 것 : 여기 내가 지금까지 쓴 P5 코드입니다. 감사합니다
내가 추가해야한다 "수입 SimpleMath.eScale;"그, "수입 SimpleMath.eOperation;", "섭씨 = SimpleMath.convertTemperature (eScale.FAHRENHEITCELSIUS, 화씨);", 그리고 "지역 = SimpleMath. geometryCircle (eOperation.AREA, Radius); " 현재 열거 형 연산과 관련된 오류가있는 P5의 유일한 부분입니다. –