public class Calculator
{
public static void main(String[] args)
{
String choice = "";
String userFract = "0";
String userFract2 = "0";
Scanner scan = new Scanner(System.in);
String fract = "0";
int clearCounter = 0;
String add = "a"; String multiply = "m";
String subtract = "s"; String divide = "d";
String reciprocal = "r"; String clear = "c";
String reverse = "n"; String proper = "p";
String improper = "i"; String quit = "q";
while(!choice.equals("q"))
{
if(clearCounter == 0)
{
System.out.print("Enter a fraction: ");
userFract = scan.nextLine();
clearCounter++;
}
NewFraction currentFract = NewFraction.parseNewFraction(userFract);
fract = currentFract.toString();
System.out.println(fract);
System.out.println("add: a, clear: c");
System.out.println("subtract: s, reverse sign: n");
System.out.println("multiply: m, proper display: p");
System.out.println("divide: d, improper display: i");
System.out.println("reciprocal: r, quit: q");
System.out.print("-->");
choice = scan.nextLine();
System.out.println("");
if(choice.equals(add))
{
System.out.print("Enter another fraction: ");
userFract2 = scan.nextLine();
NewFraction newFract = NewFraction.parseNewFraction(userFract2);
NewFraction Fract = currentFract.Add(newFract);
System.out.println(Fract.Proper());
}
if(choice.equals(clear))
{
clearCounter--;
}
}
}
}
我正在开发一个分数计算器的程序,就像我的NewFraction文件正常工作但我遇到计算器文件有问题。所以我的文件编译并执行但输出很奇怪。它取代了正确的答案,在下一行,它还显示原始分数? Idk该怎么做..帮助!
答案 0 :(得分:0)
您需要替换此行
System.out.println(Fract.Proper());
带
currentFract = Fract;
分数Fract
将在循环的下一次迭代开始时打印出来。