【问题标题】:how to read this sentence and what is it called如何阅读这句话以及它叫什么
【发布时间】:2020-04-17 21:32:51
【问题描述】:

我有这个代码来找到最大的公共分隔符,但是有一个语句行我真的很困惑,我只是想知道如何理解它以及它调用了什么。

这是我感到困惑的代码:

int common = denom_one > denom_two ? gcd(denom_one, denom_two) : gcd(denom_two, denom_one);

这是包含它的程序:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gcd;

import java.util.Scanner;

/**
 *
 * @author LWTECH
 */
public class GCD {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //adding two fractions 1/42 + 1/30 = ?
        int num_one = 1; // numerator of the first fraction (1/42)
    int denom_one = 42; // denominator of the first fraction (1/42)
    int num_two = 1; // numerator of the second fraction (1/30)
    int denom_two = 30; // denominator of the second fraction (1/30)
    int num_sum; // numerator of the sum to be calculated
    int denom_sum; // denominator of the sum to be calculated

        // finding the greatest common divider of the denominators of the two fractions
        // in the case of our example the GCD of 42 and 30 is 6
        int common = denom_one > denom_two ? gcd(denom_one, denom_two) : gcd(denom_two, denom_one);
    int mult_one = denom_two / common; // finding the multiple for the first fraction, it is 5
    int mult_two = denom_one / common; // finding the multiple for the second fraction, it is 7

        // the two fractions are being added now: 1/42 + 1/30 = 12/210
    num_sum = num_one*mult_one + num_two*mult_two; 
    denom_sum = denom_one * mult_one;
        System.out.printf("%d/%d + %d/%d = %d/%d\n", num_one,denom_one, 
                                                   num_two, denom_two,
                                                   num_sum, denom_sum);
        // Simplifying the fraction 12/210.
        // Finding GCD of the numerator and denominator
        common = gcd(denom_sum, num_sum);
        denom_sum = denom_sum/common;
        num_sum = num_sum/common;
        System.out.printf("After simplification: %d/%d + %d/%d = %d/%d\n", num_one,denom_one, 
                                                   num_two, denom_two,
                                                   num_sum, denom_sum);


    }
    /**
     * This method implements Euclid's algorithm of calculation
     * of the greatest common divider (GCD) of two integers.
     * The algorithm has recursive nature:
     * GCD of x and y  with x > y is the same as GCD of y and (x % y )
     * (x= ky + x%y)
     * 
     * @param x first integer
     * @param y second integer
     * @return greatest common divider
     */
    public static int gcd(int x, int y)
    {
    do 
    {
        int tmp;
        tmp = y;
        y = x%y;
        x = tmp;
    }
    while(y>0);
    return x;
    }
}

【问题讨论】:

  • 这句话是什么?
  • int common = denom_one > denom_two ? gcd(denom_one,denom_two):gcd(denom_two,denom_one); - 对不起,为什么它被删除了

标签: java methods numbers add fractions


【解决方案1】:

这是一个三元运算符。它就像if (...) {} else {} 语句一样使用。 是这样的。

condition ? true : false;

在尝试使用条件初始化变量时很有用。

String bool = isTrue ? "true" : "false";

在您的情况下,如果denom_one 大于denom_two,它将使用gcd(denom_one, denom_two) 初始化变量common,否则,它将使用gcd(denom_one, denom_two) 初始化它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多