我正在尝试让java函数接受一些输入,更改它并将其发回。通常你会返回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, 亚伦
答案 0 :(得分:0)
简短回答:
您需要在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");
并且主要方法中会显示更改。
但是,在您的情况下,这是不可能的,因为Java中的String
是不可变的。因此,您需要返回修改后的字符串并在main
方法中捕获返回的值。
此外,由于上述原因,您无需将nameChanged
传递到nameChanger
。
答案 1 :(得分:0)
由于Java方法调用始终是call-by-value,因此您无法通过在C中使用指针来更改当前块之外的变量(例如方法)。您需要使用返回值并分配它到变量
nameChanged = nameChanger(nameRaw, nameChanged);
或创建某种形式的holder对象,它在内部操作变量。
答案 2 :(得分:0)
您正尝试通过副作用更改变量。请注意,当您调用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;
}
答案 3 :(得分:0)
如果我理解正确,你想创建一个方法,它将一个字符串作为参数,处理它然后返回更改后的值。
你离这儿不远:
/**
* NameChanger method takes a string as a parameter
* and replaces its "s" with "z" then sends it back
*/
private static String nameChanger(String nameRaw) {
String cReturnedString = nameRaw;
if(nameRaw.contains("s")) {
cReturnedString = nameRaw.replace("s", "z");
}
return cReturnedString;
}
// From another method
String cMyNewString = nameChanger("sissy");
System.out.println(cMyNewString); //Outputs "zizzy"
我改变了什么:
删除了额外的参数 - 您无需传递您在方法参数中返回的字符串。
初始化返回的字符串,其值与nameRaw变量相同。如果您的nameRaw不包含" s",则您不希望返回null。
答案 4 :(得分:0)
您不会传递可能会发生变化的变量。而是将代码更改为main()
中的以下方法调用:
String nameChanged = nameChanger(nameRaw);
并在您实际创建它的方法中创建要返回的变量:
private static String nameChanger(String nameRaw) {
String nameChanged = null; //
// ...
// and then
return nameChanged;
}
答案 5 :(得分:0)
这一行:
nameChanger(nameRaw, nameChanged);
需要像:
nameChanged = nameChanger(nameRaw, nameChanged);
之所以出现这种情况,是因为Java是一个值传递。所以你传递String的值而不是它的引用。
如果你真的,真的希望能够改变nameChanged
; nameChanged
需要是一个(通常是可变的)Object,它具有适当的setter / getter而不是native。在这种情况下,您将传递值'对象引用的副本',它允许您在方法中更改对象的内容(如果它不是不可变的)
答案 6 :(得分:0)
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
我希望这会有所帮助
答案 7 :(得分:0)
谢谢大家的回答! 你所说的一切都帮助我解决了这个问题!
我的解决方案:nameChanged = nameChanger(nameRaw, nameChanged)
感谢各位帮助,我非常感谢您的时间和脑力,向我解释这个以及为什么!
谢谢和最诚挚的问候, 亚伦