良田百顷,不在一亩;但有远志,不在当归
//儒以文乱法,而侠以武犯禁
package test;
import java.util.Arrays;
public class Days {
public static void main(String[] args) {
String[] sky = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
String[] ground = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
int count = 0;
int n = 0;
String[] year = new String[60];
for (int i = 0; i < sky.length; i++) {
for (int j = 0; j < ground.length; j++) {
if ((i & 1) == 1 && (j & 1) == 1||(i & 1) == 0 && (j & 1) == 0) {
year[j] = sky[i] + ground[j];
System.out.print(year[j] + " ");
count++;
n++;
if (n == 6) {
System.out.print("\n---------------------------------\n");
n = 0;
}
}
}
}
System.out.println("count = " + count);
}
}
用数组存贮总犯错,原因未知,用list就没问题
import java.util.ArrayList;
public class Days {
public static void main(String[] args) {
String[] sky = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
String[] ground = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
ArrayList<String> list = new ArrayList<>();
int count = 0;
int n = 0;
String[] year = new String[60];
String[] copy = new String[60];
for (int i = 0; i < sky.length; i++) {
for (int j = 0; j < ground.length; j++) {
if ((i & 1) == 1 && (j & 1) == 1 || (i & 1) == 0 && (j & 1) == 0) {
year[j] = sky[i] + ground[j];
list.add(year[j]);
System.out.print(year[j] + " ");
count++;
n++;
if (n == 6) {
System.out.print("\n---------------------------------\n");
n = 0;
}
}
for (int k = 0; k < copy.length; k++) {
copy[k] = year[j];
}
}
}
int m = 0;
System.out.print("count = " + count);
System.out.print("\n---------------------------------------------------\n");
for (String s : list) {
System.out.print((s) + " ");
m++;
if (m == 12) {
System.out.print("\n---------------------------------------------------\n");
m = 0;
}
}
int p = 0;
System.out.print("count = " + count);
System.out.print("\n---------------------------------------------------\n");
for (int i = 0; i < year.length; i++) {
System.out.print(year[i] + " ");
p++;
if (p == 12) {
System.out.print("\n---------------------------------------------------\n");
p = 0;
}
}
int q = 0;
System.out.print("count = " + count);
System.out.print("\n---------------------------------------------------\n");
for (int i = 0; i < copy.length; i++) {
System.out.print(copy[i] + " ");
q++;
if (q == 12) {
System.out.print("\n---------------------------------------------------\n");
q = 0;
}
}
}
}