嗨,所以我需要帮助解析我的分数计算器。我对Java和编程完全陌生,我想知道是否有人能告诉我我的代码有什么问题?我目前的目标是根据给定的输入创建不正确的分数
例如: 1_2 / 3 + 2_3 / 4
应该像这样从控制台出来: 5/3 + 11/4
我真的不知道如何处理我现在所做的事情。它只是打印输入。有人能告诉我,我是否朝着正确的方向前进?
我的问题 我没有得到我想要的输出。输入正在打印出来。我该怎么办?
import java.util.Scanner;
public class FracCalc {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to FracCalc");
System.out.println("Type expressions with fractions, and I will evaluate them.");
boolean isTrue = true;
String in = "";
while(isTrue) {
in = input.nextLine();
if (in.equals("quit")) {
System.out.println("Thanks for running FracCalc!");
isTrue = false;
}else{
System.out.println("You asked me to compute " + in);
}
}
}
public static String parse(String in){
int underscore = in.indexOf("_");
int slash = in.lastIndexOf("/");
String wholeStr = in.substring(0, underscore);
String numeratorStr = in.substring(underscore + 1,slash);
String denominatorStr = in.substring(slash + 1);
int whole = Integer.valueOf(wholeStr);
int numerator = Integer.valueOf(numeratorStr);
int denominator = Integer.valueOf(denominatorStr);
int newNumerator = whole * denominator + numerator;
String newS = newNumerator + " / " + denominator;
if (underscore<0 & slash<0) {
return wholeStr;
} else if (underscore<0 & slash>0) {
return numeratorStr + " / " + denominatorStr;
} else if (underscore>0 & slash>0) {
in = newS;
} else if (underscore>0 & slash<0) {
return "Error";
}
}
}