如何从我的main传递int到计算方法?

时间:2014-10-10 16:43:19

标签: java methods

尝试创建一个程序,其中用户在我的main中输入2个数字,菜单方法显示以显示用户想要使用switch语句对这2个数字做什么,然后4个其他方法将根据哪个选项用户进行计算从菜单中选择。

我的问题/问题是我如何从我的主要传递我的2个存储的int来进行其他4种方法的计算?

我的解决方案是将请求和输入2个整数的过程移动到我的菜单方法中,但我的教师特别要求将整数从main传递给方法。

这是我到目前为止所拥有的。 编辑 我知道这段代码编译和工作,我想要的是5行代码要求并获取我的main中的2个整数而不是我的菜单方法 修改

    public static void main (String [] args)
    {       

        Scanner kb = new Scanner(System.in);    //Allows input from keyboard
        int iMenu;                              //Holds menu 
        System.out.print ("\n\nPress ENTER to continue");
        kb.nextLine();
        iMenu = menu();                         //Calls menu method


    }


public static int menu()
{

        int iNum1;                              //Holds user input for number 1
        int iNum2;                              //Holds user input for number 2
        Scanner kb = new Scanner(System.in);    //Allows input from keyboard
        System.out.print("\nPlease enter the first integer: ");
        iNum1 = kb.nextInt();
        System.out.print("\nPlease enter the second integer: ");
        iNum2 = kb.nextInt();

        int iSelection;                         //Holds menu selection
        System.out.println("\n\t M E N U ");
        System.out.println("\n1>Sum up two integers" +
        "\n2>Find the difference of two integers" +
        "\n3>Find the average of two integers" +
        "\n4>Find the product of two integers" +
        "\n5>Find the quotient of two integers");
        iSelection = kb.nextInt();
    //}     
        switch (iSelection)
        {
            case 1: 
                sumTwoNumbers(iNum1, iNum2);
                break;
            case 2:
                subTwoNumbers(iNum1, iNum2);
                break;
            case 3:
                avgTwoNumbers(iNum1, iNum2);
                break;
            case 4:
                multiTwoNumbers(iNum1, iNum2);
                break;  
            case 5:
                divTwoNumbers(iNum1, iNum2);
            }
            return iSelection;              //End menu()
        }
        public static int sumTwoNumbers(int iNum1, int iNum2)
        {
            int iSum;                       //Holds sum of iNum1 and iNum2
            iSum = iNum1 + iNum2;
            System.out.println(iSum);
            return iSum;                    //End sumTwoNumbers(int,int)
        }
        public static int subTwoNumbers(int iNum1, int iNum2)
        {
            int iSub;                       //Holds difference of iNum1 and iNum2
            iSub = iNum1 - iNum2;
            System.out.println(iSub);
            return iSub;                    //End subTwoNumbers(int,int)
        }
        public static double avgTwoNumbers(int iNum1, int iNum2)
        {
            double dAvg;                    //Holds average of iNum1 and iNum2
            dAvg = (iNum1 + iNum2) / 2;
            System.out.println(dAvg);
            return dAvg;                    //End avgTwoNumbers(int,int)
        }
        public static int multiTwoNumbers(int iNum1, int iNum2)
        {
            int iMulti;                     //Holds product of iNum1 and iNum2
            iMulti = iNum1 * iNum2;
            System.out.println(iMulti);
            return iMulti;                  //End multiTwoNumbers(int,int)
        }
        public static int divTwoNumbers(int iNum1, int iNum2)
        {
            int iDiv;                       //Holds quotient of iNum1 and iNum2
            iDiv = iNum1/iNum2;
            System.out.println(iDiv);
            return iDiv;                    //End divTwoNumbers(int,int)
        }

当我移动(输入2个整数)到我的主要部分时,我主要遇到大量错误"在我的所有方法中找不到符号,例如:

Proj2.java:78: error: cannot find symbol
                    avgTwoNumbers(iNum1, iNum2);
                                  ^
  symbol:   variable iNum1
  location: class Proj2
Proj2.java:78: error: cannot find symbol
                    avgTwoNumbers(iNum1, iNum2);
                                         ^

我假设它是因为int没有正确地传递给我的方法,但我不知道如何做到这一点。

5 个答案:

答案 0 :(得分:1)

我使用此代码编译的唯一问题是iNum1iNum2两次声明。

让我也为您提供更好的语法/惯例:

import java.util.Scanner;

class Calculator
{
    public Calculator()
    {
        Scanner kb = new Scanner(System.in);

        System.out.print("\n\nPress ENTER to continue");
        kb.nextLine();
        this.menu();
    }

