所以我是一名学习Java的学生。我目前正在忙于一项任务,但我仍然遇到一些if-else逻辑。
我的作业要求我应该写一个采用有理数(例如72/14)的类文件,显示它,将其分解为最低形式(在这种情况下为36/7),将有理数显示为{ {1}}值,最后显示最大的共同因素。我坚持的事实是我的代码没有筛选出这样一个事实:例如,72/13是理性数字可以变得最小的事实,但我得到它所以它不会崩溃,而是反过来我把它换成72/14的价值搜索,这样做不正常。
我的代码:
double
现在,请忽略这样一个事实,即我在知识渊博的Java程序员规模上仍然处于低位。我会很感激任何指针,因为我卡住了,无法弄清楚如何设置它。有两个地方我使用public class QTwoAssiThree
{
private int numerator;
private int denominator;
public void writeOutput()
{
if (getGCD(numerator, denominator) >= 2)
{
System.out.println(toString() + " is the simplified rational number.");
System.out.println(getValue() + " is the value of the two rational numbers as one number.");
System.out.println((getGCD(numerator, denominator) * 2) + " is the largest common factor.");
}
else if (getGCD(numerator, denominator) == 1)
{
System.out.println(getValue() + " is the value of the two rational numbers as one number.");
System.exit(0);
}
}
public QTwoAssiThree()
{
numerator = 0;
denominator = 0;
}
public QTwoAssiThree(int initialNum, int initialDenom)
{
if (denominator % 2 == 0)
{
numerator = initialNum;
if (initialDenom > 0)
{
denominator = initialDenom;
System.out.println(toString() + " is the rational number entered.");
simplify(numerator, denominator);
}
else
{
System.out.println("Invalid value added as the denominator! Only positive integers should be used.");
System.exit(0);
}
}
else if (denominator % 2 == 1)
{
System.out.println(toString() + " is what was entered, and is already in its simplest form.");
}
}
private void simplify(int newNum, int newDenom)
{
numerator = newNum / getGCD(newNum, newDenom);
denominator = newDenom / getGCD(newNum, newDenom);
}
private static int getGCD(int x, int y)
{
int difference = x % y;
return difference;
}
private double getValue()
{
double doubleValue = (double)numerator / (double)denominator;
return doubleValue;
}
public String toString()
{
String fraction = Integer.toString(numerator) + "/" + Integer.toString(denominator);
return fraction;
}
}
来查看它是否可行,第一个是:
if-else
和public void writeOutput()
构造函数中的public QTwoAssiThree(int initialNum, int initialDenom)
。最初那里没有任何if-else
语句,代码确实有效,但不适用于不均匀的整数。
public class QTwoAssiThreeDemo
{
public static void main(String[] args)
{
QTwoAssiThree rationalNumbers = new QTwoAssiThree(72, 14);
rationalNumbers.writeOutput();
}
}
用于演示整个班级。
基本上我的输出应该如下所示:
72/14 is the rational number entered.
36/7 is the simplified rational number.
5.142857143 is the value of the two rational numbers as one number.
2 is the largest common factor.
和
72/13 is what was entered, and is already in its simplest form.
5.538461538 is the value of the two rational numbers as one number.
代码是未经优化的,看起来很难看,我知道,但任何帮助都会受到赞赏,我会尽一切努力让它在提交作业之前看起来更好一些。
答案 0 :(得分:1)
需要更改的代码(IMO):
改变代码:
public class QTwoAssiThree {
private int numerator;
private int denominator;
public void writeOutput() {
System.out.println(toString() + " is the rational number entered.");
if (simplify(numerator, denominator)>1)
System.out.println(toString()+ " is the simplified rational number.");
else
System.out.println(toString()+ " is what was entered, and is already in its simplest form.");
System.out .println(getValue() +" is the value of the two rational numbers as one number.");
}
public QTwoAssiThree() {
numerator = 0;
denominator = 0;
}
public QTwoAssiThree(int initialNum, int initialDenom) {
numerator = initialNum;
denominator = initialDenom;
if (initialDenom <= 0) {
System.out
.println("Invalid value added as the denominator! Only positive integers should be used.");
System.exit(0);
}
}
private int simplify(int newNum, int newDenom) {
int gcd = getGCD(newNum, newDenom);
numerator = newNum / gcd;
denominator = newDenom / gcd;
return gcd;
}
private static int getGCD(int x, int y) {
if (y == 0) {
return x;
}
return getGCD(y, x % y);
}
private double getValue() {
double doubleValue = (double) numerator / (double) denominator;
return doubleValue;
}
public String toString() {
String fraction = Integer.toString(numerator) + "/"
+ Integer.toString(denominator);
return fraction;
}
}