如何通过计算方法传递整数?

时间:2014-10-01 15:34:55

标签: java methods

我正在尝试创建一个用户可以输入2个数字的程序,然后会显示一个菜单,用户可以从中选择五个选项之一。

import java.util.Scanner;

public class MenuProg
{
	public static void main (String [] args)
	{		
		int iNum1;								//Holds user input number 1
		int iNum2;								//Holds user input number 2
		Scanner kb = new Scanner(System.in);	//Holds user input from keyboard
		int iMenu;
		
		System.out.print ("\n\nPress ENTER to continue");
		kb.nextLine();
		
		System.out.print("\nPlease enter the first integer: ");
		iNum1 = kb.nextInt();
		System.out.print("\nPlease enter the second integer: ");
		iNum2 = kb.nextInt();
		
		iMenu = menu();
	}

	public static int menu()
	{
		Scanner kb = new Scanner (System.in);
		int iSelection;
		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();
				break;
			case 2:
				subTwoNumbers();
				break;
			case 3:
				avgTwoNumbers();
				break;
			case 4:
				multiTwoNumbers();
				break;	
			case 5:
				divTwoNumbers();
		}
		return iSelection;
	}
	public static int sumTwoNumbers(int iNum1, int iNum2 param)
	{
		int iNum1;
		int iNum2;
		int	iSum;
		
		iSum = iNum1 + iNum2;
		return iSum;
	}
	public static int subTwoNumbers(int iNum1, int iNum2 param)
	{
		int iNum1;
		int iNum2;
		int iSub;
		
		iSub = iNum1 - iNum2;
		return iSub;
	}
	public static int avgTwoNumbers(int iNum1, int iNum2 param)
	{
		int iNum1;
		int iNum2;
		int iAvg;
		
		iAvg = (iNum1 + iNum2) / 2;
		return iAvg;
	}
	public static int multiTwoNumbers(int iNum1, int iNum2 param)
	{
		int iNum1;
		int iNum2;
		int iMulti;
		
		iMulti = iNum1 * iNum2;
		return iMulti;
	}
	public static int divTwoNumbers(int iNum1, int iNum2 param)
	{
		int iNum1;
		int iNum2;
		int iDiv;
		
		iDiv = iNum1/iNum2;
		return iDiv;

    }

我遇到32个错误,但大多数情况下它们与预期的标识符,非法的类型启动以及预期的很多类,接口或枚举有关。我的主要问题是如何将用户输入的整数从我的主要传递到我的其他计算方法?如果我能解决这个问题,我相信我能找出其他错误。提前致谢。

MenuProg.java:82: error: ')' expected
	public static int sumTwoNumbers(int iNum1, int iNum2 param)
	                                                    ^
MenuProg.java:82: error: illegal start of type
	public static int sumTwoNumbers(int iNum1, int iNum2 param)
	                                                          ^
MenuProg.java:82: error: <identifier> expected
	public static int sumTwoNumbers(int iNum1, int iNum2 param)
	                                                           ^
MenuProg.java:83: error: ';' expected
	{
	 ^
MenuProg.java:84: error: <identifier> expected
		int iNum1;
		         ^
MenuProg.java:88: error: <identifier> expected
		iSum = iNum1 + iNum2;
		    ^
Menu.java:89: error: illegal start of type
		return iSum;
		^
MenuProg.java:89: error: ';' expected
		return iSum;

MenuProg.java:91: error: class, interface, or enum expected
	public static int subTwoNumbers(int iNum1, int iNum2 param)
	              ^
MenuProg.java:94: error: class, interface, or enum expected
		int iNum2;
		^
MenuProg.java:95: error: class, interface, or enum expected
		int iSub;
		^
MenuProg.java:97: error: class, interface, or enum expected
		iSub = iNum1 - iNum2;
		^
MenuProg.java:98: error: class, interface, or enum expected
		return iSub;

MenuProg.java:99: error: class, interface, or enum expected
	}
	^
MenuProg.java:100: error: class, interface, or enum expected
	public static int avgTwoNumbers(int iNum1, int iNum2 param)
	              ^
MenuProg.java:103: error: class, interface, or enum expected
		int iNum2;
		^
MenuProg.java:104: error: class, interface, or enum expected
		int iAvg;
		^
MenuProg.java:106: error: class, interface, or enum expected
		iAvg = (iNum1 + iNum2) / 2;
		^
MenuProg.java:107: error: class, interface, or enum expected
		return iAvg;
		^
MenuProg.java:108: error: class, interface, or enum expected
	}
	^
MenuProg.java:109: error: class, interface, or enum expected
	public static int multiTwoNumbers(int iNum1, int iNum2 param)
	              ^
MenuProg.java:112: error: class, interface, or enum expected
		int iNum2;
		^
MenuProg.java:113: error: class, interface, or enum expected
		int iMulti;
		^
MenuProg.java:115: error: class, interface, or enum expected
		iMulti = iNum1 * iNum2;
		^
MenuProg.java:116: error: class, interface, or enum expected
		return iMulti;
		^
Proj2.java:117: error: class, interface, or enum expected
	}
	^
MenuProg.java:118: error: class, interface, or enum expected
	public static int divTwoNumbers(int iNum1, int iNum2 param)
	              ^
MenuProg.java:121: error: class, interface, or enum expected
		int iNum2;
		^
MenuProg.java:122: error: class, interface, or enum expected
		int iDiv;
		^
MenuProg.java:124: error: class, interface, or enum expected
		iDiv = iNum1/iNum2;
		^
MenuProg.java:125: error: class, interface, or enum expected
		return iDiv;
		^
MenuProg.java:126: error: class, interface, or enum expected
	}
	^
		      ^

    

2 个答案:

答案 0 :(得分:0)

你不能这样做

public static int divTwoNumbers(int iNum1, int iNum2 param){} 
//you cannot have a space in variable name 

要么是

public static int divTwoNumbers(int iNum1, int iNum2,int param){}

public static int divTwoNumbers(int iNum1, int iNum2param){}

或者我没有看到任何使用 param 只是删除它

public static int divTwoNumbers(int iNum1, int iNum2){}

答案 1 :(得分:0)

你有严重的语法问题,但你的程序的一般想法实际上是非常合理的。 语法/次要的东西: 1.)查找System.out.println(),它会在打印之后添加换行符。

2。)方法或函数的参数/参数:

public static int funcName(int num1, int num2) {
     num1 = 2 + 3;
     num1 = num1 + num2;
     int result = num1 * num2;
     return result;
}

注意函数的参数如何看起来像funcName(int num1, int num2),然后在函数中已经声明了那些参数。你没有在函数中做int num1;,你可以跳转到使用num1 =无论如何。您确实需要在函数中声明新的非参数变量,例如在这种情况下为int result

3.)调用函数:

int x = 32;
int y = 15;
int functionResult = funcName(x, y);
// to print functionResult: System.out.print(functionResult); or print it within the function somewhere