简体   繁体   English

我怎样才能得到从另一个类返回的整数?

[英]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.

相关问题 我如何从另一个班级获得价值 - How can i get a value to class from another class 我如何从另一个人那里获得行动课的价值 - How can I get a value of an action class from another 如何从Java中的另一个类获取变量? - How can I get a variable from another class in Java? 我如何才能返回主班? - How can I get my main class returned? 如何将我从 class 获得的值传递给另一个 class? - How can I pass the value I get from a class to another class? 如何使用另一个类中的int,对其进行更改并在原始类中返回更改后的int - How to use an int from another class, change it and return changed ints in the original class 如何获取返回结果的服务器的数据,我想获取该结果以更新到另一台服务器 - How can I get the data of a server that returned the result, I want to get that result to update to another server 如何从另一个类的静态方法获取抽象类的子类的实例? - How can I get an instance of a subclass of abstract class from a static method of another class? 如何获得一个类,以从位于另一个类中的对象的数组列表中打印出字符串变量? - How can I get a class to print out a string variable from an arraylist of objects located in another class? 我无法使int成为数组的要素 - I can not get the ints to be elemets of the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM