在 main() 方法中调用多个方法(int)

我目前在我的主要方法中调用我的每个方法(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;

   }

}


猛跑小猪
浏览 134回答 3
3回答

明月笑刀无情

请将您的代码修改为:public class Example {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int x = 0, n = 0, p = 0;&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter your number to check even or odd: ");&nbsp; &nbsp; &nbsp; &nbsp; x = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; evenOdd(x);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter your number to make a box: ");&nbsp; &nbsp; &nbsp; &nbsp; n = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; boxMake(n);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter a number to check if prime: ");&nbsp; &nbsp; &nbsp; &nbsp; p = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; checkPrime(p);&nbsp; &nbsp; &nbsp; &nbsp; sc.close();&nbsp; &nbsp; }&nbsp; &nbsp; public static void evenOdd(int x) {&nbsp; &nbsp; &nbsp; &nbsp; if (x % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(x + " is even.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(x + " is odd.");&nbsp; &nbsp; }&nbsp; &nbsp; public static void boxMake(int n) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int x = 0; x < n; x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void checkPrime(int n) {&nbsp; &nbsp; &nbsp; &nbsp; int i;&nbsp; &nbsp; &nbsp; &nbsp; int m = 0;&nbsp; &nbsp; &nbsp; &nbsp; int flag = 0;&nbsp; &nbsp; &nbsp; &nbsp; m = n / 2;&nbsp; &nbsp; &nbsp; &nbsp; if (n == 0 || n == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(n + " is not a prime number");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (i = 2; i <= m; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n % i == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(n + " is not a prime number");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (flag == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(n + " is a prime number");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕无忌1623718

n并且x是您方法中的局部变量。请记住,例如nfrom与fromevenOdd()相同,除了名称之外什么都没有。如果你想在你的类中使用并且将它们定义为类成员。nboxMake()nximport java.util.*;public class Method{private int n;private int x;

宝慕林4294392

使 main 方法看起来像这样,并从您的方法中删除 Scanners,如下所示:&nbsp;public static void main(String [] args) {&nbsp;&nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; System.out.println("Enter your number to check even or odd: ");&nbsp; &nbsp; &nbsp; x = sc.nextInt();&nbsp; &nbsp; &nbsp; System.out.println("Enter your number to make a box: ");&nbsp; &nbsp; &nbsp; n = sc.nextInt();&nbsp; &nbsp; &nbsp; System.out.println(evenOdd(x));&nbsp; &nbsp; &nbsp; System.out.println(boxMake(n));&nbsp; &nbsp; &nbsp; System.out.println(checkPrime(n));&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;}public static boolean evenOdd(int x) {&nbsp; boolean odd = false;if(x % 2 ==0){odd = true;&nbsp; System.out.println(odd + " is true.");&nbsp;}return odd;}public static void boxMake(int n) {for(int i=0; i<n; i++){&nbsp; for(int x=0; x<n; x++){&nbsp; &nbsp; &nbsp;System.out.print("*");&nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;System.out.println("");&nbsp; }}&nbsp;public static int checkPrime(int n){&nbsp;int i;&nbsp;int m=0;&nbsp;int flag=0;&nbsp;m=n/2;&nbsp; if(n==0||n==1){&nbsp; &nbsp; &nbsp;System.out.println(n + " is not a prime number");&nbsp; }else{&nbsp; &nbsp; &nbsp;for(i=2; i<=m; i++){&nbsp; &nbsp; &nbsp; &nbsp; if(n % i == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(n + " is not a prime number");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;flag = 1;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(flag == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(n + " is a prime number");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; return n;&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java