如何在不使用扫描仪的情况下将Char /算术运算符作为用户输入

时间:2018-01-13 11:50:38

标签: java

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

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

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

感谢你回答我,即使你非常忙碌:)

2 个答案:

答案 0 :(得分:0)

是的,你可以使用命令行参数的概念。编译你的java文件之后,在执行它时就像java一样。你可以写出你想要在类旁边给出的输入作为字符串在String类型数组中。

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]中传递的字符串,我们需要Wrapper类及其方法。

编译

javac Addition.java

执行

java Addition 1 2 3

答案 1 :(得分:0)

我不确定为什么在这种情况下你不会使用Scanner ...但是,这些是在不使用它的情况下读取用户输入的其他方法:

  1. 使用JOptionPaneread more about it here
  2. 使用:

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

    而不是:

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

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

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

      InputStreamReader isr = new InputStreamReader(System.in); 
      BufferedReader br = new BufferedReader (isr);
      try {
      a = Integer.parseInt(br.readLine());
      } catch (IOException e) {
      e.printStackTrace();
      }
      
    2. 作为旁注,您无需在知道哪个是用户想要的操作之前执行所有操作。改变这个:

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