gpt4 book ai didi

java - 窃听了我自己的 If-Else 语句

转载 作者:行者123 更新时间:2023-11-30 11:03:31 25 4
gpt4 key购买 nike

所以我是一名正在学习 Java 的学生。我目前正忙于一项作业,但我一直被一些 if-else 逻辑所困。

我的作业要求我编写一个包含有理数(例如 72/14)的类文件,显示它,将其分解为最低形式(在本例中为 36/7),将有理数显示为 double 值,最后显示最大公因数。我坚持的事实是我的代码没有筛选出这样一个事实,例如,72/13 是有理数可以变成的最小值,但我明白了,所以它不会崩溃,但反过来我将其换成 72/14 的值(value)搜索,但无法正常工作。

我的代码:

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;
}
}

现在,请忽略我在知识渊博的 Java 程序员等级上仍然有些低的事实。我将不胜感激任何关于此的指示,因为我被卡住了并且无法真正弄清楚如何设置它。我在两个地方使用 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.

代码未经优化,看起来很丑,我知道,但如果有任何帮助,我们将不胜感激,在提交作业之前,我会仔细检查一下,让它看起来更漂亮一些。

最佳答案

需要修改的代码(IMO):

  1. 构造函数只能用于初始化对象
  2. 已更改 GCD引用链接后的功能
  3. 您可以在构造函数中添加简化函数(因为它会重新初始化值)
  4. 使用 try 和 catch block 来检查它们是否是有效输入,否则抛出 Exceptions

修改后的代码:

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;
}
}

关于java - 窃听了我自己的 If-Else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30343506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com