在一行中显示多个分数的操作

时间:2015-06-07 13:05:19

标签: java fractions

以下代码按预期工作,但我无法在一行显示输出。使用 Random() function. 打印所有数字随机数

  

截至目前,它的打印方式如下:

38 
--- + 
45 
90 
--- + 
16 
29 
--- + 
21 
54 
--- + 
99 


=   211 
    --- 
    181
  

输出应如下所示:

38      90       29     54     211
--- +  ----  +  ---- + ---- = ----- 
45      16       21     99     181

代码:

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class FractionJava2 {

    public static void main(String[] args) {

        /* Array to store Numerator variables */
        int[] num =  new int[4];

        /* Array to store Denominator variables */
        int [] denom = new int[4];

        /* Variable to hold the operator sign */
        char ch ;

        /* Random function to get the random numbers */
        Random random = new Random();

        /* Scanner function to take the user input */
        Scanner scan = new Scanner(System.in);

        int num_sum[] = new int[num.length];
        int denom_sum[] = new int[denom.length]; 


        for (int i = 0, j=0; i < denom.length; i++, j++) {

            num[i] = random.nextInt(100)+1;
            denom[j]=random.nextInt(100)+1;

            System.out.println("Numerators are: " +num[i]);
            System.out.println("Denominators are: "+denom[i]); 
        }


            System.out.println("Enter the operation you wish to perform");
            ch = scan.next().toCharArray()[0];

            System.out.println("\nThe resultant fraction is: ");

        /* Variable to hold the sum of numerator values */
        int numeratorSum = 0;

        /* Variable to hold the sum of denominator values */
        int denominatorSum = 0;


        switch(ch){

        case '+' : 


                for (int i = 0; i < denom_sum.length; i++) {

                    num_sum[i] = num_sum[i] + num[i];
                    denom_sum[i] = denom_sum[i]+ denom[i];

                    System.out.printf("%d \n", num_sum[i]);
                    System.out.printf("--- %c \n", ch);
                    System.out.printf("%d \n", denom_sum[i]);

                    numeratorSum = numeratorSum + num_sum[i];
                    denominatorSum = denominatorSum + denom_sum[i];
            }
                System.out.println("\n");
                System.out.printf("=\t");
                System.out.printf("%d \n", numeratorSum);
                System.out.printf("\t--- \n", ch);
                System.out.printf("\t%d ", denominatorSum);

    }
  }
}
  

我无法追查到什么阻止打印所需的输出。
  请帮助!!!

1 个答案:

答案 0 :(得分:1)

您需要多个循环,每个输出行一个循环:

            // first line
            for (int i = 0; i < denom_sum.length; i++) {
                num_sum[i] = num_sum[i] + num[i];
                System.out.printf("%d   ", num_sum[i]);
                numeratorSum = numeratorSum + num_sum[i];
            }
            System.out.printf("     %d \n", numeratorSum);
            // second line
            for (int i = 0; i < denom_sum.length; i++) {
                System.out.printf("--- %c", ch);
            }
            System.out.printf("=\t   --- \n", ch);
            // third line
            for (int i = 0; i < denom_sum.length; i++) {
                denom_sum[i] = denom_sum[i]+ denom[i];                    
                System.out.printf("%d    ", denom_sum[i]);
                denominatorSum = denominatorSum + denom_sum[i];
            }
            System.out.printf("\t%d \n", denominatorSum);

我可能错误地提到了空格和TAB,但是你明白了。

顺便说一下,你的输出不正确。这不是你添加分数的方式。