我需要創建星號和百分號的計數,通過計數矩陣,按行交替。用戶被要求輸入數字和星號,百分號數量和行依賴於用戶輸入。創建計數通過計數矩陣
舉例:如果 用戶輸入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();
}
}
}
檢查行號是否是被二整除,並選擇基礎上的字符。 – chrylis