Java中的方法/函数
对于给定的代码,我得到了指导:Java允许我们使用方法/函数,这样我们可以存储我们可能多次使用的过程,因此我希望您在方法/函数中有常见任务时更新代码。你知道怎么做吗Java中的方法/函数,java,args,Java,Args,对于给定的代码,我得到了指导:Java允许我们使用方法/函数,这样我们可以存储我们可能多次使用的过程,因此我希望您在方法/函数中有常见任务时更新代码。你知道怎么做吗 package fifthAssignment; public class Arithmetic { public static void main(String[] args) { // setting up the variable firstNumber and secondNumber
package fifthAssignment;
public class Arithmetic {
public static void main(String[] args) {
// setting up the variable firstNumber and secondNumber
int length = args.length;
if (length != 3) {
System.out.println("Your suppose to enter an int, int then an operation sign like +,-,X or /.");
return;
}
int firstNumber = Integer.parseInt(args[0]);
int secondNumber = Integer.parseInt(args[1]);
int addition = firstNumber + secondNumber;
int minus = firstNumber - secondNumber;
int division = firstNumber / secondNumber;
int multiply = firstNumber * secondNumber;
String arithmetic = args[2];
if (arithmetic.equals("+")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + addition);
} else if (arithmetic.equals("-")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + minus);
} else if (arithmetic.equals("/")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + division);
// I could not use "*" operator as it was looking to pull down all
// the files associated with this program instead of
// using it the way I intended to use it. So in this case I changed
// the "*" to "x" so that I can get the solution you
// were looking for.
} else if (arithmetic.equals("x")) {
System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + multiply);
}
// following prints out to the console what the length of each argument
// is.
System.out.println(args[0] + " has the length of " + args[0].length());
System.out.println(args[1] + " has the length of " + args[1].length());
if (arithmetic.equals("+")) {
int total = String.valueOf(addition).length();
System.out.println(addition + " has the length of " + total);
}else if (arithmetic.equals("-")) {
int total = String.valueOf(minus).length();
System.out.println(minus + " has the length of " + total);
}else if (arithmetic.equals("/")) {
int total = String.valueOf(division).length();
System.out.println(division + " has the length of " + total);
} else if (arithmetic.equals("x")) {
int total = String.valueOf(multiply).length();
System.out.println(multiply + " has the length of " + total);
}
}
}
我会提供一个单独的例子,但你应该自己做 您的代码中包含以下内容:
System.out.println(addition + " has the length of " + total);
相反,您可以创建一个可能使用两个int的方法:
public void printStatus(int check, int length) {
System.out.println(check + " has the length of " + length);
}
这样你就可以打电话了
printStatus(addition, total);
这只是一个粗略的示例,但您可以将一个代码过程包装到一个方法中,并将执行该方法所需的必要参数传递给它。这是基本Java。你可能需要通过一个基本的教程来了解你自己是如何做到这一点的。我想让你更新你的代码,在一个方法/函数中可以完成一些常见的任务。你如何做到这一点呢?另外,请注意,Java不是函数,而是方法。对不起,但是你的问题很懒。将来,如果你先尝试解决问题,你会得到更好的帮助。如果你不这样做,我们怎么知道你被困在哪里或是什么让你困惑?此外,你的努力也会在尊重你和你的问题方面获得很大的收获,表明你愿意付出努力和主动,首先尝试自己解决问题。我已经通过在方法中添加你的操作逻辑重新发布了解决方案。这就是我们实现模块化的方法。我修复了代码,但这与将一整块代码放到其他地方没什么不同。它不是真正的模块化,只是一分为二好吧,所以我有代码,我正在处理一些事情,试着让它工作。我现在的问题是,我无法访问performOperation方法中的变量加法、减法、除法等。我很抱歉,如果这是青少年或容易java,但我有一个问题。
package fifthAssignment;
public class Arithmetic {
public static void main(String[] args) {
// setting up the variable firstNumber and secondNumber
int length = args.length;
if (length != 3) {
System.out.println("Your suppose to enter an int, int then an operation sign like +,-,X or /.");
return;
}
int firstNumber = Integer.parseInt(args[0]);
int secondNumber = Integer.parseInt(args[1]);
int addition = firstNumber + secondNumber;
int minus = firstNumber - secondNumber;
int division = firstNumber / secondNumber;
int multiply = firstNumber * secondNumber;
String arithmetic = args[2];
// following prints out to the console what the length of each argument
// is.
System.out.println(args[0] + " has the length of " + args[0].length());
System.out.println(args[1] + " has the length of " + args[1].length());
performOperation(arithmetic);
}
public void performOperation(String arithmetic) {
if (arithmetic.equals("+")) {
int total = String.valueOf(addition).length();
System.out.println(addition + " has the length of " + total);
} else if (arithmetic.equals("-")) {
int total = String.valueOf(minus).length();
System.out.println(minus + " has the length of " + total);
} else if (arithmetic.equals("/")) {
int total = String.valueOf(division).length();
System.out.println(division + " has the length of " + total);
} else if (arithmetic.equals("x")) {
int total = String.valueOf(multiply).length();
System.out.println(multiply + " has the length of " + total);
}
}
}