【问题标题】:how to take Char/arithmetic operator as input from user without using Scanner如何在不使用扫描仪的情况下将字符/算术运算符作为用户的输入
【发布时间】:2018-01-13 11:50:37
【问题描述】:

感谢您查看我的问题。我想问一下,我可以在不使用扫描仪的情况下将字符或算术运算符作为用户的输入吗?

这是我编写程序的代码,该程序使用算术运算符对两个数字执行代数运算。 (代数运算为+、-、*、/、%)

import java.util.Scanner;
class q4
{
    public static void main(String args[])
    {
        Scanner s = new Scanner(System.in );
        int a, b;
        char operator;
        System.out.print("Enter A : ");
        a=s.nextInt();
        System.out.print("Enter B : ");
        b=s.nextInt();
        System.out.print("Enter operator (+, -, *, /)");
        operator = s.next().charAt(0);
        double addition  = a+b;
        double subtraction  = a-b;
        double multiplication  = a*b;
        double division  = a/b;

        switch(operator)
        {
            case '+' :
            {
                System.out.print("Total after Addition is : "+addition);
                break;
            }
            case '-' :
            {
                System.out.print("Total after Subtraction is : " +subtraction);
                break;
            }
            case '*' :
            {
                System.out.print("Total after Multiplication is : "+multiplication);
                break;
            }
            case '/' :
            {
                System.out.print("Total after Division is : "+division);
                break;
            }
            default :
            {
                System.out.print("Please select proper operator");
                return;
            }
        }
    }
}

感谢您在很忙的时候回复我:)

【问题讨论】:

  • 你为什么不想使用扫描仪?
  • 您可以随意使用System.in字段。这是一个普通的InputStream,它有一个方法read() 从输入流中读取下一个字节。但这里的问题到底是什么?
  • 代数?无论如何...如果您使用 Java SE6 或更高版本,您可以使用 BufferedReader 类并将其与 InputStreamReader 类甚至 Console 类一起包装(见SO Post)。或者创建一个 GUI。甚至可能坚持使用 Scanner 类,它的效果相当不错。 :)

标签: java


【解决方案1】:

我不太确定在这种情况下您为什么不使用 Scanner...但是,这些是其他一些无需使用即可读取用户输入的方法:

  1. 使用JOptionPane (read more about it here)

使用:

 a = Integer.parseInt(JOptionPane.showInputDialog("Enter A :"))

代替:

System.out.print("Enter A : "); 
a=s.nextInt(); 

将此用于所有不同的输入。

  1. 使用System.in 创建一个读取器变量,并使用一个BufferedReader 定义变量a 以获取输入:

    InputStreamReader isr = new InputStreamReader(System.in); 
    BufferedReader br = new BufferedReader (isr);
    try {
    a = Integer.parseInt(br.readLine());
    } catch (IOException e) {
    e.printStackTrace();
    }
    

附带说明一下,您无需执行所有操作,然后才能知道用户想要哪个操作。改变这个:

double addition  = a+b;
double subtraction  = a-b;
double multiplication  = a*b;
double division  = a/b;

switch(operator)
{
    case '+' :
    {
        System.out.print("Total after Addition is : "+addition);
        break;
    }
    case '-' :
    {
        System.out.print("Total after Subtraction is : " +subtraction);
        break;
    }
    case '*' :
    {
        System.out.print("Total after Multiplication is : "+multiplication);
        break;
    }
    case '/' :
    {
        System.out.print("Total after Division is : "+division);
        break;
    }
    default :
    {
        System.out.print("Please select proper operator");
        return;
    }
}

仅此:

switch(operator)
{
    case '+' :
    {
        double addition  = a+b;
        System.out.print("Total after Addition is : "+addition);
        break;
    }
    case '-' :
    {
        double subtraction  = a-b;
        System.out.print("Total after Subtraction is : " +subtraction);
        break;
    }
    case '*' :
    {
        double multiplication  = a*b;
        System.out.print("Total after Multiplication is : "+multiplication);
        break;
    }
    case '/' :
    {
        double division  = a/b;
        System.out.print("Total after Division is : "+division);
        break;
    }
    default :
    {
        System.out.print("Please select proper operator");
        return;
    }
}

【讨论】:

    【解决方案2】:

    是的,您可以使用命令行参数的概念。在编译您的 java 文件并像 java 一样执行它时。您可以在作为字符串的类旁边写下您想要提供的输入在字符串类型数组中。

    public static void main(String args[])
    

    您在执行 java 文件时传递的输入被带入 args[] 并存储在相应的索引号中,例如 args[0] args[1]....args[n]。这是一个动态数组,其大小会根据您在命令行中传递的参数而增加。

    示例:添加数字

    代码

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

    {

        int a=Integer.parseInt(args[0]);
        int b=Integer.parseInt(args[1]);
    
      System.out.println(a+b);
       }
      }
    

    要转换我在 args[0],args[1],args[2] 中传递的字符串,我们需要包装类及其方法。

    编译

    javac Addition.java
    

    执行

    java Addition 1 2 3
    

    【讨论】:

      猜你喜欢
      • 2020-08-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 2018-10-28
      • 2013-04-13
      相关资源
      最近更新 更多