java - 在一行中显示多个分数的运算

标签 java fractions

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

As of now, it's printing like this:

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


=   211 
    --- 
    181

The output should be displayed like this:

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);

    }
  }
}

I'm unable to trace out, as to what is preventing from printing the desired output.
Please help !!!

最佳答案

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

            // 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);

我可能把空格和制表符弄错了,但你明白了。

顺便说一句,您的输出不正确。这不是分数相加的方法。

关于java - 在一行中显示多个分数的运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30693874/

相关文章:

python - 添加分数类 python

java - Java 中的四舍五入小数

android - 小数到粗俗分数,Kotlin Android

java - weakhashmap 是如何工作的?

java - 使用从方法获得的值填充二维数组时遇到问题

java - JAX-RS 中的 Provider 是什么意思?

java - 通过缩放级别更改半径

python - 找到python中分数列表的最小公分母

python - python的fractions.limit_denominator是怎么实现的?

java - 无法在当前主题 CardView 中找到样式 'cardview style ' 在 Android Studio 3.1.3 中不起作用