[英]Want to figure out what it wrong with my fraction to decimal converter in java
I am doing a project for class and I just wanted to find out what is wrong with my converter from fractions to decimal values? 我正在为一个班级做一个项目,我只是想找出从分数到十进制值的转换器有什么问题? And it has to be done with methods.
它必须通过方法来完成。
import java.util.Scanner;
public class Fraction
{
public static void main(String args[])
{
Scanner input = new Scanner( System.in );
int numerator, int denominator, result;
System.out.println ( "Please enter the numerator: " );
num = input.nextInt();
System.out.println ( "Please enter the denominator: " );
den = input.nextInt();
result = double(numerator/denominator);
}
public static int double( int x )
{
if (denominator == 0){
System.out.println ( "Can't divide by zero" );
}
else {
double fraction = (double)numerator / denominator;
System.out.println(fraction);
}
}
}
try this: 尝试这个:
import java.util.*;
public class Fraction {
method();
}
public void method () {
Scanner sc = new Scanner (System.in);
System.out.println("Please enter the numerator: ");
double numerator = sc.nextInt();
System.out.println("Please enter the Denominator: ");
double denominator = sc.nextInt();
if (denominator == 0) {
System.out.println("Can't divide by zero");
}
else {
double fraction = (double)numerator / denominator;
System.out.println(fraction);
}
}
Try this, instead of your code: 试试这个,而不是你的代码:
import java.util.Scanner;
public class Fraction
{
public static void main(String args[])
{
Scanner input = new Scanner( System.in );
int numerator, int denominator, result;
System.out.println ( "Please enter the numerator: " );
num = input.nextInt();
System.out.println ( "Please enter the denominator: " );
den = input.nextInt();
result = double(numerator/denominator);
}
public static int doublenumber( int x ) // you can't use double only because it is reserved word in java.
{
if (denominator == 0){
System.out.println ( "Can't divide by zero" );
}
else {
double fraction = (double)numerator / denominator;
System.out.println(fraction);
}
}
}
This should work: 这应该工作:
public static void main(String args[])
{
Scanner input = new Scanner( System.in );
int num, den;
System.out.println ( "Please enter the numerator: " );
num = input.nextInt();
System.out.println ( "Please enter the denominator: " );
den = input.nextInt();
if (den != 0 ){
System.out.println ("Decimal is " + convert (num, den));
}
else
System.out.println ("Cant divide by zero");
}
public static double convert(int x, int y)
{
return ((double)x/y);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.