输出数组(Penny Pitch Game)(Java)

时间:2017-03-06 16:52:23

标签: java arrays

有点麻烦。

这个程序在一块数字板上扔了5个硬币。该板显示为:

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

因此,我无法想出一种方法来输出'P'或任何字母真正在“硬币”登陆的单元格上。我认为布尔值会起作用,但不确定。它看起来像这样:

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

此程序中的一小部分代码将加上硬币落在的数字上 (我已经做过这部分了)

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 个答案:

没有答案