簡體   English   中英

如何在不使用掃描儀的情況下將字符/算術運算符作為用戶的輸入

[英]how to take Char/arithmetic operator as input from user without using Scanner

感謝您查看我的問題。 我想問一下,我可以在不使用掃描儀的情況下將字符或算術運算符作為用戶的輸入嗎?

這是我編寫程序的代碼,該程序使用算術運算符對兩個數字執行代數運算。 (代數運算為+、-、*、/、%)

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

即使您很忙,也感謝您回復我:)

我不確定你為什么不在那種情況下使用Scanner ......但是,這些是在不使用它的情況下讀取用戶輸入的其他一些方法:

  1. 使用JOptionPane在此處閱讀更多信息

利用:

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

作為旁注,您無需執行所有操作即可知道用戶想要哪個操作。 改變這個:

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

是的,您可以使用命令行參數的概念。在編譯您的 java 文件並像 java 一樣執行它時。您可以在作為 String 類型的 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] 中傳遞的字符串,我們需要包裝類及其方法。

編譯

javac Addition.java

執行

java Addition 1 2 3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM