如何 return 从静态 void 方法到 main 方法的 int 数据?
How do I return an int data from a static void method to main method?
所以一切正常,但我唯一的问题是我不确定如何从我的 speakLine()
方法中 return 一段数据。我正在尝试打印我从一组问题中得到的正确答案的数量。
像这样:您在 3 道题中答对了“2”道。
Public static void main(String[] args) throws Exception {
int cal;
int i;
for (i = 0; i < 3; i++) {
int num1 = randomA();
int num2 = randomB();
cal = num1 + num2;
speakLines(num1, num2, cal);
}
我需要来自 speakLine 方法 () 的 "count" int:
System.out.println("You got " + count + "of " + i + "right");
} // end main()
这是方法
public static void speakLines(int num1, int num2, int cal) {
int count = 0;
Scanner scanner = new Scanner(System.in);
Voice voice;
// set up a Voicemanager object and use it to link voice with a particular voice
VoiceManager voiceManager = VoiceManager.getInstance();
voice = voiceManager.getVoice("kevin16");
// load the selected voice
voice.allocate();
// begin speaking the text
System.out.println("what is " + num1 + " + " + num2 + ":");
voice.speak("what is " + num1 + " + " + num2 + ":");
//talk
System.out.println("Please enter answer");
int answer = scanner.nextInt();
//talk
if (answer == cal) {
System.out.println("That's right");
voice.speak("That's right");
count += 1;
"Count +=1" 以上 ^ -- 我需要从 main() 方法打印这个计数整数。这是我试图 return 到主要方法的数据 "count"。这告诉我用户答对了多少答案。
} else {
System.out.println("Sorry, the answer is" + cal);
voice.speak("Sorry, the answer is " + cal);
}
} // end speakLines()
public static int randomA() {
int A;
A = 1 + (int) (Math.random() * 10);
return A;
}
public static int randomB() {
int B;
B = 1 + (int) (Math.random() * 5);
return B;
}
// end randomB()
} // end class
现在我知道 static void 方法不 return 数据。但我知道应该有一种方法可以 return 特定数据。
声明一个class变量public static int count;
来替换speakLines
方法中的局部变量count
。
这样做之后,您应该可以在 main 方法中访问和打印 score
。
return 代码可能不是你想要的。您可能希望将答案写入标准输出。这样其他一些程序就可以接收它。 System.out
上有多种方法可以写入标准输出。
如果要将 java 程序用作程序链的一部分,请使用标准输入和标准输出。
System.out.println
导致程序发送输出。这可以被另一个程序读取。例如,您可以 运行 并将其输出重定向到文件:java -jar yourjar > out.txt
您还可以使用管道字符将此输出发送到另一个程序:
java -jar yourJar | grep of
我不知道你想对输出做什么。
您可以在包含 class(主函数上方的那个)上使用静态文件,并在 speakLines 的开头重置它。
通常你不需要静态的,但因为 main 是一个静态方法,它不能访问包含 class.
的非静态字段
或
使用一个 class 参数来保存 int 值,并在 speakLines 的开头重置值。
在@DizzyCode 的帮助下,怎么样-
1. 在第2行
声明public static int count
2. 在第9行
中初始化变量count
3. 在方法 'speakLines'
中注释局部变量 'count' 的初始化
//******************************
public static int count;
Public static void main(String[] args) throws Exception {
int cal;
int i;
//set count to 0 before the loop starts
count=0;
for (i = 0; i < 3; i++) {
int num1 = randomA();
int num2 = randomB();
cal = num1 + num2;
speakLines(num1, num2, cal);
}
System.out.println("You got " + count + "of " + i + "right");
} // end main()
public static void speakLines(int num1, int num2, int cal) {
//**************************************
//make this count invalid and create a public static count variable before main method
// int count = 0;
Scanner scanner = new Scanner(System.in);
Voice voice;
// set up a Voicemanager object and use it to link voice with a particular voice
VoiceManager voiceManager = VoiceManager.getInstance();
voice = voiceManager.getVoice("kevin16");
// load the selected voice
voice.allocate();
// begin speaking the text
System.out.println("what is " + num1 + " + " + num2 + ":");
voice.speak("what is " + num1 + " + " + num2 + ":");
//talk
System.out.println("Please enter answer");
int answer = scanner.nextInt();
//talk
if (answer == cal) {
System.out.println("That's right");
voice.speak("That's right");
count += 1;}
else {
System.out.println("Sorry, the answer is" + cal);
voice.speak("Sorry, the answer is " + cal);
}
} // end speakLines()
public static int randomA() {
int A;
A = 1 + (int) (Math.random() * 10);
return A;
}
public static int randomB() {
int B;
B = 1 + (int) (Math.random() * 5);
return B;
}
// end randomB()
} // end class
所以一切正常,但我唯一的问题是我不确定如何从我的 speakLine()
方法中 return 一段数据。我正在尝试打印我从一组问题中得到的正确答案的数量。
像这样:您在 3 道题中答对了“2”道。
Public static void main(String[] args) throws Exception {
int cal;
int i;
for (i = 0; i < 3; i++) {
int num1 = randomA();
int num2 = randomB();
cal = num1 + num2;
speakLines(num1, num2, cal);
}
我需要来自 speakLine 方法 () 的 "count" int:
System.out.println("You got " + count + "of " + i + "right");
} // end main()
这是方法
public static void speakLines(int num1, int num2, int cal) {
int count = 0;
Scanner scanner = new Scanner(System.in);
Voice voice;
// set up a Voicemanager object and use it to link voice with a particular voice
VoiceManager voiceManager = VoiceManager.getInstance();
voice = voiceManager.getVoice("kevin16");
// load the selected voice
voice.allocate();
// begin speaking the text
System.out.println("what is " + num1 + " + " + num2 + ":");
voice.speak("what is " + num1 + " + " + num2 + ":");
//talk
System.out.println("Please enter answer");
int answer = scanner.nextInt();
//talk
if (answer == cal) {
System.out.println("That's right");
voice.speak("That's right");
count += 1;
"Count +=1" 以上 ^ -- 我需要从 main() 方法打印这个计数整数。这是我试图 return 到主要方法的数据 "count"。这告诉我用户答对了多少答案。
} else {
System.out.println("Sorry, the answer is" + cal);
voice.speak("Sorry, the answer is " + cal);
}
} // end speakLines()
public static int randomA() {
int A;
A = 1 + (int) (Math.random() * 10);
return A;
}
public static int randomB() {
int B;
B = 1 + (int) (Math.random() * 5);
return B;
}
// end randomB()
} // end class
现在我知道 static void 方法不 return 数据。但我知道应该有一种方法可以 return 特定数据。
声明一个class变量public static int count;
来替换speakLines
方法中的局部变量count
。
这样做之后,您应该可以在 main 方法中访问和打印 score
。
return 代码可能不是你想要的。您可能希望将答案写入标准输出。这样其他一些程序就可以接收它。 System.out
上有多种方法可以写入标准输出。
如果要将 java 程序用作程序链的一部分,请使用标准输入和标准输出。
System.out.println
导致程序发送输出。这可以被另一个程序读取。例如,您可以 运行 并将其输出重定向到文件:java -jar yourjar > out.txt
您还可以使用管道字符将此输出发送到另一个程序:
java -jar yourJar | grep of
我不知道你想对输出做什么。
您可以在包含 class(主函数上方的那个)上使用静态文件,并在 speakLines 的开头重置它。
通常你不需要静态的,但因为 main 是一个静态方法,它不能访问包含 class.
的非静态字段或
使用一个 class 参数来保存 int 值,并在 speakLines 的开头重置值。
在@DizzyCode 的帮助下,怎么样-
1. 在第2行
声明public static int count
2. 在第9行
中初始化变量count
3. 在方法 'speakLines'
//******************************
public static int count;
Public static void main(String[] args) throws Exception {
int cal;
int i;
//set count to 0 before the loop starts
count=0;
for (i = 0; i < 3; i++) {
int num1 = randomA();
int num2 = randomB();
cal = num1 + num2;
speakLines(num1, num2, cal);
}
System.out.println("You got " + count + "of " + i + "right");
} // end main()
public static void speakLines(int num1, int num2, int cal) {
//**************************************
//make this count invalid and create a public static count variable before main method
// int count = 0;
Scanner scanner = new Scanner(System.in);
Voice voice;
// set up a Voicemanager object and use it to link voice with a particular voice
VoiceManager voiceManager = VoiceManager.getInstance();
voice = voiceManager.getVoice("kevin16");
// load the selected voice
voice.allocate();
// begin speaking the text
System.out.println("what is " + num1 + " + " + num2 + ":");
voice.speak("what is " + num1 + " + " + num2 + ":");
//talk
System.out.println("Please enter answer");
int answer = scanner.nextInt();
//talk
if (answer == cal) {
System.out.println("That's right");
voice.speak("That's right");
count += 1;}
else {
System.out.println("Sorry, the answer is" + cal);
voice.speak("Sorry, the answer is " + cal);
}
} // end speakLines()
public static int randomA() {
int A;
A = 1 + (int) (Math.random() * 10);
return A;
}
public static int randomB() {
int B;
B = 1 + (int) (Math.random() * 5);
return B;
}
// end randomB()
} // end class