ödev
unknown
java
3 years ago
2.9 kB
10
Indexable
package homework2;
import java.util.Scanner;
import java.util.Random;
public class HomeworkBY {
static int total = 0;
public static void game(String[] array ) {
Random rand = new Random();
Scanner input = new Scanner(System.in);
int random = rand.nextInt(array.length);
String word = array[random];
for(int i=0; i<word.length();i++) {
System.out.print("*");
}
System.out.println();
int inGame = 0;
int cueCount = 0;
while (inGame != 1) {
System.out.print("Enter 1 to answer or 2 to request a letter: ");
inGame= input.nextInt();
if (inGame == 1) {break;};
if (inGame != 2) {System.out.println("Invalid input");System.out.print("Enter 1 to answer or 2 to request a letter: "); continue;};
cueCount++;
char[] letters = new char[word.length()];
for (int i = 0; i < word.length(); i++) {
letters[i] = word.charAt(i);
}
int cue = rand.nextInt(word.length());
for(int i=0; i<word.length();i++) {
if(i == cue) System.out.print(letters[cue]);
else{System.out.print("*");}
}
System.out.println();
}
System.out.print("Enter your guess: ");
String guess = input.next();
if (word.equals(guess)) {
int win = (word.length()-cueCount)*100;
System.out.println("Point for the round: "+win);
total = total + win;
System.out.println("Total Points: "+total);
}
else {
int lose = (word.length()-cueCount)*-100;
System.out.println("Point for the round: "+lose);
total = total + lose;
System.out.println("Total Points: "+total);
}
}
public static void main(String[] args) {
Random rand = new Random();
Scanner input = new Scanner(System.in);
int numInput = 0;
String [] colors = {"black", "white", "red", "blue", "yellow", "pink", "purple", "brown", "orange", "green"};
String [] animals = {"dog", "cat", "panda", "bear", "lion", "elephant", "tiger", "giraffe", "rat", "whale"};
String [] cars = {"fiat", "bmw", "mercedes", "toyota", "honda", "tesla", "ferrari", "nissan", "mazda", "dodge"};
System.out.println("WORD GAME");
while (numInput != 4) {
System.out.println("1: Colors");
System.out.println("2: Animals");
System.out.println("3: Car Brands");
System.out.println("4: Exit");
numInput = input.nextInt();
if (numInput < 1 || numInput > 4) {System.out.println("Please enter betweeen 1-4");continue;};
if (numInput == 1) {
game(colors);
}
if (numInput == 2) {
game(animals);
}
if (numInput == 3) {
game(cars);
}
}
System.out.println("Total Points: "+ total);
}
}Editor is loading...