我不确定要添加到//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
}
}
}
答案 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
看到您编辑了代码,else
和else if
语句是向后的。此外,else if
没有条件。要检测分数是否可以简化为整数,请执行以下操作:
else if(numerator%denominator == 0)
这将评估numerator
是否可被[{1}}整除,从而产生一个整数。