返回首页

package ConsoleApplication1;

import java.io.*;

import java.util.Scanner.*;

 

public class Program

{

	int Number1, Number2, Result;

	char Option;

 

	public void calculation()

	{

		try

		{

			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

			System.out.println("Enter the First Number");

			Number1 = Integer.parseInt(br.readLine());

 

			System.out.println("Enter the Second Number");

			Number2 = Integer.parseInt(br.readLine());

 

			System.out.println("Main Menu");

			System.out.println("1.Addition");

			System.out.println("2.Substraction");

			System.out.println("3.Multiplication");

			System.out.println("4.Division");

 

			System.out.println("Enter the option you want to perform.");

			Option = br.readLine();

			switch (Option)

			{

				case '1':

					Result = Number1 + Number2;

					System.out.println("The Addition of two numbers is: " + Result);

					break;

 

				case '2':

					Result = Number1 - Number2;

					System.out.println("The Substraction of two numbers is: " + Result);

					break;

 

				case '3':

					Result = Number1 * Number2;

					System.out.println("The Multiplication of two numbers is: " + Result);

					break;

 

				case '4':

					Result = Number1 / Number2;

					System.out.println("The Division of two numbers is: " + Result);

					break;

 

				default:

					System.out.println("Invalid Option");

					break;

			}

		}

		catch (IOException ex)

		{

		}

	}

 

	public static void main(String[] args)throws IOException

	{

		Program p = new Program();

		p.calculation();

	}

 

}

回答

评论会员:游客 时间:2012/02/06
Prera​​k帕特尔
使用的charAt
Option = br.readLine().charAt(0);
评论会员:理查德MacCutchan 时间:2012/02/06
]返回一个字符串,而不是一个字符,所以你应该考虑到一个字符串的响应,然后使用字符串的第一个字符