Weitere ähnliche Inhalte Ähnlich wie Javaプログラミング入門【第2回】 (20) Mehr von Yukiko Kato (18) Javaプログラミング入門【第2回】15. 名前と年齢を尋ねるプログラム
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HowOldAreYou {
public static void main(String[] args) {
System.out.println("あなたの名前は?");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
String line = reader.readLine();
System.out.println(line + "さん、こんにちは");
System.out.println("年齢は?");
line = reader.readLine();
int age = Integer.parseInt(line);
System.out.println("今、" + age + "歳とすると、20年後は" + (age + 20) + "歳ですね");
} catch (IOException e) {
System.out.println(e);
} catch (NumberFormatException e) {
System.out.println("年齢が正しくありません");
}
}
}
17. 解説
• readLineメソッド
▫ String line = reader.readLine();
▫ 文字列を1行分取得するメソッド
• readerは?
▫ BufferedReader型の変数
▫ 参照型のクラス
▫ データの読み込みを行うためのクラス
• new
▫ newの後にクラス名と()を付けると、そのクラスに属
するオブジェクト生成を行う
→これをインスタンス生成と言う
21. コメント
• 一行コメント
▫ // (スラッシュ・スラッシュ)
• 通常コメント
▫ /* ~*/ (スラッシュ・アスタリスク)
• ドキュメンテーションコメント
▫ /** ~*/ (スラッシュ・アスタリスク)
▫ JDKのJavadocというツールを使って自動的にド
キュメントを生成する機能
24. 比較演算子
Java
数学
意味
p == 50
p = 50
pが50に等しいならtrue、それ以外はfalse
p != 50
p ≠ 50
pが50に等しくないならtrue、それ以外はfalse
p >= 50
p ≧ 50
pが50以上であればtrue、それ以外はfalse
p <= 50
p ≦ 50
pが50以下であればtrue、それ以外はfalse
p > 50
p > 50
pが50より大きいならtrue、それ以外はfalse
p < 50
p < 50
pが50より小さいならtrue、それ以外false
40. 解説
• 初期化:int i = 0;
▫ 繰り返しの準備。繰り返し前に一度だけ実行され
る
• 条件式:i < 4;
▫ 繰り返しを続ける条件
• 次の一歩:i++;
▫ 繰り返す処理が1回終わった後に必ず実行される
処理
43. 変数の有効範囲(スコープ)
pubic static void main(String[] args) {
for (int i = 0; i < 4; i++) {
System.out.println(i);
}
System.out.println(“i = ” + i);
↑iは有効範囲外なのでコンパイルエラー
}
• iはfor文の初期化部分で宣言されているため、
括弧の外では使用できない
46. 0から3までを表示する
Public class While1 {
public static void main(String[] args) {
int i = 0;
while (i < 4) {
System.out.println(i);
i++;
}
System.out.println(“end”);
}
}
47. 入力をそのまま出力する
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CopryDisplay {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
try {
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
} catch (IOException e) {
System.out.println(e);
}
}
}
49. 複雑な条件式
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CopyOfCopryDisplay2 {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
try {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println(e);
}
}
}
50. 解説
• 条件式(line = reader.readLine()) != null
▫ 入力された文字を変数lineに代入し、その値を
nullと比較する
51. 大文字を小文字に変換する
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
try {
String line;
while ((line = reader.readLine()) != null) {
String s = line.toLowerCase();
System.out.println(s);
}
} catch (IOException e) {
System.out.println(e);
}
}
54. Stringクラスにあるメソッドの一部
メソッド
動作
String replace(char oldChar, char
newChar)
文字列のoldCharをすべてnewCharに置換
した新しい文字列を返却する
String substring(int beginIndex)
文字列のbeginIndex番目の文字以降からな
る新しい文字列を返却する
String substring(int beginIndex, int
endIndex)
文字列のbegineIndex番目~endIndex-1番
目の文字からなる新しい文字列を返却する
String toLowerCase()
文字列中の大文字を全て小文字に変換した
新しい文字列を返却する
String toString()
文字列自体を返却する
String toUpperCase()
文字列中の小文字を全て大文字に変換した
新しい文字列を返却する
String trim()
文字列の両端からホワイトスペースを取り
除いた新しい文字列を返却する
62. 文字列検索関連のStringクラスのメ
ソッド
• char charAt(int index)
• boolean equals(Object object), boolean
equalsIgnoreCase(String string)
• int compareTo(String anotherString)
• boolean startsWith(String prefix, int
index), boolean startsWith(String prefix)
• boolean endWith(String suffix)
• int indexOf(ch ch), int lastIndexOf(int ch)
• int indexOf(String s), int lastIndexOf(String s)
• 詳しくは『java api string』でググる(http://eclass.center.yuge.ac.jp/jdk_docs/ja/api/java/lang/
String.html)
65. 演習12
• 以下のプログラムをwhile文で書き換えなさい
public class DrawGraph {
public static void main(String[] args) {
for (int i =0; i < 10; i++) {
System.out.println(i + “:”);
for (int j = 0; j < i; j++) {
System.out.print (“*”);
}
System.out.println(“”);
}
}
}
71. ContinueTest.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ContinueTest {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
String line;
while ((line = reader.readLine()) != null) {
if (!line.endsWith("{") && !line.endsWith("}")) {
continue;
}
System.out.println(line);
}
} catch (IOException e) {
System.out.println(e);
}
}
}