Documentation Index
Fetch the complete documentation index at: https://v1-learn.neoartd.my.id/llms.txt
Use this file to discover all available pages before exploring further.
138. Basic Arrays 0 - Link
Create an array that can hold ten integers. Fill up each slot of the array with the number -113. Then display the contents of the array on the screen.Do not use a loop. Also, do not use any variable for the index; you must use literal numbers to refer to each slot.Sample OutputSlot 0 contains a -113
Slot 1 contains a -113
Slot 2 contains a -113
Slot 3 contains a -113
Slot 4 contains a -113
Slot 5 contains a -113
Slot 6 contains a -113
Slot 7 contains a -113
Slot 8 contains a -113
Slot 9 contains a -113
public class BasicArrays0 {
public static void main(String[] args) {
int[] arr = new int[10];
arr[0] = -113;
arr[1] = -113;
arr[2] = -113;
arr[3] = -113;
arr[4] = -113;
arr[5] = -113;
arr[6] = -113;
arr[7] = -113;
arr[8] = -113;
arr[9] = -113;
System.out.println("Slot 0 contains a " + arr[0]);
System.out.println("Slot 1 contains a " + arr[1]);
System.out.println("Slot 2 contains a " + arr[2]);
System.out.println("Slot 3 contains a " + arr[3]);
System.out.println("Slot 4 contains a " + arr[4]);
System.out.println("Slot 5 contains a " + arr[5]);
System.out.println("Slot 6 contains a " + arr[6]);
System.out.println("Slot 7 contains a " + arr[7]);
System.out.println("Slot 8 contains a " + arr[8]);
System.out.println("Slot 9 contains a " + arr[9]);
}
}
139. Basic Arrays 1 - Link
Create an array that can hold ten integers. Fill up each slot of the array with the number -113. Then display the contents of the array on the screen.This time, you must use a loop, to put the values in the array and also to display them. Also, in the condition of your loop, you should not count up to a literal number. Instead you should use the length field of your array.Sample OutputSlot 0 contains a -113
Slot 1 contains a -113
Slot 2 contains a -113
Slot 3 contains a -113
Slot 4 contains a -113
Slot 5 contains a -113
Slot 6 contains a -113
Slot 7 contains a -113
Slot 8 contains a -113
Slot 9 contains a -113
public class BasicArrays1 {
public static void main(String[] args) {
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = -113;
System.out.println("Slot " + i + " contains a " + arr[i]);
}
}
}
140. Basic Arrays 2 - Link
Create an array that can hold ten integers. Fill up each slot of the array with a random number from 1 to 100. Then display the contents of the array on the screen. You must use a loop.And, like last time, you must use the length field of your array and not a literal number (like 10) in the condition of the loop.Sample OutputSlot 0 contains a 45
Slot 1 contains a 87
Slot 2 contains a 39
Slot 3 contains a 32
Slot 4 contains a 93
Slot 5 contains a 86
Slot 6 contains a 12
Slot 7 contains a 44
Slot 8 contains a 75
Slot 9 contains a 50
import java.util.Random;
public class BasicArrays2 {
public static void main(String[] args) {
Random r = new Random();
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = 1 + r.nextInt(100);
System.out.println("Slot " + i + " contains a " + arr[i]);
}
}
}
141. Basic Arrays 3 - Link
Create an array that can hold 1000 integers. Fill the array with random numbers in the range 10-99. Then display the contents of the array on the screen. You must use a loop.If you’re careful to only pick random numbers from 10 to 99 and you put two spaces after each number, then your output will line up like mine.And if you’ve forgotten how to pick random numbers in a certain range, you can check out my random ranges visualizer.Sample Output81 69 34 39 35 19 68 14 93 91 96 90 67 82 15 75 13 34 39 88
89 70 53 31 52 79 89 97 36 56 33 61 67 98 57 85 90 55 82 94
50 91 96 73 60 58 56 46 64 71 79 54 40 79 24 68 21 37 55 33
59 46 95 12 98 12 92 78 94 14 17 16 15 99 36 40 20 69 29 42
88 50 31 60 65 57 79 53 30 84 28 97 66 23 88 97 18 86 44 79
90 63 59 97 11 28 57 88 71 89 42 84 28 55 42 13 16 48 58 68
49 92 53 25 26 25 31 53 91 50 73 80 21 86 87 60 63 59 44 39
59 25 46 11 14 21 52 97 35 27 56 49 75 13 30 57 20 74 56 39
35 46 20 62 21 59 87 43 81 24 75 42 79 26 40 19 92 50 57 32
26 71 10 65 62 98 44 69 60 66 51 72 87 24 82 36 81 69 22 88
68 73 28 49 74 15 19 44 11 11 73 55 23 73 30 94 85 92 11 53
12 69 70 17 51 40 57 28 94 93 93 90 64 45 50 90 65 66 15 42
97 69 48 96 15 11 29 19 90 73 76 33 19 14 37 20 15 69 47 58
24 81 84 34 84 26 20 49 98 16 71 47 75 18 28 26 25 21 39 42
17 58 40 10 29 17 55 35 18 59 20 88 49 42 74 65 75 52 41 93
96 19 82 34 29 71 30 86 91 15 36 85 28 24 43 38 17 76 95 30
31 48 15 96 47 56 67 70 87 16 42 96 21 20 75 70 68 11 21 50
55 51 93 56 20 21 89 49 13 33 18 62 72 95 94 62 88 91 26 46
81 28 70 87 43 30 54 82 54 39 68 54 22 99 61 29 77 19 74 87
32 44 65 32 73 88 58 23 32 23 62 27 45 84 44 33 60 23 62 64
59 90 20 49 33 11 48 90 39 49 64 54 21 52 66 61 18 65 97 59
22 53 18 80 12 67 88 57 73 65 14 82 41 73 22 42 58 69 94 87
11 35 64 81 34 30 41 16 99 42 74 96 92 47 62 49 31 34 24 99
22 76 96 15 25 77 49 71 98 60 17 67 86 52 25 17 25 67 17 26
53 42 40 46 10 36 33 67 15 79 48 69 57 91 29 36 50 71 39 10
22 55 76 93 80 48 74 85 26 64 93 83 33 30 65 34 96 12 44 43
36 13 64 59 23 70 87 73 87 12 96 65 83 79 23 92 56 86 39 57
94 46 62 75 60 12 14 15 82 91 43 85 64 73 79 14 38 46 63 82
17 49 56 22 64 10 84 67 66 85 78 22 72 53 33 18 43 90 94 61
92 52 42 95 32 61 92 24 22 23 57 96 27 45 41 97 24 52 74 23
28 52 94 64 24 47 62 48 46 72 27 50 15 37 10 75 66 53 42 81
69 12 76 84 43 79 89 75 76 89 93 13 32 85 67 24 97 65 14 62
10 47 25 62 73 40 71 66 11 81 22 33 22 90 55 39 26 63 14 45
51 71 68 26 63 71 19 95 95 93 18 17 20 56 28 74 23 85 44 83
54 99 77 57 97 14 13 54 44 35 27 55 29 58 41 35 26 23 64 53
11 55 92 10 42 57 73 59 21 92 24 53 65 60 16 26 53 69 49 61
66 87 34 15 19 85 33 96 16 11 57 11 19 38 86 55 21 12 19 21
75 69 92 45 90 96 67 33 88 99 68 76 22 60 60 11 42 62 44 23
65 74 15 92 47 35 83 79 71 86 44 86 68 97 60 66 14 75 45 56
12 98 11 10 16 38 54 83 51 83 49 62 63 24 68 13 31 13 41 34
59 35 12 88 12 21 55 93 27 92 27 14 39 58 31 39 25 94 83 88
70 69 22 71 41 43 34 45 27 26 77 11 39 32 96 78 67 58 54 84
19 26 96 14 29 92 11 28 68 91 93 74 53 13 84 99 70 11 91 70
75 55 87 16 57 17 58 13 75 79 65 51 75 33 87 54 22 63 48 45
71 79 12 40 52 71 62 38 77 36 88 53 94 88 58 20 28 11 48 59
74 33 52 78 76 38 42 20 80 99 53 64 81 79 16 30 18 46 79 70
43 83 14 71 99 64 84 94 20 54 74 40 37 22 96 34 83 85 11 99
64 21 37 80 68 34 17 87 88 53 29 27 42 44 59 37 52 85 64 34
93 23 35 13 46 14 59 87 42 78 52 89 14 52 37 30 74 22 70 79
18 80 32 34 84 98 68 27 89 30 44 86 57 49 51 65 71 30 37 63
import java.util.Random;
public class BasicArrays3 {
public static void main(String[] args) {
Random r = new Random();
int[] arr = new int[1000];
for (int i = 0; i < arr.length; i++) {
arr[i] = 10 + r.nextInt(90);
System.out.print(arr[i] + " ");
if ((i + 1) % 20 == 0) {
System.out.println();
}
}
}
}
142. Copying Arrays - Link
Write a program that creates an array of ten integers. It should put ten random numbers from 1 to 100 in the array. It should copy all the elements of that array into another array of the same size. Then display the contents of both arrays. To get the output to look like mine, you’ll need a several for loops.Create an array of ten integers
- Fill the array with ten random numbers (1-100)
- Copy the array into another array of the same capacity
- Change the last value in the first array to a -7
- Display the contents of both arrays
Sample OutputArray 1: 45 87 39 32 93 86 12 44 75 -7
Array 2: 45 87 39 32 93 86 12 44 75 50
import java.util.Random;
public class CopyingArrays {
public static void main(String[] args) {
Random r = new Random();
int[] arr1 = new int[10];
int[] arr2 = new int[10];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = 1 + r.nextInt(100);
arr2[i] = arr1[i];
}
arr1[arr1.length - 1] = -7;
System.out.print("Array 1: ");
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + " ");
}
System.out.println();
System.out.print("Array 2: ");
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i] + " ");
}
}
}
143. Grades in an Array and a File - Link
Prompt the user for a first and last name, and the name for a file. Randomly choose five grades for that person from 1 to 100 and store them in an array that can hold five integers. Then output the first and last name and those five grades to the specified file.Sample OutputName (first last): Marc Antony
Filename: ettu.txt
Here are your randomly-selected grades (hope you pass):
Grade 1: 54
Grade 2: 90
Grade 3: 18
Grade 4: 37
Grade 5: 62
Data saved in "ettu.txt".
Later, if you look in “ettu.txt” (or whatever you called your file), you should see values like this:Marc Antony
54 90 18 37 62
GradesInAnArrayAndAFile.java
import java.util.Random;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.File;
public class GradesInAnArrayAndAFile {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
Random r = new Random();
System.out.print("Name (first last): ");
String firstName = input.next();
String lastName = input.next();
System.out.print("Filename: ");
String filename = input.next();
int[] grades = new int[5];
for (int i = 0; i < grades.length; i++) {
grades[i] = 1 + r.nextInt(100);
}
FileWriter fw = new FileWriter(new File(filename));
fw.write(firstName + " " + lastName + "\n");
for (int i = 0; i < grades.length; i++) {
fw.write(grades[i] + " ");
}
fw.close();
System.out.println("\nHere are your randomly-selected grades (hope you pass):");
for (int i = 0; i < grades.length; i++) {
System.out.println("Grade " + (i + 1) + ": " + grades[i]);
}
System.out.println("\nData saved in \"" + filename + "\".");
}
}
144. Finding a Value in an Array - Link
Create an array that can hold ten integers, and fill each slot with a different random value from 1-50. Display those values on the screen, and then prompt the user for an integer. Search through the array, and if the item is present, say so. It is not necessary to display anything if the value was not found. If the item is in the array multiple times, it’s okay if the program prints the message more than once.Sample OutputArray: 45 39 32 12 44 50 26 42 16 20
Value to find: 42
42 is in the array.
Array: 45 39 32 12 44 50 26 42 16 20
Value to find: 43
Array: 24 30 31 24 32 33 34 24 35 36
Value to find: 24
24 is in the array.
24 is in the array.
24 is in the array.
FindingAValueInAnArray.java
import java.util.Random;
import java.util.Scanner;
public class FindingAValueInAnArray {
public static void main(String[] args) {
Random r = new Random();
Scanner input = new Scanner(System.in);
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = 1 + r.nextInt(50);
}
System.out.print("Array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.print("\nValue to find: ");
int value = input.nextInt();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
System.out.println("\n" + value + " is in the array.");
}
}
}
}
145. How Many Times? - Link
Create an array that can hold ten integers, and fill each slot with a different random value from 1-50. Display those values on the screen, and then prompt the user for an integer. Search through the array, and count the number of times the item is found.Sample OutputArray: 45 39 32 12 44 50 26 42 16 20
Value to find: 42
42 was found 1 times.
Array: 45 39 32 12 44 50 26 42 16 20
Value to find: 43
43 was found 0 times.
Array: 24 30 31 24 32 33 34 24 35 36
Value to find: 24
24 was found 3 times.
import java.util.Random;
import java.util.Scanner;
public class HowManyTimes {
public static void main(String[] args) {
Random r = new Random();
Scanner input = new Scanner(System.in);
int[] arr = new int[10];
System.out.print("Array: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = 1 + r.nextInt(50);
System.out.print(arr[i] + " ");
}
System.out.print("\nValue to find: ");
int value = input.nextInt();
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
count++;
}
}
System.out.println("\n" + value + " was found " + count + " times.");
}
}
146. Is It There or Not? - Link
Create an array that can hold ten integers, and fill each slot with a different random value from 1-50. Display those values on the screen, and then prompt the user for an integer. Search through the array, and if the item is present, say so. If the value is not in the array, display a single message saying so. Just like the previous assignment, if the value is present more than once, you may display the message as many times as necessary.Sample OutputArray: 45 39 32 12 44 50 26 42 16 20
Value to find: 42
42 is in the array.
Array: 45 39 32 12 44 50 26 42 16 20
Value to find: 43
43 is not in the array.
import java.util.Random;
import java.util.Scanner;
public class IsItThereOrNot {
public static void main(String[] args) {
Random r = new Random();
Scanner input = new Scanner(System.in);
int[] arr = new int[10];
System.out.print("Array: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = 1 + r.nextInt(50);
System.out.print(arr[i] + " ");
}
System.out.print("\nValue to find: ");
int value = input.nextInt();
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
found = true;
}
}
if (found) {
System.out.println("\n" + value + " is in the array.");
} else {
System.out.println("\n" + value + " is not in the array.");
}
}
}
147. Where Is It? - Link
Create an array that can hold ten integers, and fill each slot with a different random value from 1-50. Display those values on the screen, and then prompt the user for an integer. Search through the array, and if the item is present, give the slot number where it is located. If the value is not in the array, display a single message saying so. If the value is present more than once, you may either display the message as many times as necessary, or display a single message giving the last slot number in which it appeared.Sample OutputArray: 45 39 32 12 44 50 42 26 16 20
Value to find: 42
42 is in slot 6.
Array: 45 39 32 12 44 50 26 42 16 20
Value to find: 43
43 is not in the array.
Array: 24 30 31 24 32 33 34 24 35 36
Value to find: 24
you may display either
24 is in slot 0.
24 is in slot 3.
24 is in slot 7.
or
24 is in slot 7.
import java.util.Random;
import java.util.Scanner;
public class WhereIsIt {
public static void main(String[] args) {
Random r = new Random();
Scanner input = new Scanner(System.in);
int[] arr = new int[10];
System.out.print("Array: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = 1 + r.nextInt(50);
System.out.print(arr[i] + " ");
}
System.out.print("\nValue to find: ");
int value = input.nextInt();
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
System.out.println("\n" + value + " is in slot " + i + ".");
found = true;
}
}
if (!found) {
System.out.println("\n" + value + " is not in the array.");
}
}
}
148. Finding the Largest Value in an Array - Link
Write a program that creates an array which can hold ten values. Fill the array with random numbers from 1 to 100. Display the values in the array on the screen. Then use a linear search to find the largest value in the array, and display that value.Sample OutputArray: 45 87 39 32 93 86 12 44 75 50
The largest value is 93
FindingTheLargestValueInAnArray.java
import java.util.Random;
public class FindingTheLargestValueInAnArray {
public static void main(String[] args) {
Random r = new Random();
int[] arr = new int[10];
int max = 0;
System.out.print("Array: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = 1 + r.nextInt(100);
System.out.print(arr[i] + " ");
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("\n\nThe largest value is " + max);
}
}
149. Locating the Largest Value in an Array - Link
Write a program that creates an array which can hold ten values. Fill the array with random numbers from 1 to 100. Display the values in the array on the screen. Then use a linear search to find the largest value in the array, and display that value and its slot number.Sample OutputArray: 45 87 39 32 93 86 12 44 75 50
The largest value is 93
It is in slot 4
import java.util.Random;
public class LocatingLargest {
public static void main(String[] args) {
Random r = new Random();
int[] arr = new int[10];
int max = 0;
int slot = 0;
System.out.print("Array: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = 1 + r.nextInt(100);
System.out.print(arr[i] + " ");
if (arr[i] > max) {
max = arr[i];
slot = i;
}
}
System.out.println("\n\nThe largest value is " + max);
System.out.println("It is in slot " + slot);
}
}
150. Giving an Array a Bunch of Values at Once - Link
Write a program that creates an array which can hold ten values. Fill the array with random numbers from 1 to 100. Display the values in the array on the screen. Then use a linear search to find the largest value in the array, and display that value and its slot number.Files NeededSample OutputThe first array is filled with the following values:
alpha bravo charlie
After you add in the code you’re supposed to, you should see something more like this:The first array is filled with the following values:
alpha bravo charlie delta echo
The second array is filled with the following values:
11 23 37 41 53
GivingAnArrayABunchOfValuesAtOnce.java
public class GivingAnArrayABunchOfValuesAtOnce {
public static void main(String[] args) {
String[] arr1 = {"alpha", "bravo", "charlie", "delta", "echo"};
int[] arr2 = {11, 23, 37, 41, 53};
System.out.println("The first array is filled with the following values:");
for (int i = 0; i < arr1.length; i++) {
System.out.print(" " + arr1[i]);
}
System.out.println("\nThe second array is filled with the following values:");
for (int i = 0; i < arr2.length; i++) {
System.out.print(" " + arr2[i]);
}
}
}
151. Parallel Arrays - Link
Create three arrays to store data about five people. The first array should be Strings and should hold their last names. The next array should be doubles and should hold a grade average (on a 100-point scale). The last array should be ints, should hold their student id numbers.Give each of the arrays values (using array initializers). Then print the values of all three arrays on the screen.Finally, ask the user for an ID number to lookup. Search through the ID array until you find that ID, and then print out the values from the same slot number of the other two arrays.Sample OutputValues:
Mitchell 99.5 123456
Ortiz 78.5 813225
Luu 95.6 823669
Zimmerman 96.8 307760
Brooks 82.7 827131
ID number to find: 307760
Found in slot 3
Name: Zimmerman
Average: 96.8
ID: 307760
import java.util.Scanner;
public class ParallelArrays {
public static void main(String[] args) {
String[] names = {"Mitchell", "Ortiz", "Luu", "Zimmerman", "Brooks"};
double[] grades = {99.5, 78.5, 95.6, 96.8, 82.7};
int[] ids = {123456, 813225, 823669, 307760, 827131};
System.out.println("Values:");
for (int i = 0; i < names.length; i++) {
System.out.println("\t" + names[i] + " " + grades[i] + " " + ids[i]);
}
Scanner input = new Scanner(System.in);
System.out.print("\nID number to find: ");
int id = input.nextInt();
for (int i = 0; i < ids.length; i++) {
if (ids[i] == id) {
System.out.println("\nFound in slot " + i);
System.out.println("\tName: " + names[i]);
System.out.println("\tAverage: " + grades[i]);
System.out.println("\tID: " + ids[i]);
}
}
}
}
152. Tic-Tac-Toe - Link
Code an interactive, two-player game of Tic-Tac-Toe. You’ll use a two-dimensional array of chars.Starter CodeSample Output(...a game already in progress)
X O
O X X
X O
'O', choose your location (row, column): 0 1
X O O
O X X
X O
'X', choose your location (row, column): 2 0
X O O
O X X
X X O
The game is a tie.
import java.util.Scanner;
public class TicTacToe
{
private static char[][] board = new char[3][3];
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
initBoard();
displayBoard();
char player = 'X';
int turns = 0, row, col;
while (true)
{
turns++;
System.out.print( "\n'" + player + "', choose your location (row, column): " );
row = keyboard.nextInt();
col = keyboard.nextInt();
if ( board[row][col] == ' ' )
{
board[row][col] = player;
}
else
{
System.out.println("That slot is already taken.");
continue;
}
displayBoard();
if ( checkWinner(player) )
{
System.out.println("Player " + player + " wins!");
break;
}
if ( turns == 9 )
{
System.out.println("The game is a tie.");
break;
}
if ( player == 'X' )
player = 'O';
else
player = 'X';
}
}
public static void initBoard()
{
// fills up the board with blanks
for ( int r=0; r<3; r++ )
for ( int c=0; c<3; c++ )
board[r][c] = ' ';
}
public static void displayBoard()
{
System.out.println(" 0 " + board[0][0] + "|" + board[0][1] + "|" + board[0][2]);
System.out.println(" --+-+--");
System.out.println(" 1 " + board[1][0] + "|" + board[1][1] + "|" + board[1][2]);
System.out.println(" --+-+--");
System.out.println(" 2 " + board[2][0] + "|" + board[2][1] + "|" + board[2][2]);
System.out.println(" 0 1 2 ");
}
public static void displayBoard2()
{
for ( int r=0; r<3; r++ )
{
System.out.print("\t"+r+" ");
for ( int c=0; c<3; c++ )
{
System.out.print( board[r][c] + " " );
}
System.out.println();
}
System.out.println("\t 0 1 2 ");
}
public static boolean checkWinner(char player)
{
for ( int i=0; i<3; i++ )
{
if ( board[i][0] == player && board[i][1] == player && board[i][2] == player )
return true;
if ( board[0][i] == player && board[1][i] == player && board[2][i] == player )
return true;
}
if ( board[0][0] == player && board[1][1] == player && board[2][2] == player )
return true;
if ( board[0][2] == player && board[1][1] == player && board[2][0] == player )
return true;
return false;
}
}
153. Hangman - Link
Write a program to play a word-guessing game like Hangman.
- It must randomly choose a word from a list of words.
- It must stop when all the letters are guessed.
- It must give them limited tries and stop after they run out.
- It must display letters they have already guessed (either only the incorrect guesses or all guesses).
Sample Output-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: _ _ _ _ _ _ _ _ _
Misses:
Guess: e
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: _ e _ _ _ _ _ _ _
Misses:
Guess: i
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: _ e _ i _ _ _ _ _
Misses:
Guess: a
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: _ e _ i a _ _ a _
Misses:
Guess: r
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: _ e _ i a _ _ a _
Misses: r
Guess: s
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: _ e _ i a _ _ a _
Misses: rs
Guess: t
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: _ e _ i a t _ a _
Misses: rs
Guess: n
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: _ e _ i a t _ a n
Misses: rs
Guess: o
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: _ e _ i a t _ a n
Misses: rso
Guess: l
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: l e _ i a t _ a n
Misses: rso
Guess: v
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: l e v i a t _ a n
Misses: rso
Guess: h
-=-=-=-=-=-=-=-=-=-=-=-=-=-
Word: l e v i a t h a n
Misses: rso
YOU GOT IT!
Play "again" or "quit"? quit
import java.util.Scanner;
import java.util.Random;
public class Hangman {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random r = new Random();
String[] words = {"leviathan", "gargantuan", "behemoth", "kraken", "hydra"};
String word = words[r.nextInt(words.length)];
String guess = "";
String misses = "";
int tries = 7;
System.out.println("Welcome to Hangman!");
while (true) {
System.out.println("\n=-=-=-=-=-=-=-=-=-=-=-=-");
System.out.println("\nWord: " + displayWord(word, guess));
System.out.println("\nMisses: " + misses);
System.out.print("\nGuess: ");
char letter = input.next().charAt(0);
if (word.contains(String.valueOf(letter))) {
guess += letter;
} else {
misses += letter;
tries--;
}
if (word.equals(displayWord(word, guess).replace(" ", ""))) {
System.out.println("\n=-=-=-=-=-=-=-=-=-=-=-=-");
System.out.println("\nWord: " + displayWord(word, guess));
System.out.println("\nMisses: " + misses);
System.out.println("\nYOU GOT IT!");
break;
}
if (tries == 0) {
System.out.println("\nYou ran out of tries!");
break;
}
}
System.out.print("\nPlay \"again\" or \"quit\"? ");
String choice = input.next();
if (choice.equals("again")) {
main(null);
}
}
public static String displayWord(String word, String guess) {
String result = "";
for (int i = 0; i < word.length(); i++) {
if (guess.contains(String.valueOf(word.charAt(i)))) {
result += word.charAt(i);
} else {
result += "_";
}
result += " ";
}
return result;
}
}