Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.IOException;
- public class SimpleDotComTestDrive {
- public static void main(String[] args) {
- SimpleDotCom dot = new SimpleDotCom();
- int[] locations = {2, 3, 4};
- dot.setLocationCells(locations);
- String userGuess = "2";
- String result = dot.chekYourself(userGuess);
- }
- }
- class SimpleDotCom {
- int[] locationCells;
- int numOfHits = 0;
- public void setLocationCells(int[] locs) {
- locationCells = locs;
- }
- public String chekYourself(String stringGuess) {
- int guess = Integer.parseInt(stringGuess);
- String result = "Мимо";
- for (int cell : locationCells) {
- if (guess == cell) {
- result = "Попал";
- numOfHits++;
- break;
- }
- }
- if (numOfHits == locationCells.length) {
- result = "Потопил";
- }
- System.out.println(result);
- return result;
- }
- }
- public class SimpleDotComGame {
- public static void main(String[] args) {
- int numOfGuesses = 0;
- GameHelper helper = new GameHelper();
- SimpleDotComGame gameDot = new SimpleDotComGame();
- int randomNum = (int) (Math.random() * 5);
- boolean isAlive = true;
- while (isAlive == true) {
- String guess = helper.getUserInput("Введите число");
- String result = theDotCom.chekYourself(guess);
- numOfGuesses++;
- if (result.equals("Потопил")) {
- isAlive = false;
- System.out.println("Вам потребовалось " + numOfGuesses + "попыток");
- }
- }
- }
- }
- //import java.io.*;
- class GameHelper {
- public String getUserInput(String prompt) {
- String inputLine = null;
- System.out.print(prompt + " ");
- inputLine = is.readLine();
- if (inputLine.length() == 0) return null;
- } catch(
- IOException e)
- {
- System.out.println("IOException: " + e);
- }
- return inputline;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement