如何在Java中比较相同方法的输入

huangapple go评论69阅读模式
英文:

how do I compare inputs from same methods in java

问题

以下是翻译好的内容:

我会感激一些协助来指出我哪里出错了。我试图比较相同方法中的分数,但似乎无法获得输出。当我运行代码时没有错误,但肯定有些东西丢失了,因此我无法获得输出。

我确实尝试使用设置器和获取器,但可能没有设置和获取正确的内容。

import java.util.Scanner;

public class Fraction {
    private int numerator;
    private int denominator;
    
    public void input() {
        Scanner kb = new Scanner(System.in);
        boolean flag = true;
        
        System.out.println("This program will display your input as fraction.");
        
        while (flag) {
            System.out.println("Enter a numerator: ");
            numerator = kb.nextInt();
            if (numerator < 0) {
                System.exit(numerator);
            }
            
            System.out.println("Enter a denominator: ");
            denominator = kb.nextInt();
            while (denominator == 0) {
                System.out.println("Please enter a number other than 0: ");
                denominator = kb.nextInt();
            }
            if (denominator < 0) {
                numerator = ~(numerator - 1);
                denominator = ~(denominator - 1);
            }
            
            display();
            isZero(numerator);
            //setNumerator();
            //setDenominator();
        }
    }
    
    public void setNumerator(int numerator) {
        this.numerator = numerator;
    }
    
    public void setDenominator(int denominator) {
        this.denominator = denominator;
    }
    
    public Integer getNumerator() {
        return this.numerator;
    }
    
    public Integer getDenominator() {
        return this.denominator;
    }
    
    public static boolean isZero(int numerator) {
        boolean fraction = false;
        if (numerator == 0) {
            fraction = true;
            System.out.println("Fraction is 0");
            System.exit(numerator);
        }
        return fraction;
    } 
    
    public void display() {
        if (numerator != 0) {
            System.out.println("The fraction is: " + numerator + "/" + denominator);
            System.out.println("--------------------------------");
        }
    }
    
    public boolean isEqual() {
        boolean equal = false;
        int otherNumerator = getNumerator();
        if (numerator == otherNumerator) {
            int otherDenominator = getDenominator();
            if (denominator == otherDenominator) {
                System.out.println("The fraction is the same.");
            }
            equal = true;
        } else {
            System.out.println("The fraction is not the same.");
        }
        return equal;
    }
}
public class TestFraction {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Fraction frac = new Fraction();
        
        frac.input();
        frac.display();
    }
}
英文:

I would appreciate some assistance to show me where I went wrong. I am trying to compare fraction from the same method but I can't seem to get the output. there is no error when I run the code but there must be something missing hence I couldn't get the output.

I did try using setter and getter but it is probably not setting and getting the right thing

import java.util.Scanner;
public class Fraction {
private int numerator;
private int denominator;
public void input() {
Scanner kb = new Scanner (System.in);
boolean flag=true;
System.out.println(&quot;This program will display your input as fraction.&quot;);
while (flag) {
System.out.println(&quot;Enter a numerator: &quot;);
numerator = kb.nextInt();
if (numerator &lt; 0 ) {
System.exit(numerator);
}
System.out.println(&quot;Enter a denominator: &quot;);
denominator = kb.nextInt();
while (denominator == 0) {
System.out.println(&quot;Plase enter a number other than 0: &quot;);
denominator = kb.nextInt();
}
if (denominator &lt; 0) {
numerator = ~(numerator - 1);
denominator = ~(denominator - 1);
}
display();
isZero(numerator);
//setNumerator();
//setDenominator();
}
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
}
public Integer getNumerator() {
return this.numerator;
}
public Integer getDenominator() {
return this.denominator;
}
public static boolean isZero(int numerator) {
boolean fraction = false;
if (numerator == 0) {
fraction = true;
System.out.println(&quot;Fraction is 0&quot;);
System.exit(numerator);
}
return fraction;
} 
public void display() {
if (numerator !=0) {
System.out.println(&quot;The fraction is: &quot; +numerator +&quot;/&quot; +denominator);
System.out.println(&quot;--------------------------------&quot;);
}
}
public boolean isEqual() {
boolean equal = false;
int otherNumerator = getNumerator();
if (numerator == otherNumerator) {
int otherDenominator = getDenominator();
if (denominator == otherDenominator) {
System.out.println(&quot;the fraction is the same.&quot;);
}
equal = true;
} else {
System.out.println(&quot;the fraction is not the same.&quot;);
}
return equal;
}
}
`
public class TestFraction {
public static void main(String[] args) {
// TODO Auto-generated method stub
Fraction frac = new Fraction();
frac.input();
frac.display();
}
}

答案1

得分: 1

这里有很多错位的内容。我认为我们现在无法帮助您解决特定的问题,因为在下一行您可能会陷入麻烦。您应该完全重新考虑/重写代码:

  • main 函数中,您需要编写 input 逻辑。Fraction 自身不应该有一个 input,特别是不应该作为非 static 方法存在。
  • display 在值为 0 的情况下也应该输出一些内容。
  • Fraction 应该有一个构造函数,接受 numeratordenominator
  • isZero 首先基本上是无用的,其次绝对不应该调用 System.exit(),而且根本不应该是 static 的。
  • isEqual 函数可能需要处理使 1/2 等于 1/4 的情况。
英文:

There is a lot of stuff here that is misplaced. I don't think we can help with your specific issue right now because you will just get into trouble on the next line. You should rethink / rewrite the code completely:

  • In the main you need to write the input logic. There should not be a input on Fraction itself, especially not as a non-static method.
  • display should output something in case of 0 as well.
  • Fraction should have a constructor that takes a numerator and denominator
  • isZero is firstly basically useless and secondly it should ABSOLUTELY NOT call System.exit() and it should not be static at all.
  • isEqual probably should take care of making 1/2 equal to 1/4

huangapple
  • 本文由 发表于 2020年9月28日 19:25:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64101279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定