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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在main()方法中调用多个方法(int)相关的知识,希望对你有一定的参考价值。

我目前在我的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;
   }
}
答案

请将您的代码修改为:

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");
            }
        }
    }
}
另一答案

使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;
   }
}
另一答案

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

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

以上是关于在main()方法中调用多个方法(int)的主要内容,如果未能解决你的问题,请参考以下文章

将公共属性和方法公开给 Main Activity 的片段是不是是一种不好的形式?

课堂验证作业

JAVA第一章整理实验

根据父活动在片段中调用不同的方法

java中main方法中的static函数不管是否在最后,都会最先执行吗?

Java:在方法 main 中调用方法