gpt4 book ai didi

java - 需要帮助编译此迭代和接口(interface)程序

转载 作者:行者123 更新时间:2023-12-01 13:48:46 24 4
gpt4 key购买 nike

我正在尝试编写这个java程序,让用户输入两个有理数,并从选项菜单中要求计算某种函数A。我被困在几个地方,不知道还有什么去做。我需要一些指导。它不会编译。说构造函数 Rational 未定义,最后一个默认值是无效标签。它是两个类文件,驱动程序文件使用 Rational 文件。两个文件均已上传并以文本分隔。需要帮助

 import java.util.Scanner;
import java.util.*;

public class RationalDriver{
public static void main(String[] args){

int rationalNum1, rationalDen1, rationalNum2, rationalDen2;

Scanner in = new Scanner(System.in);
// first rational
System.out.println(" Input first rational number for the Numerator");
rationalNum1 = in.nextInt();
System.out.println(" Input first rational number for the Denominator");
rationalDen1 = in.nextInt();
if (rationalDen1 == 0){
System.out.println(" Cannont divide by zero ");
System.out.println(" please re enter another number ");
}
System.out.println("Rational Number #1 = ("+rationalNum1+"/"+rationalDen1+")");
//Displays 1st Rational Number

// second rational
System.out.println(" Input 2nd rational number for the 2nd Numerator");
rationalNum2 = in.nextInt();
System.out.println(" Input 2nd rational number for the 2nd Denominator");
rationalDen2 = in.nextInt();
if (rationalDen2 == 0){
System.out.println("Cannont divide by zero");
System.out.println(" please re enter another number");
}
System.out.println("Rational Number #2 = ("+rationalNum2+"/"+rationalDen2+")");
//Displays 2nd Rational Number

Rational r1 = new Rational ( rationalNum1, rationalDen1);
Rational r2 = new Rational ( rationalNum2, rationalDen2);

// System.out.println;//toString
}


public void display_menu() //menu options
{
System.out.print(" Enter the corresponding number for the desired action ");
System.out.println("1) Addition\n2) 2) Subtraction\n3) 3) Multiplication\n4) 4)Division\n5) 5) Test for Eqaulity\n6) 6) Change 1st rational number\n7) 7) Change 2nd rational number");
}
public void question()
{
System.out.println("Do you want to exit? [y/n]"); // ask user if they want to quit with yes or no option
Scanner q = new Scanner(System.in);
switch (q.nextInt())
{
case 'y' :
System.out.println ("Thank you and goodbye.");
break;

case 'n' :
InputMenu();
break;
default:

System.err.println ( "Unrecognized option" );
break;
}
}
public void InputMenu() // keys for the menu
{
Scanner in = new Scanner(System.in);
display_menu();
switch (in.nextInt())
{
case 1: //addition
System.out.println ( "1" );
System.out.println( "("+rationalNum1+"/"+rationalDen1+")"+ " + " + " ("+rationalNum2+"/"+rationalDen2+")" + "=" + r1.add(r2));
break;

case 2: //subtraction
System.out.println ( "2" );
System.out.println( "("+rationalNum1+"/"+rationalDen1+")"+ " - " + "("+rationalNum2+"/"+rationalDen2+")" + "=" + r1.subtract(r2));
break;

case 3: //mulitplication
System.out.println ( "3" );
System.out.println( "("+rationalNum1+"/"+rationalDen1+")"+ " * " + " ("+rationalNum2+"/"+rationalDen2+")" + "=" + r1.multiply(r2));
break;

case 4: //division
System.out.println ( "4" );
System.out.println( "("+rationalNum1+"/"+rationalDen1+")"+ " / " + "("+rationalNum2+"/"+rationalDen2+")" + "=" + r1.divide(r2));
break;

case 5: //compare to
System.out.println ( "5" );
question();
break;

case 6: //change the 1st Rational Number
System.out.println ( "6" );
Scanner in = new Scanner(System.in);
System.out.println(" Input first rational number for the Numerator");
rationalNum1 = in.nextInt();
System.out.println(" Input first rational number for the Denominator");
rationalDen1 = in.nextInt();
if (rationalDen1 == 0){
System.out.println(" Cannont divide by zero");
System.out.println(" please re enter another number");
}
break;

case 7: //change the 2nd Rational Number
System.out.println ( "7" );
System.out.println(" Input 2nd rational number for the 2nd Numerator");
rationalNum2 = in.nextInt();
System.out.println(" Input 2nd rational number for the 2nd Denominator");
rationalDen2 = in.nextInt();
if (rationalDen2 == 0){
System.out.println("Cannont divide by zero");
System.out.println(" please re enter another number");
break;


default:
System.err.println ( "Unrecognized option" );
break;
}
}
}
}

