我目前的目標是解析分數並創建不正確的分數。 例如:解析分數
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