    public int menu()
    {       
        Scanner kb = new Scanner(System.in);
        System.out.print("\nPlease enter the first integer: ");
        int iNum1 = kb.nextInt();
        System.out.print("\nPlease enter the second integer: ");
        int iNum2 = kb.nextInt();

        int iSelection = 0;
        System.out.println("\n\t M E N U ");
        System.out.println("\n1>Sum up two integers"
                + "\n2>Find the difference of two integers"
                + "\n3>Find the average of two integers"
                + "\n4>Find the product of two integers"
                + "\n5>Find the quotient of two integers");
        iSelection = kb.nextInt();

        switch (iSelection)
        {
            case 1:
                Calculator.sumTwoNumbers(iNum1, iNum2);
                break;
            case 2:
                Calculator.subTwoNumbers(iNum1, iNum2);
                break;
            case 3:
                Calculator.avgTwoNumbers(iNum1, iNum2);
                break;
            case 4:
                Calculator.multiTwoNumbers(iNum1, iNum2);
                break;
            case 5:
                Calculator.divTwoNumbers(iNum1, iNum2);
            default:
                System.out.println("Option : " + iSelection + "not handled.");
        }

        return iSelection;
    }

    public static int sumTwoNumbers(int iNum1, int iNum2)
    {
        int iSum = iNum1 + iNum2;
        System.out.println(iSum);
        return iSum;                    //End sumTwoNumbers(int,int)
    }

    public static int subTwoNumbers(int iNum1, int iNum2)
    {
        int iSub = iNum1 - iNum2;
        System.out.println(iSub);
        return iSub;                    //End subTwoNumbers(int,int)
    }

    public static double avgTwoNumbers(int iNum1, int iNum2)
    {
        double dAvg = (iNum1 + iNum2) / 2;
        System.out.println(dAvg);
        return dAvg;                    //End avgTwoNumbers(int,int)
    }

    public static int multiTwoNumbers(int iNum1, int iNum2)
    {
        int iMulti = iNum1 * iNum2;
        System.out.println(iMulti);
        return iMulti;                  //End multiTwoNumbers(int,int)
    }

    public static int divTwoNumbers(int iNum1, int iNum2)
    {
        int iDiv = iNum1 / iNum2;
        System.out.println(iDiv);
        return iDiv;                    //End divTwoNumbers(int,int)
    }

    public static void main(String[] args)
    {
        new Calculator();
    }
}

答案 1 :(得分:0)

public static void main (String [] args)
{
    Scanner kb = new Scanner(System.in);    //Allows input from keyboard
    int iNum1;                              //Holds user input for number 1
    int iNum2;                              //Holds user input for number 2
    int iSelection;                         //Holds menu selection

    System.out.print("\nPlease enter the first integer: ");
    iNum1 = kb.nextInt();
    System.out.print("\nPlease enter the second integer: ");
    iNum2 = kb.nextInt();
    System.out.println("\n\t M E N U ");
    System.out.println("\n1>Sum up two integers" +
            "\n2>Find the difference of two integers" +
            "\n3>Find the average of two integers" +
            "\n4>Find the product of two integers" +
            "\n5>Find the quotient of two integers");
    iSelection = kb.nextInt();   
    doAction(iNum1,iNum2,iSelection);
}

private static void doAction(int iNum1, int iNum2, int iSelection) {
    switch (iSelection)
    {
    case 1:
        sumTwoNumbers(iNum1, iNum2);
        break;
    case 2:
        subTwoNumbers(iNum1, iNum2);
        break;
    case 3:
        avgTwoNumbers(iNum1, iNum2);
        break;
    case 4:
        multiTwoNumbers(iNum1, iNum2);
        break;
    case 5:
        divTwoNumbers(iNum1, iNum2);
    }
}

private static int sumTwoNumbers(int iNum1, int iNum2)
{
    int iSum;                       //Holds sum of iNum1 and iNum2
    iSum = iNum1 + iNum2;
    System.out.println(iSum);
    return iSum;                    //End sumTwoNumbers(int,int)
}

private static int subTwoNumbers(int iNum1, int iNum2)
{
    int iSub;                       //Holds difference of iNum1 and iNum2
    iSub = iNum1 - iNum2;
    System.out.println(iSub);
    return iSub;                    //End subTwoNumbers(int,int)
}

private static double avgTwoNumbers(int iNum1, int iNum2)
{
    double dAvg;                    //Holds average of iNum1 and iNum2
    dAvg = (iNum1 + iNum2) / 2;
    System.out.println(dAvg);
    return dAvg;                    //End avgTwoNumbers(int,int)
}