这是有理数的类文件

public class Rational{

private int Numerator;
private int Denominator;


//constructors
public Rational(){
Numerator = 1;
Denominator = 1 ;

}

//setters
//a-numerator
//b-denmonator
//c other.getNumerator
//d-other.getDenominator
public void add (Rational other){ // (ad + bc) / bd
Numerator = (Numerator*other.getDenominator() + Denominator*other.getNumerator());
Denominator = (Denominator*other.getDenominator());
//Normalize();
}
public void subtract (Rational other){ // (ad-bc) / bd
Numerator = (Numerator*other.getDenominator() - Denominator*other.getNumerator());
Denominator = (Denominator*other.getDenominator());
//Normalize();
}
public void multiply (Rational other){ // ac/db
Numerator = (Numerator*other.getNumerator() / other.getDenominator()* Denominator);
// Normalize();
}
public void divide (Rational other){//
}

public int getNumerator(){
return Numerator;
}

public int getDenominator(){
return Denominator;
}

//toString
//public String toString(){
//return toString()+ (rationalNum1 + "/" + rationalDen1);
}

最佳答案

在你的主类中,你有:

Rational r1 = new Rational ( rationalNum1, rationalDen1);
Rational r2 = new Rational ( rationalNum2, rationalDen2);

您正在将 2 个整数传递给接收 void 的构造函数,因此您必须像这样更改构造函数(Rational 类的):

 public Rational(int rationalNumber, int rationalDen){  
Numerator = rationalNumber;
Denominator = rationalDen;

}

希望它有帮助,让我知道它是否有效或者是否还有其他问题......

编辑:您的扫描仪和打印错误。你有这个:

public void question()
{
System.out.println("Do you want to exit? [y/n]"); // ask user if they want to quit with yes or no option
Scanner q = new Scanner(System.in);
switch (q.nextInt()) // here you are assuming that you are reading a int in step
{ // of a string
case 'y' : // ' ' arent used for strings...
System.out.println ("Thank you and goodbye.");
break;

case 'n' :
InputMenu();
break;
default:

System.err.println ( "Unrecognized option" );
break;
}
}

所以,你需要的是:

 public void question()
{
System.out.println("Do you want to exit? [y/n]"); // ask user if they want to quit with yes or no option
Scanner q = new Scanner(System.in);
switch (q.nextLine()) // change nextInt to nextLine, that is the string method
{ // of a string
case "y" : //change ' ' to " "
System.out.println ("Thank you and goodbye.");
break;

case "n" : //change ' ' to " "
InputMenu();
break;
default:

System.err.println ( "Unrecognized option" );
break;
}
}

希望有帮助:)

编辑3:

在你的代码中你有:` 公共(public)类 RationalDriver{ 公共(public)静态无效主(字符串[]参数){

 int rationalNum1, rationalDen1, rationalNum2, rationalDen2; // this are local variables, they only exist inside main method
...`
}

所以,你可以做的是:

 public class RationalDriver{
private static int rationalNum1, rationalDen1, rationalNum2, rationalDen2;
public static void main(String[] args){
// your main
}

我所做的就是获取仅存在于主程序中的局部变量,并将它们转换为全局变量,这样当您想要更改它们的值或给它们一个值时,您只需执行以下操作:

rationalNum1 = your valor;

请注意,如果您使用任何变量而不用值初始化它,您将得到一个空点异常...

关于java - 需要帮助编译此迭代和接口(interface)程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20136073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com