以下の問題は穴埋め問題です。
IDEなどにコピーしてかまいません。
1.
俳句の文字数がルールに沿っているかどうか(17文字か?)を判定するプログラムである。
プログラムを完成させなさい。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Kadai1 { public static void main(String args[]) { int stringLength; String s = "ふるいけやかわずとびこむみずのおと"; stringLength = //ここに答えを書く; if (stringLength > 17) { System.out.println("字余りです。"); } else if (stringLength < 17) { System.out.println("字足らずです"); } else { System.out.println("ちょうど17文字です。"); } } } |
2.
ユーザーが入力したパスワードが正しいパスワード「abc」と同一かどうかの判定をするプログラムである。
間違いを正しなさい。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; public class Kadai2 { public static void main(String[] args) { String password = "abc"; String passwordIn; System.out.print("Enter your password please:"); Scanner sc = new Scanner(System.in); passwordIn = sc.next(); sc.close(); if (password == passwordIn) { System.out.println("ログインしました。"); } else { System.out.println("パスワードが違います。"); } } } |
3.
次のプログラムは人工無能と英会話するゲームのプロトタイプである。
このゲームは、キーボードから入力された英単語をコンピューターが定型文を付加して繰り返し、「END」が押されると終了する。
プログラムを完成させなさい。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; public class Kadai3 { public static void main(String args[]) { String s = "START"; Scanner sc = new Scanner(System.in); while (!s.isEmpty()) { System.out.print("Let's talk. (I will end when you enter nothing):"); s = sc.nextLine(); if (!s.isEmpty()) { System.out.println("It's really " + s + " isn't it?"); } } System.out.println("It was fun. see you soon!"); sc.close(); } } |
4.
ユーザーが入力した2つの英単語のアルファベット順(辞書順の前後関係)を判定するプログラムである。
プログラムを完成させなさい。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Scanner; public class Kadai4 { public static void main(String args[]) { String str1, str2; System.out.print("Input a English keyword:"); Scanner sc1 = new Scanner(System.in); str1 = sc1.next(); System.out.print("Input another keyword:"); Scanner sc2 = new Scanner(System.in); str2 = sc2.next(); int comp = /*ここに答えを書く*/ ; if (comp < 0) { System.out.println("\"" + str1 + "\"" + " is the word in front of " + "\"" + str2 + "\""); } else if (comp > 0) { System.out.println("\"" + str1 + "\"" + " is the word after the " + "\"" + str2 + "\""); } else { System.out.println("The two words are the same."); } } } |
5.
メールアドレスからユーザー名のみ抽出するプログラムである。(ユーザー名の長さはそれぞれ異なるものとする)
プログラムを完成させなさい。
ヒント:StringクラスのindexOf()メソッドとsubstring()メソッドを使う。
1 2 3 4 5 6 7 8 9 |
public class Kadai5 { public static void main(String[] args) { String email = "katsuya_imai@saycon.co.jp"; //★ここにコードを書く } } |
(出力例)
ユーザー名:katsuya_imai ドメイン名:saycon.co.jp |
6.
CSV形式をタブ区切り形式(TSV)に変換するプログラムである。
以下を完成させなさい。
1 2 3 4 5 6 7 8 9 |
public class Kadai6 { public static void main(String[] args) { String csv = "1,111-1111,愛知県名古屋市,山田太郎,090-999-99XX"; //★ここにコードを書く System.out.println(csv); System.out.println(tsv); } } |
(出力例)
1 111-1111 愛知県名古屋市 山田太郎 090-999-99XX |
7.StringBuilderクラスの意義
次のプログラムを実行して、その意味を考えなさい。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class SpeedTest { public static void main(String[] args) { final int COUNT = 10_000; String st = null; long startTime = System.currentTimeMillis(); for (int i = 0; i < COUNT; i++) { String str = Integer.toString(i); st += str; } long endTime = System.currentTimeMillis(); long sTime = endTime - startTime; StringBuilder sb = new StringBuilder(); startTime = System.currentTimeMillis(); for (int j = 0; j < COUNT; j++) { sb.append(Integer.toString(j)); } endTime = System.currentTimeMillis(); long sbTime = endTime - startTime; System.out.println("Stringを使った文字列の結合:" + sTime + "ミリ秒"); System.out.println("StringBuilderを使った文字列の結合 " + sbTime + "ミリ秒"); } } |
8.エスケープシーケンス
次の文章をコンソールに表示しなさい。
彼は言った。「\3,000円のお買い上げです」
私は”あなた”に言っているのです。
私は’あなた’に言っているのです。
9.オリジナル問題作成
将来の後輩のために良い問題が出来たら教えてください。
以上。