1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Javaで電卓を実装してみた

Posted at

概要

今日はJavaの勉強ということで、電卓を作ってみました。以下のページを参考にしました。
https://www.javadrive.jp/start/sample1/index1.html

ソースコード

上記のページは式を表示するだけだったので、計算もさせて結果を表示させてみました。
ソースコードは以下の通りです。

dentaku2.java
class dentaku2{
  public static void main(String args[]){
    int val1 = 0;  /* 最初の数字 */
    int val2 = 0;  /* 次の数字 */
    String ope;    /* 演算子 */

    if (args.length != 3){
      System.out.println("Usage : java dentaku 数値 演算子 数値");
      System.out.println("演算子は kake waru tasu hiku の4つです");
      System.exit(0);  /* プログラムを終了する */
    }

    val1 = Integer.parseInt(args[0]);
    val2 = Integer.parseInt(args[2]);
    ope = args[1];

    System.out.println("入力された式は " + val1 + " " + ope + " " + val2 + " です");
    if(ope.equals("kake")==true) System.out.println("計算結果は "+(val1*val2));
    if(ope.equals("waru")==true) System.out.println("計算結果は "+(val1/val2));
    if(ope.equals("tasu")==true) System.out.println("計算結果は "+(val1+val2));
    if(ope.equals("hiku")==true) System.out.println("計算結果は "+(val1-val2));
  }
}

実行結果

コンパイルします。

$ javac dentaku2.java

実行します。

$ java dentaku2 2 kake 3
入力された式は 2 kake 3 です
計算結果は 6
$ java dentaku2 2 waru 3
入力された式は 2 waru 3 です
計算結果は 0
$ java dentaku2 2 tasu 3
入力された式は 2 tasu 3 です
計算結果は 5
$ java dentaku2 2 hiku 3
入力された式は 2 hiku 3 です
計算結果は -1

何かの役に立てばと。

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?