Guest User

Untitled

a guest
Oct 17th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. package jp.ac.ca.data.input;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.io.IOException;
  6.  
  7. import jp.ac.ca.data.create.CreateRandomNumber;
  8.  
  9. //import java.text.NumberFormat;
  10.  
  11. public class UserInputNumber {
  12.  
  13. /**
  14. * @author nuki このメソッドではユーザからのキーボード入力を受け配列に入力された文字列を代入する
  15. */
  16.  
  17. /*
  18. * 入力回数をcountという変数名で宣言。入力は1回目からなので初期化は1で行う
  19. */
  20. private static int count = 1;
  21.  
  22. public static void inputKyeboard() {
  23.  
  24. // 2つのクラスで同じ定数を宣言してるのは隣のクラスで宣言してるのに呼び方がわからないので・・・
  25. final int ARRAY_SIZE = 4;
  26. boolean flag = true;
  27. /*
  28. *
  29. * 引数System.inを取るインスタンスInputStreamReaderを生成し、
  30. * それを引数に取るBufferReadReaderインスタンスにを生成する。
  31. * そのBufferReadReaderインスタンスをBufferReader型のreaderという変数名に代入する
  32. */
  33. BufferedReader reader = new BufferedReader(new InputStreamReader(
  34. System.in));
  35. // 乱数を生成し、arr[]配列に代入
  36. int arr[] = CreateRandomNumber.randomNumber();
  37.  
  38. // ユーザからの入力に対して例外処理を行う
  39. while (flag) {
  40. try {
  41. // ユーザからの入力を受け付ける前に文章を表示する。ここのcountは入力が何回目かを数えるものである
  42. System.out.println(ARRAY_SIZE + "桁の数字を入力してください" + count + "回目");
  43. // 変数名countをインクリメントする
  44. count++;
  45. // String型の変数名lineに読み込んだテキスト行を代入します。readerインスタンスのreadLineメソッドを代入
  46. String line = reader.readLine();
  47. // int型のarr2という配列を宣言
  48. int[] arr2;
  49. // arr2にARRAY_SIZEの要素が入る配列を生成
  50. arr2 = new int[ARRAY_SIZE];
  51. // ユーザが入力した文字列を1文字づつsubstringメソッドを用いて配列arr2へ代入する。
  52. // 代入する際にString型からint型に変換している
  53. for (int m = 0; m < ARRAY_SIZE; m++) {
  54. arr2[m] = Integer.parseInt(line.substring(m, m + 1));
  55. }
  56.  
  57. // HITとBLOWをカウントするためにh,bという変数名を宣言し0で初期化を行う
  58. int h = 0;
  59. int b = 0;
  60. for (int x = 0; x < ARRAY_SIZE; x++){
  61. for (int y = 0;y < ARRAY_SIZE; y++ ){
  62. if (arr[x] == arr2[y]){
  63. if (x == y){
  64. System.out.println("HIT" + ++h);
  65. } else {
  66. System.out.println("BLOW" + ++b);
  67. }
  68. }
  69.  
  70. }
  71.  
  72. }
  73.  
  74. /* if (arr[0] == arr2[0]) {
  75. System.out.println("HIT" + "arr[0]");
  76. if (arr[1] == arr2[1]){
  77. System.out.println("HIT" + "arr[1]");
  78. if(arr[2] == arr2[2]){
  79. System.out.println("HIT" + "arr[2]");
  80. if (arr[3] == arr2[3]) {
  81. System.out.println("HIT" + "arr[2]");
  82. System.out.println("good end");
  83. flag = false;
  84. }
  85. }
  86. }
  87. }
  88. */
  89. // IOエラーなど予期せぬ例外処理に対する実装
  90. } catch (IOException e) {
  91. System.out.println("test");
  92. // 数値以外の入力n例外処理を実装
  93. } catch (NumberFormatException e) {
  94. System.out.println("入力は数値のみです");
  95. // 上記例外処理で拾えなかった例外処理をここで実装
  96. } catch (Exception e) {
  97. System.out.println("4桁の数値を入力してください");
  98. }
  99.  
  100. }
  101. }
  102.  
  103. }
Add Comment
Please, Sign In to add comment