开关盒选项字符覆盖 [英] Switch Case option character coversion

查看:208
本文介绍了开关盒选项字符覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

}

推荐答案

使用charAt
Option = br.readLine().charAt(0);


readline() [
readline()[^] returns a string not a character, so you should take the response into a string and then use the first character of the string.


这篇关于开关盒选项字符覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