Proposition
Design and write a Java application that takes as input an integer and a character from the screen. The output should be a diamond on the screen composed of the character and extending for the width specified by the integer. For example, if the integer is 11 and the character is an asterisk (*), the diamond would look like this:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Code
int j = 0;
int m;
int n = 10;
if (n/2==0) {
m = n/2;
}
else {
m = (n/2)+1;
}
int[] a = new int [m];
for (int i=1;i<=n;i++) {
if (i%2!=0) {
a[j] = i;
j++ ;
}
}
for (int k=0; k<m; k++) {
for (int i=0; i<a[k]; i++) {
print ("*") ;
}
println () ;
}
if (n%2!=0) {
for (int c=m-2; c>=0; c--) {
for (int i=0; i<a[c]; i++) {
print ("*") ;
}
println () ;
}
}
else {
for (int d=m-1; d>=0; d--) {
for (int i=0; i<a[d]; i++) {
print ("*") ;
}
println () ;
}
}
Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2
ไม่มีความคิดเห็น:
แสดงความคิดเห็น