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

问题描述 投票:-1回答:3

我目前在我的main方法中调用我的每个方法(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;
   }
}
java methods int boolean
3个回答
0
投票

请将您的代码修改为:

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

0
投票

使main方法看起来像这样,并从您的方法中删除扫描仪,如下所示:

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

0
投票

nx是您方法中的局部变量。请记住,例如来自nevenOdd()什么都没有,但这个名字与nboxMake()一样。如果你想在你的类中使用nx将它们定义为类成员。

import java.util.*;
public class Method{
private int n;
private int x;
© www.soinside.com 2019 - 2024. All rights reserved.