I have to create a peghole game program that will display the numbers 1 through 10 then prompt the user to choose a number to set to 0. The 1-10 are all stored in an arraylist and if the user tries to make an element that was already set to 0 by them, set to 0 again then an if statement will display the message "Peghole is already filled!". How do I compare the value of the element the user picked to 0? I'm trying to accomplish this in the method peg_hole.
import java.util.*;
import java.util.Scanner;
public class PegBoardGame {
public static ArrayList create_pegboard(){
//for loop and add method to holes 1-10
for(){
}
}
public static void print_pegboard(ArrayList pegboard) {
//print results from array 1-10 or final result
System.out.println("-----------------------------------------");
System.out.println(pegboard);
System.out.println("-----------------------------------------");
}
public static Integer peg_hole(ArrayList pegboard){
Scanner in = new Scanner(System.in);
//variable for user input
int holetofillInt;
int checkInt;
//prompt for input
System.out.println("Select a peghole 1-10 to fill");
holetofillInt = in.nextInt();
//if peg chosen by user is already 0 then print error message
checkInt = pegboard[holetofillInt];
if( ){
System.out.println("Peghole is already filled!");
}
else{
//set selected hole to 0
pegboard.set(holetofillInt,0);
return holetofillInt;
}
}
public static void main(String[] args) {
//create array list with peghole numbers
ArrayList<Integer> pegboard = create_pegboard();
//print the pegboard unchanged
print_pegboard(pegboard);
//construct array list up to 10
for (int i = 0; i < 10; i++) {
//see if they want to change and change what hole is peggged
peg_hole(pegboard);
//print changed peghole board
print_pegboard(pegboard);
}
}
}
Aucun commentaire:
Enregistrer un commentaire