如何将字符串值传递给 Java 中的另一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29155581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass a String value to another class in Java
提问by Colin Koenig
Currently, I am running into a problem in my Java code. I am somewhat new to Java, so I would love it if you kept that in mind.
目前,我在我的 Java 代码中遇到了一个问题。我对 Java 有点陌生,所以如果你记住这一点,我会喜欢它。
My problem is with passing a String value from one class to another.
我的问题是将字符串值从一个类传递到另一个类。
Main Class:
主要类:
private static void charSurvey()
{
characterSurvey cSObj = new characterSurvey();
cSObj.survey();
System.out.println();
}
Second:
第二:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class characterSurvey
{
public void survey(String character)
{
Scanner s = new Scanner(System.in);
int smartChina = 0,smartAmerica = 0,dumbAmerica = 0;
String answer;
System.out.println("Are you good with girls?");
System.out.println("y/n?");
answer = s.nextLine();
if(answer.equalsIgnoreCase("y"))
{
smartChina = smartChina - 3;
smartAmerica = smartAmerica + 2;
dumbAmerica = dumbAmerica + 4;
}
//...
//ASKING SEVERAL OF ABOVE ^
List<Integer> charSelect = new ArrayList<Integer>();
charSelect.add(smartChina);
charSelect.add(smartAmerica);
charSelect.add(dumbAmerica);
Collections.sort(charSelect);
Collections.reverse(charSelect);
int outcome = charSelect.get(0);
if(smartChina == outcome)
{
character = "smartChina";
}
else if(smartAmerica == outcome)
{
character = "smartAmerica";
}
else if(dumbAmerica == outcome)
{
character = "dumbAmerica";
}
System.out.println(character);
s.close();
}
}
When I call the first class I am trying to grab the value of the second.
当我打电话给第一堂课时,我试图抓住第二堂课的价值。
Disclaimer* the strings in this class were not meant to harm anyone. It was a joke between myself and my roommate from China, thanks.
免责声明* 本课程中的字符串无意伤害任何人。这是我和我来自 CN 的室友之间的笑话,谢谢。
采纳答案by Ioannis Stefanou
There are several mistakes here.
这里有几个错误。
First off, in your main class as you write you call the method survey()
on the CharacterSurvey
object but the survey itself the way it is implemented needs a String parameter to work
首先,在编写时在主类中调用对象survey()
上的方法,CharacterSurvey
但调查本身的实现方式需要一个 String 参数才能工作
public void survey(String character)
Also this method returns void. If you want somehow to grab a string out of that method you need to declare the method as
此方法也返回void。如果您想以某种方式从该方法中获取一个字符串,则需要将该方法声明为
public String survey() {}
this method returns a string now.
此方法现在返回一个字符串。
If i were to give a general idea, declare a String
variable in the second class which will be manipulated inside the survey method and once the survey is declared as a String
method return the value at the end inside the method.
如果我要给出一个总体思路,请String
在第二个类中声明一个变量,该变量将在调查方法内进行操作,一旦将调查声明为String
方法,则返回方法内最后的值。
By doing that you'll be able to receive the String value by calling the method on the characterSurvey
object (and of course assign the value to a string variable or use it however).
通过这样做,您将能够通过调用characterSurvey
对象上的方法来接收字符串值(当然,也可以将该值分配给字符串变量或使用它)。
Hope this helped
希望这有帮助
回答by Vince Emigh
It seems as if you want to obtain the character in your main class after the survey has completed, so it can be printed out in the main method.
好像你想在调查完成后在你的主类中获取字符,所以它可以在main方法中打印出来。
You can simply change your void survey
method to a String survey
method, allowing you to returna value when that method is called:
您可以简单地将您的void survey
方法更改为一个String survey
方法,允许您在调用该方法时返回一个值:
class CharacterSurvey {
public String takeSurvey() {
//ask questions, score points
String character = null;
if(firstPerson == outcome) {
character = "First Person";
}
return character;
}
}
Now, when you call this method, you can retrieve the value returned from it:
现在,当您调用此方法时,您可以检索从它返回的值:
class Main {
public static void main(String[] args) {
CharacterSurvey survey = new CharacterSurvey();
String character = survey.takeSurvey();
System.out.println(character);
}
}