[英]How can I get the ints returned from another class?
I have a project in a computer math course where we need to take input for 3 coefficients (ax^2 + bx + c) and use the quadratic formula to calculate the zeroes.我在计算机数学课程中有一个项目,我们需要输入 3 个系数 (ax^2 + bx + c) 并使用二次公式计算零。 it wouldn't be super hard for us, but we have to split the work into different resource classes and a driver class.
这对我们来说不会太难,但我们必须将工作拆分为不同的资源类和驱动程序类。
outline: https://imgur.com/a/0Mn3m大纲: https : //imgur.com/a/0Mn3m
so far this is what I have so far到目前为止,这是我迄今为止所拥有的
import java.util.Scanner;
public class coEff
{
public int coEff()
{
int a, b, c;
double root1, root2, root3, d;
Scanner s = new Scanner(System.in);
System.out.print("Enter the A coefficient");
a = s.nextInt();
System.out.print("Enter the B coefficient");
b = s.nextInt();
System.out.print("Enter the C coefficient");
c = s.nextInt();
return coEff();
}
}
second class:第二类:
import java.util.Scanner;
public class calcZeroes
{
public int calcZeroes()
{
coEff coEff = new coEff();
coEff.coEff();
int a, b, c;
double root1, root2, root3, d;
return calcZeroes();
}
}
I have the code to calculate the formula, but it doesn't work because the coefficients aren't initialized.我有计算公式的代码,但它不起作用,因为系数没有初始化。 any help would be appreciated :)
任何帮助,将不胜感激 :)
You're returning your classname at the end of each class.您将在每堂课结束时返回您的班级名。 It's just going to be recursively calling your class.
它只会递归地调用你的类。 (Might even give an error, i've not tried it before).
(甚至可能会出错,我以前没有尝试过)。 You should only return an int value in your case.
在您的情况下,您应该只返回一个 int 值。 For example
例如
`int something = 0;
return something;`
I assume you are new to java because you whole class structure is out of order.我假设您是 Java 新手,因为您的整个类结构都是乱序的。 for starters as Bami suggested you are returning class name.
对于初学者,Bami 建议您返回班级名称。 In java the concept of 'polymorphism' describes a language's ability to process objects of various types and classes through a single, uniform interface.
在 Java 中,“多态”的概念描述了一种语言通过单个统一接口处理各种类型和类对象的能力。 So you can make coEff class and do the calculation there and take the user input in driver class.
所以你可以创建 coEff 类并在那里进行计算并在驱动程序类中获取用户输入。 you can pass the variables from driver class to coEff method as parameter.
您可以将驱动程序类中的变量作为参数传递给 coEff 方法。
import java.util.Scanner;
public class coEff{
public int coEff(int a, int b, int c){
int result;
//do the calculation here.
return result;
}
}
In your driver class you take the input and use the coEff object to pass those input as parameter for you coEff method.在您的驱动程序类中,您获取输入并使用 coEff 对象将这些输入作为参数传递给您的 coEff 方法。
import java.util.*;
public class Driver{
public static void main(String [] args){
int a,b,c;
Scanner input = new Scanner(System.in);
System.out.println("Enter co-efficient A:\n");
a = input.nextInt();
System.out.println("Enter co-efficient B:\n");
b = input.nextInt();
System.out.println("Enter co-efficient C:\n");
c = input.nextInt();
coEff coeff = new coEff();
System.out.println(coEff.coEff(a,b,c));
}
}
As a side note: don't use the same name for a class and the method unless it's a constructer.附带说明:不要对类和方法使用相同的名称,除非它是构造函数。
EDIT: if you are required to take the input in a separate class, you can still use the same concept, instead of taking the input in driver just take it in a different class and call the coEff method and print the result in driver.编辑:如果您需要在单独的类中获取输入,您仍然可以使用相同的概念,而不是在驱动程序中获取输入,只需将其放入不同的类中并调用 coEff 方法并在驱动程序中打印结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.