我目前的目标是解析分数并创建不正确的分数。 例如:解析分数
1_1/3 + 5/3
应该进入控制台,
4/3 + 5/3
有人能告诉我,我会在正确的去方向,我应该关注什么?
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 void parse(String in){
int underscore = in.indexOf("_");
int slash = in.lastIndexOf("/");
String wholenumber = in.substring(0, underscore);
String numerator = in.substring(underscore + 1,slash);
String denominator = in.substring(slash + 1);
if (underscore<0 & slash<0) {
in = wholenumber;
} else if (underscore<0 & slash>0) {
in = in;
} else if (underscore>0 & slash>0) {
} else if (underscore>0 & slash<0) {
in = "Error";
}
}
}
我会说你'.split(“_”)'该字符串和自动双方在一个单一的步骤。 – ChiefTwoPencils 2014-11-07 02:57:14
你的问题是什么? – Tetramputechture 2014-11-07 02:57:46
我们还没有了解.split(“_”)我在搞清楚如何处理if else(下划线> 0和斜线> 0){还有System.out.println( “你问我计算”+ in)我尝试用解析替换,但它不起作用 – 2014-11-07 03:13:50