forked from biblelamp/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW3Lesson.java
More file actions
123 lines (118 loc) · 5.38 KB
/
HW3Lesson.java
File metadata and controls
123 lines (118 loc) · 5.38 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Java. Level 1. Lesson 3. Example of homework
*
* @author Sergey Iryupin
* @version dated Dec 02, 2017
*/
import java.util.*;
import java.io.*;
class HW3Lesson {
static Random random = new Random();
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.print("Make a choice:\n"+
"1. Guess the Number\n2. Guess the Word\n3. Exit\n: ");
switch (sc.next()) {
case "1":
guessTheNumber();
break;
case "2":
guessTheWord();
break;
default:
return;
}
}
}
/**
* 1. Написать программу, которая загадывает случайное число от 0 до 9,
* и пользователю дается 3 попытки угадать это число. При каждой попытке
* компьютер должен сообщить больше ли указанное пользователем число чем
* загаданное, или меньше. После победы или проигрыша выводится запрос
* «Повторить игру еще раз? 1 – да / 0 – нет» (1 – повторить, 0 – нет).
*/
static void guessTheNumber() {
do {
int count = 0;
int guess = -1;
int number = random.nextInt(10);
while (count < 3 && guess != number) {
System.out.print(
"Guess [" + (count + 1) + "] the number (0..9): ");
try {
guess = sc.nextInt();
if (number == guess) {
System.out.println("You won!");
} else {
System.out.println("Your number is " + ((guess > number)?
"greater" : "less."));
count++;
}
} catch (InputMismatchException ex) {
System.out.println("Input Mismatch Exception!");
sc.next();
}
}
if (count == 3)
System.out.println("You lost!");
System.out.print("Repeat the game?\n[1 - yes / 0 - no]: ");
} while (sc.next().equals("1"));
}
/**
* 2. * Прочитать массив слов из файла
* String[] words = {"apple", "orange", "lemon", "banana", "apricot",
* "avocado", "broccoli", "carrot", "cherry", "garlic", "grape",
* "melon", "leak", "kiwi", "mango", "mushroom", "nut", "olive",
* "pea", "peanut", "pear", "pepper", "pineapple", "pumpkin",
* "potato"};
* При запуске программы компьютер загадывает слово, запрашивает ответ
* у пользователя, сравнивает его с загаданным словом и сообщает
* правильно ли ответил пользователь. Если слово не угадано, компьютер
* показывает буквы которые стоят на своих местах.
* apple – загаданное
* apricot - ответ игрока
* ap#############
* (15 символов, чтобы пользователь не мог узнать длину слова)
* Для сравнения двух слов посимвольно, можно пользоваться:
* str.charAt(0) - метод вернет char, который стоит в слове str на
* первой позиции, играем до тех пор, пока игрок не отгадает слово,
* используем только маленькие буквы
*/
static void guessTheWord() {
String[] words = readFromFile(new File("hw3text.txt"));
/*{"apple", "orange", "lemon", "banana", "apricot",
"avocado", "broccoli", "carrot", "cherry", "garlic",
"grape", "melon", "leak", "kiwi", "mango",
"mushroom", "nut", "olive", "pea", "peanut",
"pear", "pepper", "pineapple", "pumpkin", "potato"};*/
String guess = null;
String word = words[random.nextInt(words.length)];
System.out.println(Arrays.toString(words));
do {
System.out.print("Guess the word: ");
guess = sc.next();
for (int i = 0; i < 15; i++)
if (i < word.length() && i < guess.length() &&
word.charAt(i) == guess.charAt(i))
System.out.print(word.charAt(i));
else
System.out.print((word.equals(guess))? "" : "#");
System.out.println();
} while (!word.equals(guess));
}
/**
* reading text file to array using Scanner
*
* @param file object File for reading
* @return String[] array from file
*/
static String[] readFromFile(File file) {
String str = "";
try (Scanner read = new Scanner(file)) {
while (read.hasNext())
str += read.nextLine() + "\n";
} catch (IOException ex) {}
return str.split("\n");
}
}