2015年2月22日日曜日

文字列をアルファベット順(辞書式)に比較して先に現れる文字列を表示するプログラム

参考 Javaバイブルシリーズ Java入門 Java 7版
P128 確認問題 5.5
------
import java.io.*;
public class DictionaryComp {
 public static void main(String[] args) throws IOException {

  BufferedReader br =
          new BufferedReader(new InputStreamReader(System.in));


  String appear = "先に現れる文字列:";

  System.out.print("文字列1>");
  String str1 = br.readLine();

  System.out.print("文字列2>");
  String str2 = br.readLine();

  if (str1.compareTo(str2) < 0) {
   appear += str1;
  } else if (str1.compareTo(str2) > 0) {
   appear += str2;
  } else {
   appear = "一致:" + str1 + "と" + str2;
  }

  System.out.println(appear);
 }
}
------

実行結果
------
>java DictionaryComp
文字列1>baseboll
文字列2>bus
先に現れる文字列:baseboll
 -- Press any key to exit (Input "c" to continue) --
------

2015年2月11日水曜日

入力点数が70点以上なら"合格",70点未満なら"不合格"と表示するプログラム

参考 Javaバイブルシリーズ Java入門 Java 7版
P121 確認問題 5.3

------
import java.io.*;
public class PassJudge02 {
 public static void main(String[] args) throws IOException {

  BufferedReader br =
          new BufferedReader(new InputStreamReader(System.in));

  System.out.print("点数を入力>");
  int ten = Integer.parseInt(br.readLine());

  if (ten < 70) {
   System.out.println("不合格です");
  } else {
      System.out.println("合格です");
  }
 }
}
-----

結果は以下
パターン1
-----
>java PassJudge02
点数を入力>70
合格です
 -- Press any key to exit (Input "c" to continue) --
-----
パターン2
-----
>java PassJudge02
点数を入力>69
不合格です
 -- Press any key to exit (Input "c" to continue) --
-----

2015年2月10日火曜日

70以上を入力したら「合格です」と表示するプログラム

参考
Javaバイブルシリーズ Java入門 Java 7版 P116 確認問題5.2

-------
import java.io.*;
public class PassJudge01 {
 public static void main(String[] args) throws IOException {
 
  BufferedReader br =
          new BufferedReader(new InputStreamReader(System.in));
 
  System.out.print("点数を入力>");
  int ten = Integer.parseInt(br.readLine());
 
  if (ten >= 70) {
   System.out.println("合格です");
  }
 }
}
-------
コンパイルと実行結果

パターン1
------
>java PassJudge01
点数を入力>70
合格です
 -- Press any key to exit (Input "c" to continue) --
------
パターン2
------
>java PassJudge01
点数を入力>64
 -- Press any key to exit (Input "c" to continue) --
------リーズ Java入門 Java 7