简体   繁体   English

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

[英]Calling multiple methods(int) in main() method

I am currently having a problem calling each of my methods(int) in my main method, getting the error "cannot find variable" for each of my calls.我目前在主方法中调用每个方法(int)时遇到问题,每次调用都出现错误“找不到变量”。 How can I fix my code so I could call each method and have an output for each separate method?如何修复我的代码,以便我可以调用每个方法并为每个单独的方法输出?

Heres my code:这是我的代码:

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;
   }
}

Make the main method look like this and remove Scanners from your methods like so:使主要方法看起来像这样,并从你的方法中删除扫描器,如下所示:

 public static void main(String [] args) { 
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your number to check even or odd: ");
      x = sc.nextInt();
      System.out.println("Enter your number to make a box: ");
      n = sc.nextInt();
      System.out.println(evenOdd(x));
      System.out.println(boxMake(n));
      System.out.println(checkPrime(n));  
     }
public static boolean evenOdd(int x) {
  boolean odd = false;
if(x % 2 ==0){
odd = true;
  System.out.println(odd + " is true.");
 }
return odd;
}

public static void boxMake(int n) {
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;
 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;
   }
}

n and x are local variables in your methods. nx是方法中的局部变量。 And keep in mind that for example n from evenOdd() have nothing, but the name, in common with n from boxMake() .请记住,例如,来自evenOdd() n除了名称之外什么都没有,与来自boxMake() n boxMake() If you want to use n and x all over your class define them as class members.如果您想在整个类中使用nx ,请将它们定义为类成员。

import java.util.*;
public class Method{
private int n;
private int x;

Please modify your code to this :请将您的代码修改为:

public class Example {

    public static void main(String[] args) {
        int x = 0, n = 0, p = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your number to check even or odd: ");
        x = sc.nextInt();
        evenOdd(x);
        System.out.println("Enter your number to make a box: ");
        n = sc.nextInt();
        boxMake(n);
        System.out.println("Enter a number to check if prime: ");
        p = sc.nextInt();
        checkPrime(p);
        sc.close();
    }

    public static void evenOdd(int x) {
        if (x % 2 == 0) {
            System.out.println(x + " is even.");
            return;
        }
        System.out.println(x + " is odd.");
    }

    public static void boxMake(int n) {
        for (int i = 0; i < n; i++) {
            for (int x = 0; x < n; x++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    }

    public static void checkPrime(int n) {
        int i;
        int m = 0;
        int flag = 0;
        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");
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM