英文:
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("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("Plase 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();
}
}
答案1
得分: 1
这里有很多错位的内容。我认为我们现在无法帮助您解决特定的问题,因为在下一行您可能会陷入麻烦。您应该完全重新考虑/重写代码:
- 在
main
函数中,您需要编写input
逻辑。Fraction
自身不应该有一个input
,特别是不应该作为非static
方法存在。 display
在值为0
的情况下也应该输出一些内容。Fraction
应该有一个构造函数,接受numerator
和denominator
。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 theinput
logic. There should not be ainput
onFraction
itself, especially not as a non-static
method. display
should output something in case of0
as well.Fraction
should have a constructor that takes anumerator
anddenominator
isZero
is firstly basically useless and secondly it should ABSOLUTELY NOT callSystem.exit()
and it should not bestatic
at all.isEqual
probably should take care of making1/2
equal to1/4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论