Tableau de sortie (Jeu Penny Pitch) (Java)
Avoir un peu de mal.
Ce programme lance 5 pièces sur un tableau de chiffres. Le tableau est affiché comme suit:
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
J'ai donc du mal à trouver un moyen de sortir un 'P' ou n'importe quelle lettre vraiment sur les cellules sur lesquelles la "pièce" a atterri. Je pense qu'un booléen fonctionnerait, pas sûr cependant. Cela ressemblerait à ceci:
1 P 1 1 1
1 2 2 2 P
P 2 3 P 1
1 P 2 2 1
1 1 1 1 1
Une petite section de code dans ce programme additionnerait alors le nombre sur lequel les pièces ont atterri
(J'ai déjà fait cette partie)
import java.util.Scanner;
import java.util.Random;
public class PennyPitch{
public static void main(String [] args){
Scanner reader = new Scanner(System.in);
Random gen = new Random();
int total = 0;
Boolean p = true;
int [][] array = {{1,1,1,1,1},
{1,2,2,2,1},
{1,2,3,2,1},
{1,2,2,2,1},
{1,1,1,1,1}};
System.out.println("Press Enter to commence penny operation.");
String Enter = reader.nextLine();
for (int row = 0; row < 1; row++){
for (int col = 0; col < 5; col++){
System.out.print(array[row][col] + " ");
}
}
System.out.print("\n");
for (int row = 1; row < 2; row++){
for (int col = 0; col < 5; col++){
System.out.print(array[row][col] + " ");
}
}
System.out.print("\n");
for (int row = 2; row < 3; row++){
for (int col = 0; col < 5; col++){
System.out.print(array[row][col] + " ");
}
}
System.out.print("\n");
for (int row = 3; row < 4; row++){
for (int col = 0; col < 5; col++){
System.out.print(array[row][col] + " ");
}
}
System.out.print("\n");
for (int row = 4; row < 5; row++){
for (int col = 0; col < 5; col++){
System.out.print(array[row][col] + " ");
}
}
int penny1 = array[gen.nextInt(5)][gen.nextInt(5)];
int penny2 = array[gen.nextInt(5)][gen.nextInt(5)];
int penny3 = array[gen.nextInt(5)][gen.nextInt(5)];
int penny4 = array[gen.nextInt(5)][gen.nextInt(5)];
int penny5 = array[gen.nextInt(5)][gen.nextInt(5)];
for (int index = 1; index < 4; index++){
if (penny1 == index){
total = total + index;
}
if (penny2 == index){
total = total + index;
}
if (penny3 == index){
total = total + index;
}
if (penny4 == index){
total = total + index;
}
if (penny5 == index){
total = total + index;
}
}
System.out.print("\nPress Enter for total");
String Enter2 = reader.nextLine();
System.out.print("\ntotal is: " + total + "\n");
System.out.print("\nPress Enter to display where the pennies landed");
String Enter3 = reader.nextLine();
}
}
0
Author: Jeremy Lin, 2017-03-06