我如何在另一个类中调用以下方法?JAVA

我如何在另一个类中调用以下方法?JAVA,java,Java,如何将这些方法调用到一个新类中,以便从中运行程序?我知道您通常会将类的名称放入。nameofmethod();但是我如何传递参数等等呢? 我是一名Java初学者,因此非常感谢所有帮助! 提前感谢各位。因为所有方法都是无参数的,所以没有参数可传递。您要做的是接受从方法返回的int: import java.io.IOException; import java.util.*; public class calling { public static int num1() { int x; S

如何将这些方法调用到一个新类中,以便从中运行程序?我知道您通常会将类的名称放入。nameofmethod();但是我如何传递参数等等呢? 我是一名Java初学者,因此非常感谢所有帮助!
提前感谢各位。

因为所有方法都是无参数的,所以没有参数可传递。您要做的是接受从方法返回的int:

import java.io.IOException;
import java.util.*;

public class calling {


public static int num1() {
int x;
Scanner scanner = new Scanner (System.in);
System.out.println("Please enter a number called x: ");
x=scanner.nextInt();
return x;    
}

public static int num2() {
int y;
Scanner scanner = new Scanner (System.in);
System.out.println("Please enter a second number called y: ");
y=scanner.nextInt();
return y;    
}

public static String operand() {
Scanner input = new Scanner (System.in);
String s;
System.out.println("What process would you like to do? *, /, + or - ?");
s=input.next();
return s;
}

public static void calculation(int x, int y, String s) {
String t;
Scanner input = new Scanner(System.in);


if (s.equals("*")) {
System.out.println("\nThe product of these numbers is:" + (x*y));}
else 
if (s.equals("+")) {
System.out.println("\nThe sum of these numbers is: " + (x+y));}

System.out.println("\nDo you want x or y to be the dividor/subtractor?: ");
t=input.next();

if (t.equals("y") || t.equals("Y") ) {

if (s.equals("/")) {
System.out.println("\nThe quotient of these numbers is:  " + (x/y));}
else 
if (s.equals("-")) {
System.out.println("\nThe difference of these numbers is: " + (x-y));}}

else 
if (t.equals("x") || t.equals("X")){

if (s.equals("/")) {
System.out.println("\nThe quotient of these numbers is: " + (y/x));}
else 
if (s.equals("-")) {
System.out.println("\nThe difference of these numbers is: " + ((y-x)));}}
}

public static void  main (String [] args) throws IOException {


int x1=num1();
int y1=num2();
String s1=operand();
calculation(x1, y1, s1);




}

}

编辑:除了您的计算方法,但您似乎知道如何使用它。现在我不确定您的问题是什么,因为您在主方法中使用的方法是OK(除非您不是从类名中调用它们,但如果主方法在类本身内部,则不必这样做)。

没有要传递的参数,因为您的所有方法都是无参数的。您要做的是接受从方法返回的int:

import java.io.IOException;
import java.util.*;

public class calling {


public static int num1() {
int x;
Scanner scanner = new Scanner (System.in);
System.out.println("Please enter a number called x: ");
x=scanner.nextInt();
return x;    
}

public static int num2() {
int y;
Scanner scanner = new Scanner (System.in);
System.out.println("Please enter a second number called y: ");
y=scanner.nextInt();
return y;    
}

public static String operand() {
Scanner input = new Scanner (System.in);
String s;
System.out.println("What process would you like to do? *, /, + or - ?");
s=input.next();
return s;
}

public static void calculation(int x, int y, String s) {
String t;
Scanner input = new Scanner(System.in);


if (s.equals("*")) {
System.out.println("\nThe product of these numbers is:" + (x*y));}
else 
if (s.equals("+")) {
System.out.println("\nThe sum of these numbers is: " + (x+y));}

System.out.println("\nDo you want x or y to be the dividor/subtractor?: ");
t=input.next();

if (t.equals("y") || t.equals("Y") ) {

if (s.equals("/")) {
System.out.println("\nThe quotient of these numbers is:  " + (x/y));}
else 
if (s.equals("-")) {
System.out.println("\nThe difference of these numbers is: " + (x-y));}}

else 
if (t.equals("x") || t.equals("X")){

if (s.equals("/")) {
System.out.println("\nThe quotient of these numbers is: " + (y/x));}
else 
if (s.equals("-")) {
System.out.println("\nThe difference of these numbers is: " + ((y-x)));}}
}

public static void  main (String [] args) throws IOException {


int x1=num1();
int y1=num2();
String s1=operand();
calculation(x1, y1, s1);




}

}
编辑:除了您的计算方法,但您似乎知道如何使用它。现在我不确定您的问题到底是什么,因为您在主方法中使用的方法是OK(除非您不是从类名中调用它们,但如果主方法在类本身内部,则不必这样做)。

例如:

int myResult = Myclass.MyMethod(); 
…将
num1()
方法的结果存储在x中,然后打印出x的内容

在参数方面:

int x = calling.num1();
System.out.println(x);
您需要问这个问题的事实表明,您可能应该走开,阅读一些非常基本的Java教程,如下面的一个:-这根本不是攻击,只是建议如何最好地拓宽您的知识面。

例如:

int myResult = Myclass.MyMethod(); 
…将
num1()
方法的结果存储在x中,然后打印出x的内容

在参数方面:

int x = calling.num1();
System.out.println(x);

您需要问这个问题的事实表明,您可能应该走开,阅读一些非常基本的Java教程,如下面的一个:-这根本不是攻击,只是建议如何最好地拓宽您的知识面。

由于所有方法都是静态的,您只需执行以下操作:

calling.calculation(4,5,"+");
但是,如果您没有使它们成为静态的,并且您想要调用它们,那么您可以实例化一个类并调用该类中的方法

int x1=calling.num1();
int y1=calling.num2();
String s1=calling.operand();
calling.calculation(x1, y1, s1);

由于所有方法都是静态的,因此您只需执行以下操作:

calling.calculation(4,5,"+");
但是,如果您没有使它们成为静态的,并且您想要调用它们,那么您可以实例化一个类并调用该类中的方法

int x1=calling.num1();
int y1=calling.num2();
String s1=calling.operand();
calling.calculation(x1, y1, s1);

你有比这更大的问题。
请开始阅读Robert C.Martin的《清洁代码》一书,你的代码不是面向对象的

你有比这更大的问题。
请开始阅读Robert C.Martin的《清洁代码》一书,你的代码不是面向对象的。。。传递参数,如StaticCalculatorClass.calculation(5,10,“wat”)。但这种方法几乎肯定是错误的;您传入一个字符串,然后立即用扫描仪的输入覆盖它。您还希望这些方法是非静态的,因此它们是实例方法您可以通过。。。传递参数,如
StaticCalculatorClass.calculation(5,10,“wat”)
。但这种方法几乎肯定是错误的;您传入一个字符串,然后立即用扫描仪的输入覆盖它。您还希望这些方法是非静态的,因此它们是实例methodscalculation(),接受参数;)@贝瑞120:是的,我注意到了,并编辑了我的答案。顺便说一句,你的回答不错。calculation()接受参数;)@贝瑞120:是的,我注意到了,并编辑了我的答案。顺便说一句,你的回答很好。调用。计算(x1,y1,s1);在括号中给出一个错误:s调用.计算(x1,y1,s1);给我一个括号上的错误:S