Question: So i have my code all figured out and it does exactly what it needs to but i need to add a loop in the
So i have my code all figured out and it does exactly what it needs to but i need to add a loop in the main method to ask the user if they want to play again... if the first letter of their input is n then that means no and then the program ends. But if they say yes then the program has to start over again. Again this loop (i assume) has to go in the main method.
import java.util.Random;
import java.util.Scanner;
//FIXME class header comment
public class MineSweeper {
public static void main(String[] args) {
System.out.println("Welcome to Mine Sweeper!");
Scanner in = new Scanner(System.in);
String widthString = "What width of map would you like ";
String heightString = "What height of map would you like ";
int max = Config.MAX_SIZE;
int min = Config.MIN_SIZE;
int width = promptUser(in, widthString + "(" + min +" - " + max + "): ", min, max);
int height = promptUser(in, heightString + "(" + min +" - " + max + "): ", min, max);
char [][] map = new char[height][width];
eraseMap(map);
simplePrintMap(map);
String rowPrompt = "row: ";
String columnPrompt = "column: ";
int row = promptUser(in, rowPrompt, 1, height);
int column = promptUser(in, columnPrompt, 1, width);
map[row-1][column-1]= Config.NO_NEARBY_MINE;
simplePrintMap(map);
*NEED HELP HERE MAKING PLAY AGAIN LOOP
}
public static int promptUser(Scanner in, String prompt, int min, int max) {
int userInput = -99;
System.out.print(prompt);
while(1==1)
if(in.hasNextInt()) {
userInput=in.nextInt();
in.nextLine();
if(userInput>=min && userInput<=max) {
break;
}
System.out.println("Expected a number from "+min+" to "
+max+".");
}else {
System.out.println("Expected a number from "+min+" to "+max+
".");
in.next();
in.nextLine();
}
return userInput;
}
public static void eraseMap(char[][] map) {
for (int i = 0; i < map.length; ++i) {
for (int j = 0; j < map[i].length; ++j) {
map[i][j] = Config.UNSWEPT;
}
}
}
public static void simplePrintMap(char[][] map) {
for(int i=0;i< map.length; ++i) {
for(int j=0; j< map[i].length; ++j) {
System.out.print(" " + map[i][j]);
}
System.out.println();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
