2013-09-24 130 views
0

我需要創建星號和百分號的計數,通過計數矩陣,按行交替。用戶被要求輸入數字和星號,百分號數量和行依賴於用戶輸入。創建計數通過計數矩陣

舉例:如果 用戶輸入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(); 
    } 
    } 
} 
+1

檢查行號是否是被二整除,並選擇基礎上的字符。 – chrylis

回答

0
java pseudo code 
--------------------- 
for(int i=0;i<=n;i++) 
{ 

String x; 
if(i%2==0) 
x="*"; 
else 
x="%" 

for(int i=0;i<=n;i++) 
{ 
System.out.println(x+"\t"); 
} 
System.out.println("\n"); 
} 
0

模運算符和三元運算表達式形成共同的和強大的團隊是這樣的:

String sym = (i % 2) ? "%" : "*"; 

您的代碼可能看起來像:

for(int i=0; i<count; i++) 
{ 
    String sym = (i % 2) = "%" : "*"; 
    for(j=0; j<count; j++) 
    { 
     // Up to you 
    } 
} 

char可能是一個更合適的類型sym,但是)

0

print語句應該有條件地採取行動。

for (int i = 0; i<= count; i++) 
    { 
    if(i%2 == 0) 
     System.out.print(ast); 
    else 
     System.out.print(perc); 
    }