gpt4 book ai didi

Java错误: The method add(String) is undefined for the type String

转载 作者:行者123 更新时间:2023-12-01 19:29:49 25 4
gpt4 key购买 nike

每当我尝试运行我的代码时,都会收到错误“String 类型的方法 add(String) 未定义”、“String 类型的方法 minus(String) 未定义”等等。乘法和除法。我不知道我的其余代码是否正确运行,因为我无法运行实际的程序。为什么我会收到此错误?

主要:

import java.util.*;

public class RationalNumberMain
{

public static void main(String[] args)
{

Scanner s = new Scanner(System.in);

System.out.print("Enter first fraction here: ");
String r1 = s.nextLine();

System.out.print("Enter second fraction here: ");
String r2 = s.nextLine();

RationalNumber x = new RationalNumber(r1);
RationalNumber y = new RationalNumber(r2);

System.out.println("Rational Number 1 is " + r1);
System.out.println("Rational number 2 is " + r2);
System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2));
System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2));
System.out.println(r1 + " / " + r2 + " = " + r1.divide(r2));
}
}

类别:

public class RationalNumber 
{

private String fraction;
private String s1;
private String s2;
private int num;
private int den;
private int combineNum;
private int combineDen;
private String finalFraction;
private int divideNum;
private int divideDen;


public RationalNumber(String userFraction)
{
fraction = userFraction;
s1 = "";
s1 = "";
num = 0;
den = 0;
combineNum = 0;
combineDen = 0;
finalFraction = "";
divideNum = 0;
divideDen = 0;
}

public String toString()
{
return fraction;
}

public void seperate()
{
s1 = fraction.substring(0,1);
s2 = fraction.substring(2);
num = Integer.parseInt(s1);
den = Integer.parseInt(s2);

}

public int getNum()
{
return num;
}

public int getDen()
{
return den;
}

public String add(RationalNumber x)
{
combineDen = den * x.getDen();
combineNum = (num * x.getDen()) + (x.getNum() * den);
finalFraction = String.valueOf(combineDen) + "/" + String.valueOf(combineNum);
return finalFraction;
}

public String subtract(RationalNumber x)
{
combineDen = den * x.getDen();
combineNum = (num * x.getDen()) - (x.getNum() * den);
finalFraction = String.valueOf(combineDen) + "/" + String.valueOf(combineNum);
return finalFraction;
}

public String multiply(RationalNumber x)
{
combineDen = den * x.getDen();
combineNum = num * x.getNum();
finalFraction = String.valueOf(combineDen) + "/" + String.valueOf(combineNum);
return finalFraction;
}

public String divide(RationalNumber x)
{
divideNum = x.getDen();
divideDen = x.getNum();
combineDen = den * divideDen;
combineNum = num * divideNum;
finalFraction = String.valueOf(combineDen) + "/" + String.valueOf(combineNum);
return finalFraction;
}

}

最佳答案

r1 的类型为 String:

String r1 = s.nextLine();

您希望它添加 RationalNumber 类型的对象(从 r1 创建):

RationalNumber x = new RationalNumber(r1);

即:

x.add(y) 而不是 r1.add(r2)

代码中的其他算术运算也是如此。

关于Java错误: The method add(String) is undefined for the type String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60100011/

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