2015-01-09 2 views
-4

나는 여기에 게시하고 전에 도움이되었습니다 여기 내 코드가 있습니다. 변수가 잘못된 것으로 보이고 이유가 확실하지 않습니다. 나는 1/2 1/2 3/4 분수를 입력하고 대답 8/4를 얻고 있습니다. 디버거를 실행할 때 "a"= 1, 'b'= 1, 'c'= 4 및 'd'= 4라고하기 때문에 변수와 관련이 있다는 것을 알고 있습니다. 'a'와 'd'는 내가 원하는 방식으로 올바른 것이며, 'a'에서 'd'를 거쳐 왼쪽에서 오른쪽으로 이동합니다. 나는 문제가 어디에 있는지 알지만 그것을 고치는 법을 모른다. 나는 여기에변수가 잘못되었습니다. 분수 계산기; 자바

b = Integer.parseInt("1"); 

내 전체 코드

수입하고있는 java.util입니다 'B'의 문제가 코드 행과 관련이있다 생각합니다. *;

public class Calculator { 
public static void main(String[] args) { 
System.out.println("Please enter two fractions to add, subtract, multiply, or divide\nor\nType    'quit' to exit the program."); 
Boolean on = true; 
Scanner console = new Scanner(System.in); 
while (on) { 
    String input = console.nextLine(); 
    if (input.equalsIgnoreCase("quit")) { 
     on = false; 
    } else 
     System.out.println(run(input)); 
} 
} 
public static String run(String input) { 
int indexOfSecondSpace = 0; 
int indexOfOperation = 0; 
String firstNumber = "0"; 
String secondNumber = "0"; 
int beginning = input.indexOf(" ") + 1; 
int end = input.indexOf(" ", beginning); 
String operator = input.substring(beginning, end); 
if (input.contains("+") == true) { 
indexOfOperation = input.indexOf("+"); 
} else if (operator.equals("-")) { 
indexOfOperation = input.indexOf("-"); 
} else if (operator.equals("*")) { 
indexOfOperation = input.indexOf("*"); 
} else if (operator.equals("/")) { 
indexOfOperation = input.indexOf("/"); 
} 
firstNumber = (input.substring(0, input.indexOf(" "))); 
secondNumber = (input.substring(beginning + 1)); 
int a = 0; 
int b = 0; 
int c = 0; 
int d = 0; 
if (firstNumber.contains("/")) { 
a = Integer.parseInt(firstNumber.substring(0,firstNumber.indexOf("/"))); 
b = Integer.parseInt(firstNumber.substring(0,firstNumber.indexOf("/"))); 
} else if (!firstNumber.contains("/")) 
a = Integer.parseInt(input.substring(0, input.indexOf(" "))); 
b = Integer.parseInt("1"); 
{ 
    if (secondNumber.contains("/")) { 
    c = Integer.parseInt(secondNumber.substring(secondNumber.indexOf("/")+1)); 
    d = Integer.parseInt(secondNumber.substring(secondNumber.indexOf("/")+1)); 
    } else if (!secondNumber.contains("/")) { 
    c = Integer.parseInt(secondNumber.substring(secondNumber.length())); 
    d = Integer.parseInt("1"); 
    } 
} 
String res = calculate(input, a, b, c, d) ; 
return res; 
} 
public static String calculate(String input, int a, int b, int c, int d){ 
if (input.contains ("+")) 
{ 
    System.out.println("your answer is " + (a*d + b*c)+"/" +(b*d)); 
} 
else if (input.contains("-")) 
{ 
    System.out.println("your answer is " + (a*d - b*c)+ "/" +(b*d)); 
} 
else if (input.contains("/")) 
{ 
    System.out.println("your answer is " + (a*d)/(b*c)+ "/" +(b*d)); 
} 
else if (input.contains("*")) 
{ 
    System.out.println("your answer is " + (a*c) +"/" +(b*d)); 
} 
return input; 
} 
} 

답변

1

당신은 당신의 코드에서 다음과 같은 한

c = Integer.parseInt(secondNumber.substring(secondNumber.indexOf("/")+1)); 
d = Integer.parseInt(secondNumber.substring(secondNumber.indexOf("/")+1)); 

왜 C와 D는 다른 값을 가질 기대?

또한 노트의

d = Integer.parseInt("1"); 

는 그래서 내가 어떻게 그것을 주변에 내가 너무 원하는 출력을 인쇄하려면이 고정 갈 것

d = 1; 
+0

로 재 작성 될 수 있습니다. 미안 나는 아직도 자바에 정말로 새로운 것이다. 도움이 절실합니다. 감사 – diy900