私は初心者のJavaプログラマで、2つのユーザ入力分数の等価性をテストするプログラムを作成しています。 Fractionクラスには、分子と分母を設定するためのミューテータメソッド、比率としての割合を表示するメソッド、および等価性をテストするメソッドが含まれます。ユーザーが分数を作成できるようにするMainクラスもあります。正しい出力を得ることができません。入力に関係なく、分数は常に「等しくない」と表示されます。次に例を示します。ここでは部分平等プログラムの出力が正しくありません
This application allows you to test if two fractions are equal to each other.
Enter the numerator of the first fraction: 1
Enter the denominator of the first fraction: 2
The fraction you entered is: 1/2
Enter the numerator of the target fraction: 2
Enter the denominator of the target fraction: 4
The target fraction you entered is: 2/4
The two fractions being compared are 1/2 and 2/4...
The two fractions are NOT equal.
Would you like to test another fraction? Enter 0 to quit anything else to continue.
は、クラスフラクションのためのコードは次のとおりです。
public class Fraction {
int numerator1;
int denominator1;
int numerator2;
int denominator2;
public void Fraction() {
numerator1 = 0; //initialize variables
denominator1 = 1;
numerator2 = 0;
denominator2 = 1;
}
public void setNum1(int num) { //mutator method
numerator1 = num;
}
public void setDen1(int denom) { //mutator method
denominator1 = denom;
}
public void setNum2(int num) {
numerator2 = num;
}
public void setDen2(int denom) {
denominator2 = denom;
}
public boolean equals(int numerator1, int numerator2, int denominator1, int denominator2) {
double frac1;
double frac2;
frac1 = numerator1/denominator1;
frac2 = numerator2/denominator2;
if(frac1 == frac2)
return true;
else
return false;
}
public String displayFraction1() {
String ratio1;
ratio1 = (numerator1 + "/" + denominator1);
return ratio1;
}
public String displayFraction2() {
String ratio2;
ratio2 = (numerator2 + "/" + denominator2);
return ratio2;
}
}
ここでは、クラスのメインのコードです:
import java.util.*;
public class Main {
public static void main(String[] args) {
Fraction create = new Fraction(); //creating fraction objects
Fraction target = new Fraction();
int num1, num2; //stores numerators of 2 fractions
int den1, den2; //stores denominators of 2 fractions
int input = 1; //initialized to 1 for while loop
Scanner keyboard = new Scanner(System.in);
System.out.println("This application allows you to test if two fractions are equal to each other.");
System.out.print("Enter the numerator of the first fraction: ");
num1 = keyboard.nextInt();
create.setNum1(num1);
System.out.print("Enter the denominator of the first fraction: ");
den1 = keyboard.nextInt();
create.setDen1(den1);
System.out.println("The fraction you entered is: " + create.displayFraction1());
while(input != 0) { //allows user to hold initial fraction and repeatedly tests against with target fractions
System.out.print("Enter the numerator of the target fraction: ");
num2 = keyboard.nextInt();
target.setNum2(num2);
System.out.print("Enter the denominator of the target fraction: ");
den2 = keyboard.nextInt();
target.setDen2(den2);
System.out.println("The target fraction you entered is: " + target.displayFraction2());
System.out.println("The two fractions being compared are " + create.displayFraction1() + " and " + target.displayFraction2() + "...");
if(create.equals(target)) //calls equals method to test equality
System.out.println("The two fractions are equal.");
else
System.out.println("The two fractions are NOT equal.");
System.out.println("Would you like to test another fraction? Enter 0 to quit anything else to continue.");
input = keyboard.nextInt();
}
}
}
私は今理解しています。ありがとうございました! – Snodalia