用于计算分数的Java程序

时间:2014-10-08 15:42:42

标签: java html case lang

该程序的目的是获得一个分数的两个用户输入,从用户接收一个运算符,然后为另一个分数获得另外两个用户输入。程序必须检查两个分数中使用的数字是否在0-99之间,并且具有非零分母。该程序还必须确保用户输入有效的运算符( - ,+,*,/)。

我现在面临的唯一问题是我的变量都没有被初始化,而且我也不知道如何使输出看起来像这样:

 1     1      3
--- + ---  = ---
 4     8      8

这是我到目前为止的代码,任何帮助都会非常感激,因为我使用java的知识很少:

import java.util.Scanner;
public class FractionCalculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int n1;
int n2;
int d1;
int d2;
int n;
int d;
char o;
int m1,m2; 
int tempN1, tempN2;
int lcm, x;

System.out.println("Enter a numerator for fraction 1: ");
n1 = in.nextInt();
System.out.println("Enter a denominator for fraction 1: ");
d1 = in.nextInt();

if  (d1 > 0) {
  System.out.println(); 
} else {
  System.out.println("Invalid denominator");
  System.exit(0); 
}

System.out.println("Enter an operator: ");
o = in.next().toCharArray()[0]; 
System.out.println("Enter a numerator for fraction 2: ");
n2 = in.nextInt();
System.out.println("Enter a denominator for fraction 2: ");
d2 = in.nextInt();

if (d2 > 0) {
  System.out.println(); 
} else {
  System.out.println("Invalid denominator");
  System.exit(0); 
}


switch(o){
    case '*':
        n = n1 * n2;
        d = d1 * d2;
        break;

    case '/':
        n = n1 * d2;
        d = n2 * d1;
        break;

    case '+':
        int max=n1>d1?n1:d1;
        int min=n1<d1?n1:d1; 
        for(int i=1;i<=min;i++)
            x=max*i;
            if (x%min==0)
            lcm=x;      
        tempN1=n1*m1;
        tempN2=n2*m2;
        m1=lcm/d1;
        m2=lcm/d2;
        n = tempN1 + tempN2;
        d = lcm;
        break; 

    case '-':
        n = tempN1 - tempN2;
        d = lcm;
        break;
    default:
        System.out.println("Illegal Operator: "+ o);
        break; }
 }
}

3 个答案:

答案 0 :(得分:0)

也许你想进入OOP(面向对象编程):

Q x = new Q(1, 4);
Q y = new Q(1, 8);
Q z = x.plus(y);
System.out.println("%s + %s = %s%n", x, y, z);

(1 / 4) + (1 / 8) = (3 / 8)

public class Q {
    final int numerator;
    final int denominator;

    public Q(int numerator, int denominator) {
        int g = gcd(numerator, denominator);
        this.numerator = numerator / g;
        this.denominator = denominator / g;
    }

    @Override
    public String toString() {
        return String.format("(%d / %d)", numerator, denominator);
    }

    public Q plus(Q rhs) {
        return new Q(numerator * rhs.denominator + rhs.numerator * denominator,
            denominator * rhs.denominator);
    }

答案 1 :(得分:0)

问题的一部分:您需要为'+''-'执行某些逻辑,但是您编写它的方式将为{{1}执行只有:

'+'

当用户输入switch(o){ ... other cases case '+': ... logic to compute lcm and other things n = tempN1 + tempN2; d = lcm; break; case '-': n = tempN1 - tempN2; d = lcm; break; 时,该程序不会进入'-'下的部分。这意味着不会设置case '+'tempN1tempN2。如果你从编译器那里得到关于未初始化变量的错误,这就是原因之一。

编写针对多种情况执行的代码的一种方法:

lcm

也就是说,如果您有多个switch(o){ ... other cases case '+': case '-': ... whatever logic will apply to both cases if (o == '+') { ... whatever logic will apply to + only } else { ... whatever logic will apply to - only } ... if you have more logic for both cases, you can put it here break; 彼此相邻,并且中间没有代码,则以下代码适用于多个案例。 (这是因为“堕落”。它实际上转到了case,然后由于case '+'代码中没有break,它通过case '+'。但是不建议利用掉落,除非上面没有任何代码。)

答案 2 :(得分:0)

<强>更新

请注意,这不是我的完整和/或最佳解决方案,但至少它应该带您朝着正确的方向前进。你仍然需要减少分数并做一些其他的调整。

import java.math.BigInteger;
import java.util.Scanner;

public class FractionCalculator {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n1;
        int n2;
        int d1;
        int d2;
        int n = 0;
        int d;
        char o;

        System.out.println("Enter a numerator for fraction 1: ");
        n1 = in.nextInt();
        System.out.println("Enter a denominator for fraction 1: ");
        d1 = in.nextInt();

        if (d1 > 0) {
            System.out.println();
        } else {
            System.out.println("Invalid denominator");
            System.exit(0);
        }

        System.out.println("Enter an operator: ");
        o = in.next().toCharArray()[0];
        System.out.println("Enter a numerator for fraction 2: ");
        n2 = in.nextInt();
        System.out.println("Enter a denominator for fraction 2: ");
        d2 = in.nextInt();

        if (d2 > 0) {
            System.out.println();
        } else {
            System.out.println("Invalid denominator");
            System.exit(0);
        }

        switch (o) {
            case '*':
                n = n1 * n2;
                d = d1 * d2;

                break;

            case '/':
                n = n1 * d2;
                d = n2 * d1;

                break;

            case '+':
            case '-':

                d = gcd(d1, d2);

                n1 *= d / d1;
                n2 *= d / d2;

                if(o == '+') {
                    n = n1 + n2;
                }
                else if(o == '-') {
                    n = n1 - n2;
                }

                break;
            default:
                System.out.println("Illegal Operator: " + o);
                return;
        }

        System.out.printf(" %d     %d     %d\n", n1, n2, n);
        System.out.printf("--- %c --- = ---\n", o);
        System.out.printf(" %d     %d     %d\n", d1, d2, d);
    }

    private static int gcd(int d1, int d2) {
        BigInteger gcd = new BigInteger(String.valueOf(d1)).gcd(new BigInteger(String.valueOf(d2)));
        return gcd.intValue();
    }
}