Temel olarak, içinde sıfırlanmış tamsayılarla (0-15) bir 4x4 dizim var.Java'da belirtilen bir sütun 4x4 dizisini döndürme
rotateColumn uygulanmasından sonra14 0 1 12
13 4 2 3
7 6 11 8
5 15 9 10
(3) olarak görünmelidir dizisi aşağıdaki gibidir::
14 0 1 10
13 4 2 12
7 6 11 3
5 15 9 8
I dizisi ise, bu küçük bir görev bu nedenle, örneğin, dizinin belirli bir sütun döndürmektir satır rotasyon yöntemi uygulamak başardı ve kodudur var:
public static void rotateRow(int[][] arr, int row) {
int newCurrent = arr[row][arr.length - 1];
int nextCurrent;
for (int currentIndex = 0; currentIndex < arr.length; currentIndex++) {
nextCurrent = arr[row][currentIndex];
arr[row][currentIndex] = newCurrent;
newCurrent = nextCurrent;
}
}
ben kolon yöntemi için de benzer bir kod denedik ama işe yaramadı:
public static void rotateColumn(int[][] arr, int column) {
int newCurrent1 = arr[column][arr.length - 1];
int nextCurrent1 ;
for (int currentIndex1 = 0; currentIndex1 < arr.length; currentIndex1++){
nextCurrent1 = arr[column][currentIndex1];
arr[column][currentIndex1] = newCurrent1;
newCurrent1 = nextCurrent1;
}
}
Programı için tüm kodu:
public class Puzzle {
public static final int N = 4;
public static final int NUMBER_OF_ROTATIONS = 5;
public static void main(String[] args) {
int[][] puzzle = new int[N][N];
reset(puzzle);
test(puzzle);
reset(puzzle);
scramble(puzzle);
System.out.println("### Testing puzzle game play\n");
play(puzzle);
}
public static void print(int[][] puzzle) {
for (int[] row : puzzle) {
for (int elem : row) {
System.out.printf("%4d", elem);
}
System.out.println();
}
System.out.println();
}
public static void test(int[][] puzzle) {
System.out.println("### Testing reset method\n");
print(puzzle);
System.out.println("### Testing rotate methods\n");
print(puzzle);
for (int i = 0; i < N; i++) {
System.out.println("### rotateColumn(" + i + ")\n");
rotateColumn(puzzle, i);
print(puzzle);
System.out.println("### rotateRow(" + i + ")\n");
rotateRow(puzzle, i);
print(puzzle);
}
reset(puzzle);
System.out.println("### Testing random rotations\n");
print(puzzle);
for (int i = 0; i < 5; i++){
randomRotation(puzzle);
print(puzzle);
}
}
public static void reset(int[][] puzzle) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
puzzle[i][j] = i * N + j;
}
}
public static void scramble(int[][] puzzle) {
for (int i = 0; i < NUMBER_OF_ROTATIONS; i++) {
randomRotation(puzzle);
}
}
public static void rotateRow(int[][] arr, int row) {
int newCurrent = arr[row][arr.length - 1];
int nextCurrent;
for (int currentIndex = 0; currentIndex < arr.length; currentIndex++) {
nextCurrent = arr[row][currentIndex];
arr[row][currentIndex] = newCurrent;
newCurrent = nextCurrent;
}
}
// TODO: Implement method as specified in assignment brief
public static void rotateColumn(int[][] arr, int column) {
int newCurrent1 = arr[column][arr.length - 1];
int nextCurrent1 ;
for (int currentIndex1 = 0; currentIndex1 < arr.length; currentIndex1++){
nextCurrent1 = arr[column][currentIndex1];
arr[column][currentIndex1] = newCurrent1;
newCurrent1 = nextCurrent1;
}
}
// TODO: Implement method as specified in assignment brief
public static void randomRotation(int[][] puzzle) {
}
// TODO: Implement method as specified in assignment brief
static void play(int[][] puzzle) {
}
}
birisi Emin düzgün çalıştığını yapma hakkı yapmanız gerekenler işaret olabilir mi? Sizden çok daha fazla :).
Çıktınız nedir? – robotlos
Aldığım çıkış, önceki satır döndürme işleminin aynı kalmasına rağmen, döndürülen sütun yerine, yalnızca o sütundaki tam sayıların kendi aralarında değişmesini istediğimde, rasgele sayılar sütundaki tamsayılarla değiştiriliyor. –
"rotateColumn" işlevini çalıştırarak, int yeniCurrent1 = arr [column] [arr.length - 1]; ' – robotlos