我试图让一个java函数接受一些输入,更改它并将其发送回来。通常你会 return varName;或其他东西,但在这里它不起作用。我的问题是:如何创建一个自定义方法来接受变量“nameRaw”并在函数“nameChanger()”中更改它,然后将更改后的名称输出到变量:“nameChanged”。
我已经使用IDEONE.com来展示代码并对其进行处理,所以这里是链接:http://ideone.com/cdj6Cd
如果您不信任随机链接,这是完全可以理解的。因此,我也将其放在这里,但假设用户输入的唯一内容是“Test”。
代码:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception {
System.out.println("*INTENDED* Function of this-To create a custom method to take in a variable 'nameRaw' and change it within the function 'nameChanger()' then out put the changed name to variable: 'nameChanged'.\nProblem: the name changes within the function 'nameChanger()' only, therefore I cannot call the variable 'nameChanged' elsewhere, such as in main().........\n\n\n");
// Initalize the Java Scanner
Scanner in = new Scanner(System.in);
// Initalizes a completely empty string with name "nameChanged"
String nameChanged = null;
System.out.println("Please enter a username for yourself below:");
System.out.println("Test"); // Fake input to make me feel good I guess, it just looks better
// Sets "nameRaw" to "Test"
String nameRaw = in.nextLine();
// Spits out the untouched value that the user has entered
System.out.println("\nRaw name before passing to custom method: " + nameRaw);
// Puts "nameRaw" into the custom method "nameChanger()" to change the, "s" in, "Test" to a "z" by default
nameChanger(nameRaw, nameChanged);
// Spits out the touched and changed nameChanged variable from "nameChanger()"
if(nameChanged == null) {
System.out.println("\nHere is the failed changed name: " + nameChanged);
System.out.println("\nARE YOU KIDDING ME! WHY DOES THIS NOT WORK?!?! PLEASE HELP!!!!");
} else {
System.out.println("Here is the successfuly changed name: " + nameChanged);
System.out.println("\nWhoever solved this problem is a god..");
}
} // Closes method main()
// Custom method named "nameChanger" that will need a variable named "nameRaw" *which is set* within its () to function
private static String nameChanger(String nameRaw, String nameChanged) {
//// Initalizes a completely empty string with name "nameChanged"
//String nameChanged = null;
// States the set name *unchanged* from the main method on line 17
System.out.println("\t#\tName unchanged read and displayed by the nameChanger custom method: " + nameRaw);
// The name by default "Test" does contain an "s" so the if statement is satisfied
if(nameRaw.contains("s")) {
// The variable "nameRaw should be running through here and having it's "s" replaced with a "z"
nameChanged = nameRaw.replace("s", "z");
// Output the changed name *if all goes right*
System.out.println("\t#\tName changed *still in custom function* is: " + nameChanged);
// The else statement is just for testing purposes, such as changing "Test" as the input to "Demo" to dis-satisfy the if statemtent
} else {
System.out.println("\t#\tFor reference, here is the unchanged name raw: " + nameRaw);
System.out.println("\t#\tAlso for reference, here is the null value 'nameChanged': " + nameChanged);
}
// One more time to show my hate toward Java, output the changed variable "nameChanged". Take note that the "s" is a "z".....
System.out.println("\t#\tPlease don't be null or have an 's' in it: " + nameChanged);
// To output to main() that nameChanged is now not null *if all goes right*, but "Tezt" should be the output
return nameChanged;
} // Close custom method, nameChanger()
}
谢谢大家,希望这不会让你们不知所措:p, 亚伦
您不传递可能发生变化的变量。相反,请将代码更改为
main()
中的以下方法调用:
String nameChanged = nameChanger(nameRaw);
并在实际创建的方法中创建要返回的变量:
private static String nameChanger(String nameRaw) {
String nameChanged = null; //
// ...
// and then
return nameChanged;
}
这一行:
nameChanger(nameRaw, nameChanged);
需要类似于:
nameChanged = nameChanger(nameRaw, nameChanged);
之所以会出现这种情况,是因为Java是按值传递的。因此,您将传入字符串的值而不是其引用。
如果你真的、真的希望能够改变
nameChanged
; nameChanged
需要是一个(通常是可变的)对象,具有适当的 setter/getter 而不是基元。在这种情况下,您将按值传递“对象引用的副本”,这允许您在方法中更改对象的内容(如果它不是不可变的)
简短回答: 您需要在您的
main
方法中执行此操作:
nameChanged = nameChanger(nameRaw, nameChanged);
说明:如果在不同的方法中更改对象引用指向的值,则调用方法将看不到更改。在您的情况下,
nameChanged
在 main
方法中声明,并在 nameChanger
方法中重新分配,因此,更改在 main
方法中不可见。
但是,您可以通过调用对象的可变方法来修改对象:
// in the main method
SomeStudent s = new SomeStudent();
modifyStudent(s);
// inside modifyStudent method
// change will be visible in the main method
s.setResult("PASS");
并且更改将在 main 方法中可见。
但是,在您的情况下这是不可能的,因为 Java 中的
String
是不可变的。因此,您需要返回修改后的字符串并在 main
方法中捕获返回值。
此外,由于上述原因,您不需要将
nameChanged
传递到 nameChanger
。
private static String nameChanger(String nameRaw, String nameChanged)
在这个方法中,你为什么要传递nameChanged
?我认为 nameChanged
应该是 nameChanger
方法结束时返回的值。
我不明白为什么你要检查
nameChanged
是否为空,但你将其初始化为 null
并且没有更改它。
String nameChanged = null;
if(nameChanged == null) {
System.out.println("\nHere is the failed changed name: " + nameChanged);
System.out.println("\nARE YOU KIDDING ME! WHY DOES THIS NOT WORK?!?! PLEASE HELP!!!!");
} else {
System.out.println("Here is the successfuly changed name: " + nameChanged);
System.out.println("\nWhoever solved this problem is a god..");
}
如果您仍然按照您的方法,请尝试
nameChanged = nameChanger(nameRaw, nameChanged)
而不是nameChanged = null
我希望这有帮助
由于在 Java 中方法调用始终是按值调用,因此您无法像在 C 中使用指针那样更改当前块(例如方法)之外的变量。您需要使用返回值并分配它到变量
nameChanged = nameChanger(nameRaw, nameChanged);
或创建某种形式的持有者对象,在内部操作变量。
您试图通过副作用来更改变量。请注意,当您调用
nameChanger()
时,您实际上并未将返回值分配给变量。
String result = nameChanger(nameRaw)
是一种更直接的方法。通常不希望依赖副作用。请注意,我还更改了方法签名,因为您现在不需要向其传递想要存储结果的变量,您可以在方法的范围内创建它。
public static String nameChanger(String nameRaw) {
String result = "";
// do stuff
if(nameRaw.contains("s")) {
result = nameRaw.replace("s", "z");
}
//more stuff and System.out.println() statements
return result;
}
谢谢所有回答的人! 你所说的一切都帮助我解决了这个问题!
我的解决方案:
nameChanged = nameChanger(nameRaw, nameChanged)
谢谢你们的帮助,我非常感谢你们花时间和脑力向我解释这一点以及原因!
谢谢并致以最诚挚的问候, 亚伦