Advertisement
Manavard

Untitled

Feb 28th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.IOException;
  4.  
  5. public class SimpleDotComTestDrive {
  6. public static void main(String[] args) {
  7. SimpleDotCom dot = new SimpleDotCom();
  8. int[] locations = {2, 3, 4};
  9. dot.setLocationCells(locations);
  10. String userGuess = "2";
  11. String result = dot.chekYourself(userGuess);
  12. }
  13. }
  14.  
  15. class SimpleDotCom {
  16.  
  17. int[] locationCells;
  18. int numOfHits = 0;
  19.  
  20. public void setLocationCells(int[] locs) {
  21. locationCells = locs;
  22. }
  23.  
  24. public String chekYourself(String stringGuess) {
  25. int guess = Integer.parseInt(stringGuess);
  26. String result = "Мимо";
  27. for (int cell : locationCells) {
  28. if (guess == cell) {
  29. result = "Попал";
  30. numOfHits++;
  31. break;
  32. }
  33. }
  34. if (numOfHits == locationCells.length) {
  35. result = "Потопил";
  36. }
  37. System.out.println(result);
  38. return result;
  39. }
  40. }
  41.  
  42. public class SimpleDotComGame {
  43. public static void main(String[] args) {
  44. int numOfGuesses = 0;
  45. GameHelper helper = new GameHelper();
  46. SimpleDotComGame gameDot = new SimpleDotComGame();
  47. int randomNum = (int) (Math.random() * 5);
  48. boolean isAlive = true;
  49. while (isAlive == true) {
  50. String guess = helper.getUserInput("Введите число");
  51. String result = theDotCom.chekYourself(guess);
  52. numOfGuesses++;
  53. if (result.equals("Потопил")) {
  54. isAlive = false;
  55. System.out.println("Вам потребовалось " + numOfGuesses + "попыток");
  56. }
  57. }
  58. }
  59. }
  60. //import java.io.*;
  61.  
  62. class GameHelper {
  63. public String getUserInput(String prompt) {
  64. String inputLine = null;
  65. System.out.print(prompt + " ");
  66. inputLine = is.readLine();
  67. if (inputLine.length() == 0) return null;
  68. } catch(
  69. IOException e)
  70.  
  71. {
  72. System.out.println("IOException: " + e);
  73. }
  74. return inputline;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement