从main方法调用void方法,无法在void方法中传递参数
我的老师要求我们为我的计算机科学课程创建一个附加素数程序。我创造了我所有的无效方法,并相信将所有的数学逻辑都弄清楚了。然而,当我尝试参数传递到我的主要方法中我的方法的情况下,它给了我一个错误说:从main方法调用void方法,无法在void方法中传递参数
它无法找到该变量在这种情况下,变量“X”
package additiveprimes;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author talarik048
*/
public class AdditivePrimes {
public static void main(String[] args){
AdditivePrimes additivePrime = new AdditivePrimes();
additivePrime.userInput(x);
additivePrime.isPrime(x);
additivePrime.numArray(x);
}
public void userInput(String x){
Scanner sc = new Scanner(System.in);
try{
System.out.println("Please enter a number: ");
x = sc.nextLine();
} catch(NumberFormatException e){
System.out.println("Error, try again: ");
x = sc.nextLine();
}
}
public void isPrime(String x){
this.userInput(x);
boolean prime = true;
int y = Integer.parseInt(x);
for(int i = 0; i < y; i++){
if(y % i == 0){
prime = false;
break;
}
if(prime){
System.out.println("Your number is prime...");
} else {
System.out.println("Your number is not prime...");
}
}
}
public void numArray(String x){
this.userInput(x);
String[] strArray = x.split("\\s+");
boolean prime = true;
int[] numbArray = new int[strArray.length];
for(int j = 0; j < strArray.length; j++){
try{
numbArray[j] = Integer.parseInt(strArray[j]);
} catch(NumberFormatException e){
System.out.println("ERROR");
}
for(int i = 0; i < numbArray.length; i++){
int sum = (Arrays.stream(numbArray).sum());
if(sum % i == 0){
prime = false;
break;
}
if(prime){
System.out.println("Your number is an additive prime...");
} else {
System.out.println("Your number is not an additive prime...");
}
}
}
}
}
你的方法接受一个字符串作为参数,但你没有通过任何。你需要主要初始化x
。
public static void main(String[] args){
AdditivePrimes additivePrime = new AdditivePrimes();
String x= "55";
additivePrime.userInput(x);
additivePrime.isPrime(x);
additivePrime.numArray(x);
}
注意
虽然上面的代码解决当前的问题。它似乎不是使用这个类的正确方法。您应该可能调用.userInput()
方法并获取userInput,然后将其传递给其他方法。
你可以整体类可能更改为
public static void main(String[] args){
AdditivePrimes additivePrime = new AdditivePrimes();
String x = additivePrime.userInput();
additivePrime.isPrime(x);
additivePrime.numArray(x);
}
public String userInput(){
Scanner sc = new Scanner(System.in);
String x = null;
try{
System.out.println("Please enter a number: ");
x = sc.nextLine();
} catch(NumberFormatException e){
System.out.println("Error, try again: ");
x = sc.nextLine();
}
return x;
}
将字符串传递给userInput方法没有任何意义....考虑回答之前的情况。 –
@Os。是的我懂。我只是在解决他最初的问题。但要解决他的全班又是两美分:) –
你还没有使用它之前x作为任何定义。尝试定义x。在这种情况下,由于您的参数都是字符串,我建议......
String x = new String();
我想你想从userInput
返回一个值:
public static void main(String[] args){
AdditivePrimes additivePrime = new AdditivePrimes();
String x = additivePrime.userInput();
additivePrime.isPrime(x);
additivePrime.numArray(x);
}
public String userInput(){
Scanner sc = new Scanner(System.in);
String x = null;
try{
System.out.println("Please enter a number: ");
x = sc.nextLine();
} catch(Exception e){// see OH GOD SPIDERS comment
System.out.println("Error, try again: ");
x = sc.nextLine();//this is not a good way for a retry
}
return x;
}
或者你可以做一个x
领域。
“x”未定义。 –
你正在尝试做什么不会奏效。你应该阅读https://*.com/questions/40480/is-java-pass-by-reference-or-pass-by-value。您的userInput方法应该**返回**输入,以便您可以使用它们。您不能将字符串传递给该方法,然后在userInput方法内更改该字符串,并在该方法之外产生该效果。 –
欢迎来到SO。未来请发布可重现此问题的[MCVE]或SSCCE(http://sscce.org)。 这会增加您获得有用答案的机会(并且可能会帮助您找到问题)。 – c0der