gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:11:29 26 4
gpt4 key购买 nike

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);




}

}

我如何将这些方法调用到一个新类中,以便我可以从中运行程序?我知道通常你会把 class.nameofmethod(); 的名字放在但我将如何传递参数等?我是 Java 的初学者,所以非常感谢所有帮助!提前谢谢大家。

最佳答案

举个例子:

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

...将 num1() 方法的结果存储在 x 中,然后打印出 x 的内容。

在参数方面:

calling.calculation(4,5,"+");

您需要问这个问题的事实表明您可能应该离开并阅读并遵循一些非常基础的 Java 教程,例如此处的教程:http://download.oracle.com/javase/tutorial/getStarted/index.html - 这根本不是攻击,只是关于如何最好地扩展您的知识的建议。

关于java - 我将如何在另一个类中调用以下方法? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7940571/

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