问题描述
该程序的目的是获得一个分数的两个用户输入,从用户接收一个操作员,然后为另一个分数获得两个更多的用户输入。程序必须检查两个分数中使用的数字范围在0-99之间并且分母不为零。该程序还必须确保用户输入一个有效的运算符( - ,+,*,/)。我现在面临的唯一问题是没有我的变量正在初始化,我不知道如何使输出如下所示:
1 1 3
--- + --- = ---
4 8 8
这里是我到目前为止的代码,任何帮助将不胜感激,因为我对使用java的知识是最小的:
import java。 util.Scanner;
public class FractionCalculator {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
int n1;
int n2;
int d1;
int d2;
int n;
int d;
char o;
int m1,m2;
int tempN1,tempN2;
int lcm,x;
System.out.println(输入分子1的分子:);
n1 = in.nextInt();
System.out.println(输入分数1的分母:);
d1 = in.nextInt();
if(d1> 0){
System.out.println();
} else {
System.out.println(Invalid denominator);
System.exit(0);
}
System.out.println(Enter an operator:);
o = in.next()。toCharArray()[0];
System.out.println(输入分数2的分子:);
n2 = in.nextInt();
System.out.println(输入分数2的分母:);
d2 = in.nextInt();
if(d2> 0){
System.out.println();
} else {
System.out.println(Invalid denominator);
System.exit(0);
$ b switch(o){
case'*':
n = n1 * n2;
d = d1 * d2;
休息;
案例'/':
n = n1 * d2;
d = n2 * d1;
休息;
case'+':
int max = n1> d1?n1:d1;
int min = n1 for(int i = 1; i x = max * i;
if(x%min == 0)
lcm = x;
tempN1 = n1 * m1;
tempN2 = n2 * m2;
m1 = lcm / d1;
m2 = 1cm / d2;
n = tempN1 + tempN2;
d = lcm;
休息;
案例' - ':
n = tempN1 - tempN2;
d = lcm;
休息;
默认值:
System.out.println(非法操作符:+ o);
休息; }
}
}
更新:
请注意,这是我的不完整和/或最佳解决方案,但至少它应该带您进入正确的方向。你仍然必须减少分数并做一些其他的调整。
import java.math.BigInteger;
import java.util.Scanner;
public class FractionCalculator {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
int n1;
int n2;
int d1;
int d2;
int n = 0;
int d;
char o;
System.out.println(输入分子1的分子:);
n1 = in.nextInt();
System.out.println(输入分数1的分母:);
d1 = in.nextInt();
if(d1> 0){
System.out.println();
} else {
System.out.println(Invalid denominator);
System.exit(0);
}
System.out.println(Enter an operator:);
o = in.next()。toCharArray()[0];
System.out.println(输入分数2的分子:);
n2 = in.nextInt();
System.out.println(输入分数2的分母:);
d2 = in.nextInt();
if(d2> 0){
System.out.println();
} else {
System.out.println(Invalid denominator);
System.exit(0);
switch(o){
case'*':
n = n1 * n2;
d = d1 * d2;
break;
案例'/':
n = n1 * d2;
d = n2 * d1;
break;
case'+':
case' - ':
d = gcd(d1,d2);
n1 * = d / d1;
n2 * = d / d2;
if(o =='+'){
n = n1 + n2;
}
else if(o ==' - '){
n = n1 - n2;
}
break;
默认值:
System.out.println(非法操作符:+ o);
return;
}
System.out.printf(%d%d%d \ n,n1,n2,n);
System.out.printf(---%c --- = --- \\\
,o);
System.out.printf(%d%d%d \ n,d1,d2,d);
$ b $ private static int gcd(int d1,int d2){
BigInteger gcd = new BigInteger(String.valueOf(d1))。gcd(new BigInteger(String。的valueOf(D2)));
return gcd.intValue();
}
}
The purpose of the program is to get two user inputs for a fraction, receive a operator from the user, and then to get two more user inputs for a second fraction. The program must check that the numbers used in both fractions range between 0-99 and have a non-zero denominator. The program also has to make sure that the user inputs a valid operator (-,+,*,/).
The only problem I am facing now is that none of my variables are being initialized and that I don't know how to make the output look like so:
1 1 3
--- + --- = ---
4 8 8
Here is the code I have so far, any help would be much appreciated because my knowledge for using java is minimal:
import java.util.Scanner;
public class FractionCalculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n1;
int n2;
int d1;
int d2;
int n;
int d;
char o;
int m1,m2;
int tempN1, tempN2;
int lcm, x;
System.out.println("Enter a numerator for fraction 1: ");
n1 = in.nextInt();
System.out.println("Enter a denominator for fraction 1: ");
d1 = in.nextInt();
if (d1 > 0) {
System.out.println();
} else {
System.out.println("Invalid denominator");
System.exit(0);
}
System.out.println("Enter an operator: ");
o = in.next().toCharArray()[0];
System.out.println("Enter a numerator for fraction 2: ");
n2 = in.nextInt();
System.out.println("Enter a denominator for fraction 2: ");
d2 = in.nextInt();
if (d2 > 0) {
System.out.println();
} else {
System.out.println("Invalid denominator");
System.exit(0);
}
switch(o){
case '*':
n = n1 * n2;
d = d1 * d2;
break;
case '/':
n = n1 * d2;
d = n2 * d1;
break;
case '+':
int max=n1>d1?n1:d1;
int min=n1<d1?n1:d1;
for(int i=1;i<=min;i++)
x=max*i;
if (x%min==0)
lcm=x;
tempN1=n1*m1;
tempN2=n2*m2;
m1=lcm/d1;
m2=lcm/d2;
n = tempN1 + tempN2;
d = lcm;
break;
case '-':
n = tempN1 - tempN2;
d = lcm;
break;
default:
System.out.println("Illegal Operator: "+ o);
break; }
}
}
UPDATE:
Be aware that this is my no means complete and/or the best solution, but at least it should take you in the right direction. You will still have to reduce fractions and do some other tweaks.
import java.math.BigInteger;
import java.util.Scanner;
public class FractionCalculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n1;
int n2;
int d1;
int d2;
int n = 0;
int d;
char o;
System.out.println("Enter a numerator for fraction 1: ");
n1 = in.nextInt();
System.out.println("Enter a denominator for fraction 1: ");
d1 = in.nextInt();
if (d1 > 0) {
System.out.println();
} else {
System.out.println("Invalid denominator");
System.exit(0);
}
System.out.println("Enter an operator: ");
o = in.next().toCharArray()[0];
System.out.println("Enter a numerator for fraction 2: ");
n2 = in.nextInt();
System.out.println("Enter a denominator for fraction 2: ");
d2 = in.nextInt();
if (d2 > 0) {
System.out.println();
} else {
System.out.println("Invalid denominator");
System.exit(0);
}
switch (o) {
case '*':
n = n1 * n2;
d = d1 * d2;
break;
case '/':
n = n1 * d2;
d = n2 * d1;
break;
case '+':
case '-':
d = gcd(d1, d2);
n1 *= d / d1;
n2 *= d / d2;
if(o == '+') {
n = n1 + n2;
}
else if(o == '-') {
n = n1 - n2;
}
break;
default:
System.out.println("Illegal Operator: " + o);
return;
}
System.out.printf(" %d %d %d\n", n1, n2, n);
System.out.printf("--- %c --- = ---\n", o);
System.out.printf(" %d %d %d\n", d1, d2, d);
}
private static int gcd(int d1, int d2) {
BigInteger gcd = new BigInteger(String.valueOf(d1)).gcd(new BigInteger(String.valueOf(d2)));
return gcd.intValue();
}
}
这篇关于用于计算分数的Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!