我目前在我的主要方法中调用我的每个方法(int)时遇到问题,我的每个调用都收到错误“找不到变量”。如何修复我的代码,以便我可以调用每个方法并为每个单独的方法提供一个输出?
这是我的代码:
import java.util.*;
public class Method{
public static void main(String [] args) {
System.out.println(evenOdd(x));
System.out.println(boxMake(n));
System.out.println(checkPrime(n));
}
public static boolean evenOdd(int x) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your number to check even or odd: ");
x = sc.nextInt();
boolean odd = false;
if(x % 2 ==0){
odd = true;
System.out.println(odd + " is true.");
}
return odd;
}
public static void boxMake(int n) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your number to make a box: ");
n = sc.nextInt();
for(int i=0; i<n; i++){
for(int x=0; x<n; x++){
System.out.print("*");
}
System.out.println("");
}
}
public static int checkPrime(int n){
int i;
int m=0;
int flag=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check if prime: ");
n = sc.nextInt();
m=n/2;
if(n==0||n==1){
System.out.println(n + " is not a prime number");
}else{
for(i=2; i<=m; i++){
if(n % i == 0){
System.out.println(n + " is not a prime number");
flag = 1;
break;
}
}
if(flag == 0){
System.out.println(n + " is a prime number");
}
}
return n;
}
}
明月笑刀无情
慕无忌1623718
宝慕林4294392
相关分类