Java 程序 - Patternmaker |如何让奇数列显示?
Java Program - Patternmaker | How to make odd columns show?
为了我的家庭作业,我需要使用 for 嵌套循环制作一个模式。它需要看起来像这样:
pattern for 5 rows and 7 columns
但是,当我编写代码时,我无法弄清楚如何使列(如果是奇数)出现。我认为我需要添加另一个 If/else 语句。这是我的代码:
public class PatternMaker {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Declaration
int numrows = 5;
int numcols = 7;
String str1= "XX";
String str2 = "OO";
String sep = "***";
if (numcols % 2 == 0) { // if columns is even
// for each row
for (int i = 1; i <= numrows; i++) {
// for each column
for (int k = 0; k < numcols/ 2; k++) {
if ( i % 2 == 1) { // for odds
if(k > 0) {
System.out.print(sep + str1 + sep+ str2);
}
else
System.out.print(str1 + sep + str2);
}
else { // for evens
if(k > 0) {
System.out.print(sep + str2 + sep + str1);
}
else
System.out.print(str2 + sep + str1);
}
}
System.out.println();
}
}
else { // if columns is odd
// for each row
for (int i = 1; i <= numrows; i++) {
// for each column
for (int k = 0; k < (numcols/ 2); k++) {
if ( i % 2 == 1) { // for odds
if(k > 0) {
System.out.print(sep + str1 + sep+ str2);
}
else
System.out.print(str1 + sep + str2);
}
else { // for evens
if(k > 0) {
System.out.print(sep + str2 + sep + str1);
}
else
System.out.print(str2 + sep + str1);
}
}
System.out.println();
}
}
给你:
// Declaration
int numrows = 5;
int numcols = 7;
String str1 = "XX";
String str2 = "OO";
String sep = "***";
for (int i = 0; i < numrows; i++) {
for (int j = 0; j < numcols; j++) {
if (j != 0) {
System.out.print(sep);
}
if (i % 2 == 0) {
if (j % 2 == 0) {
System.out.print(str1);
} else {
System.out.print(str2);
}
} else if (j % 2 == 0) {
System.out.print(str2);
} else {
System.out.print(str1);
}
}
System.out.println("");
}
为了我的家庭作业,我需要使用 for 嵌套循环制作一个模式。它需要看起来像这样: pattern for 5 rows and 7 columns
但是,当我编写代码时,我无法弄清楚如何使列(如果是奇数)出现。我认为我需要添加另一个 If/else 语句。这是我的代码:
public class PatternMaker {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Declaration
int numrows = 5;
int numcols = 7;
String str1= "XX";
String str2 = "OO";
String sep = "***";
if (numcols % 2 == 0) { // if columns is even
// for each row
for (int i = 1; i <= numrows; i++) {
// for each column
for (int k = 0; k < numcols/ 2; k++) {
if ( i % 2 == 1) { // for odds
if(k > 0) {
System.out.print(sep + str1 + sep+ str2);
}
else
System.out.print(str1 + sep + str2);
}
else { // for evens
if(k > 0) {
System.out.print(sep + str2 + sep + str1);
}
else
System.out.print(str2 + sep + str1);
}
}
System.out.println();
}
}
else { // if columns is odd
// for each row
for (int i = 1; i <= numrows; i++) {
// for each column
for (int k = 0; k < (numcols/ 2); k++) {
if ( i % 2 == 1) { // for odds
if(k > 0) {
System.out.print(sep + str1 + sep+ str2);
}
else
System.out.print(str1 + sep + str2);
}
else { // for evens
if(k > 0) {
System.out.print(sep + str2 + sep + str1);
}
else
System.out.print(str2 + sep + str1);
}
}
System.out.println();
}
}
给你:
// Declaration
int numrows = 5;
int numcols = 7;
String str1 = "XX";
String str2 = "OO";
String sep = "***";
for (int i = 0; i < numrows; i++) {
for (int j = 0; j < numcols; j++) {
if (j != 0) {
System.out.print(sep);
}
if (i % 2 == 0) {
if (j % 2 == 0) {
System.out.print(str1);
} else {
System.out.print(str2);
}
} else if (j % 2 == 0) {
System.out.print(str2);
} else {
System.out.print(str1);
}
}
System.out.println("");
}