我不知道如何处理我的代码。分数转换

时间:2016-06-28 03:08:35

标签: java fractions

我不确定要添加到//Sample 3的内容,是否有人可以帮助我并告诉我我做错了什么?如果不正确的分数转换为整数,我正在写最后的部分写在其他地方,但我不知道如何写那个

package ch2_project;

import java.util.Scanner;

public class Ch2_project {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a numerator: ");
        int numerator = input.nextInt();
        System.out.print("Enter a denominator: ");
        int denominator = input.nextInt();

        if (numerator < denominator)
        {  
            System.out.println(numerator + " / " + denominator + " is a proper     fraction"); // Sample 2
        }
        else
        {
            int mix = numerator / denominator;
            int remainder = numerator % denominator;    
            System.out.println(numerator + " / " + denominator + " is a improper fraction and it's mixed fraction is " + mix + " and " remainder + " / " + denominator);// Sample 1
        }
        else if ()
        {
            int whole = numerator / denominator 
            System.out.println(numerator + " / " + denominator + " is an improper fraction and it can be reduced to " + whole);//Sample 3      
        }  
    }

}

1 个答案:

答案 0 :(得分:1)

在余数导致编译器抛出有关意外符号的错误之前,您错过了一个加法运算符。由于此处的错误,您的连接不起作用:

System.out.println(numerator + " / " + denominator + " is a improper fraction and it's mixed fraction is " + mix + " and " remainder + " / " + denominator);// Sample 1
                                                                                                                          ^

应该改为:

System.out.println(numerator + " / " + denominator + " is a improper fraction and it's mixed fraction is " + mix + " and " + remainder + " / " + denominator);// Sample 1

注意添加的+来解决问题,这是一个缺失的符号,导致连接失败。

工作示例:Here

修改

看到您编辑了代码,elseelse if语句是向后的。此外,else if没有条件。要检测分数是否可以简化为整数,请执行以下操作:

else if(numerator%denominator == 0)

这将评估numerator是否可被[{1}}整除,从而产生一个整数。