private static int multiTwoNumbers(int iNum1, int iNum2)
{
    int iMulti;                     //Holds product of iNum1 and iNum2
    iMulti = iNum1 * iNum2;
    System.out.println(iMulti);
    return iMulti;                  //End multiTwoNumbers(int,int)
}

private static int divTwoNumbers(int iNum1, int iNum2)
{
    int iDiv;                       //Holds quotient of iNum1 and iNum2
    iDiv = iNum1/iNum2;
    System.out.println(iDiv);
    return iDiv;                    //End divTwoNumbers(int,int)
}

答案 2 :(得分:0)

您需要让menu接受您想要传递给它的参数。您可以将此方法的签名更改为更像

的内容
public static int menu(int argument)
{
   //your code
}

并像

一样使用它
int parameter = ...
int result = menu(parameter);

答案 3 :(得分:0)

您需要做的是在方法中添加参数并从主方法中传递它们

获取main方法中的所有输入并将其传递给方法

{
    public static void main (String [] args)
    {       

        Scanner kb = new Scanner(System.in);    //Allows input from keyboard




            int iNum1;                              //Holds user input for number 1
            int iNum2;                              //Holds user input for number 2
            Scanner kb = new Scanner(System.in);    //Allows input from keyboard
            System.out.print("\nPlease enter the first integer: ");
            int iNum1 = kb.nextInt();
            System.out.print("\nPlease enter the second integer: ");
            int iNum2 = kb.nextInt();

            int iSelection;                         //Holds menu selection
            System.out.println("\n\t M E N U ");
            System.out.println("\n1>Sum up two integers" +
            "\n2>Find the difference of two integers" +
            "\n3>Find the average of two integers" +
            "\n4>Find the product of two integers" +
            "\n5>Find the quotient of two integers");
            iSelection = kb.nextInt();

    Create object of your class here and call the method dont knw whats ur class name so for eg
  class1 cl = new class1();
cl.menu(iNum1,iNum2,iSelection);
    }


    public static int menu(int iNum1,int iNum2,int iSelection)
    {

        //}     
            switch (iSelection)
            {
                case 1: 
                    sumTwoNumbers(iNum1, iNum2);
                    break;
                case 2:
                    subTwoNumbers(iNum1, iNum2);
                    break;
                case 3:
                    avgTwoNumbers(iNum1, iNum2);
                    break;
                case 4:
                    multiTwoNumbers(iNum1, iNum2);
                    break;  
                case 5:
                    divTwoNumbers(iNum1, iNum2);
                }
                return iSelection;              //End menu()
            }
            public static int sumTwoNumbers(int iNum1, int iNum2)
            {
                int iSum;                       //Holds sum of iNum1 and iNum2
                iSum = iNum1 + iNum2;
                System.out.println(iSum);
                return iSum;                    //End sumTwoNumbers(int,int)
            }
            public static int subTwoNumbers(int iNum1, int iNum2)
            {
                int iSub;                       //Holds difference of iNum1 and iNum2
                iSub = iNum1 - iNum2;
                System.out.println(iSub);
                return iSub;                    //End subTwoNumbers(int,int)
            }
            public static double avgTwoNumbers(int iNum1, int iNum2)
            {
                double dAvg;                    //Holds average of iNum1 and iNum2
                dAvg = (iNum1 + iNum2) / 2;
                System.out.println(dAvg);
                return dAvg;                    //End avgTwoNumbers(int,int)
            }
            public static int multiTwoNumbers(int iNum1, int iNum2)
            {
                int iMulti;                     //Holds product of iNum1 and iNum2
                iMulti = iNum1 * iNum2;
                System.out.println(iMulti);
                return iMulti;                  //End multiTwoNumbers(int,int)
            }
            public static int divTwoNumbers(int iNum1, int iNum2)
            {
                int iDiv;                       //Holds quotient of iNum1 and iNum2
                iDiv = iNum1/iNum2;
                System.out.println(iDiv);
                return iDiv;                    //End divTwoNumbers(int,int)
            }

答案 4 :(得分:0)

将数字作为参数传递给menu()方法:

public static void main (String [] args)
{       

    Scanner kb = new Scanner(System.in);    //Allows input from keyboard
    int iMenu;                              //Holds menu 
    System.out.print ("\n\nPress ENTER to continue");
    kb.nextLine();

    int iNum1;                              //Holds user input for number 1
    int iNum2;                              //Holds user input for number 2

    System.out.print("\nPlease enter the first integer: ");
    iNum1 = kb.nextInt();
    System.out.print("\nPlease enter the second integer: ");
    iNum2 = kb.nextInt();

    iMenu = menu(iNum1, iNum2);                         //Calls menu method

}


public static int menu(int iNum1, int iNum2)
{
    /* rest of the menu code */
}