2015年4月16日木曜日

3つの整数を入力して最大値を表示するjavaプログラムパターン1

Javaバイブルシリーズ Java入門 P142演習問題5.3

------
import java.io.*;
public class Maximum {
 public static void main(String[] args) throws IOException {
 
  final String MAX_MESSAGE = "最大値は";
  String max = "";
 
  BufferedReader br =
          new BufferedReader(new InputStreamReader(System.in));
 
 
  System.out.print("xを入力>");
  int x = Integer.parseInt(br.readLine());
  System.out.print("yを入力>");
  int y = Integer.parseInt(br.readLine());
  System.out.print("zを入力>");
  int z = Integer.parseInt(br.readLine());
 
  if(y > x || z > x) {
   if(z > y) {
    max = MAX_MESSAGE + z;
   } else {
    max = MAX_MESSAGE + y;
   }
     } else {
   if (z > x) {
    max = MAX_MESSAGE + z;
   } else {
    max = MAX_MESSAGE + x;
   }
  }
  System.out.println(max);


 }
}
------

実行結果
------
>java Maximum
xを入力>3
yを入力>4
zを入力>5
最大値は5
 -- Press any key to exit (Input "c" to continue) --
------

0 件のコメント:

コメントを投稿