我需要创建一个逐行交替的星号和百分号的计数矩阵。要求用户输入一个数字,星号、百分号和行的数量取决于用户输入。
示例:如果用户输入为 5,则输出将如下所示...
* * * * *
% % % % %
* * * * *
% % % % %
* * * * *
我正在尝试使用 for 循环,并对其进行了编程,以便打印出 5 个星号宽和 5 个高,从而形成一个小正方形,但我不知道如何让每隔一行打印为百分号。
到目前为止,这是我的代码:我的问题低于 7c。
import java.util.Scanner;
public class Project1
{
public static void main(String[] args)
{
System.out.println("************** Question 1 *************************\n");
System.out.println("*\n");
System.out.println("***************************************************\n");
System.out.println("************** Question 2 *************************\n");
char perc = '%';
char ast = '*';
System.out.println(ast+"\n");
System.out.println("***************************************************\n");
System.out.println("************** Question 3 *************************\n");
for (int i = 0; i<=9; i++)
{
System.out.print(ast);
}
System.out.println("\n");
System.out.println("***************************************************");
System.out.println();
System.out.println("************** Question 4 *************************");
System.out.println();
for (int i =0; i<=9; i++)
{
System.out.println(ast);
}
System.out.println();
System.out.println("***************************************************");
System.out.println();
System.out.println("************** Question 5 *************************");
Scanner keyboardInput = new Scanner(System.in);
System.out.println("Please enter a positive number :");
int count = keyboardInput.nextInt();
for (int i=0; i<=count;i++)
{
System.out.print(ast);
}
System.out.println("\n");
System.out.println("***************************************************\n");
System.out.println("************** Question 6 *************************\n");
for (int i=1;i<=count;i++)
{
System.out.print(ast);
if (i%5==0)
{
System.out.println();
}
}
System.out.println();
System.out.println();
System.out.println("***************************************************\n");
System.out.println("************** Question 7a ************************\n");
System.out.println("***************************************************\n");
System.out.println("************** Question 7b ************************\n");
for (int h=0;h<count;h++)
{
for (int w=0;w<count;w++)
{
System.out.print(ast);
}
System.out.println();
}
System.out.println();
System.out.println("***************************************************\n");
System.out.println("************** Question 7c ************************\n");
for (int h=0; h<count; h++)
{
for(int w=0; w<count; w++)
{
System.out.print(ast);
}
System.out.println();
}
}
}