其實(shí)打印菱形,關(guān)鍵是明白哪些部位打空格,哪些打星號,原理是通用的,我就不拿你這個代碼來解釋了,之前正好回答過別人的問題,直接拿來用。
創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)文山州,十多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
----------------
把輸入?yún)?shù)定義為棱形的邊長(即一邊有多少個星號)。然后通過計(jì)算每個星號的坐標(biāo)來控制具體的打印字符。坐標(biāo)計(jì)算見圖解。
具體代碼如下:
public?class?Main
{
public?static?void?main(String[]?args)
{
printHollowRhombus(10);
}
public?static?void?printHollowRhombus(int?size)
{
for?(int?i?=?0;?i??size;?i++)
{
for?(int?j?=?0;?j??size?-?i?+?2?*?i;?j++)
{
if?(j?==?size?-?i?-?1?||?j?==?size?+?i?-?1)
{
System.out.print("*");
}
else
{
豎謹(jǐn)??System.out.print("?");
}
}
System.out.println("");
}
//此爛讓處如果改成i=1,那就是一個完全尖的棱形
for?(int?i?=?0;?i??size;?i++)
{
for?(int?j?=?0;?j??2?*?size?-?i?-?1;?j++)
{
if?(j?==?i?||?j?==?2?*?size?-?i?-?1?-?1)
{
System.out.print("*");
}
else
{
System.out.print("?");
}
}
饑?yán)w局?????
System.out.println("");
}
}
}
public class Test {
public static void main(String[] args) {
Rectangle r = new Rectangle(5, 4);
System.out.println("Circle for rectangle is: " + r.circle());
System.out.println("Area for rectangle is: " + r.area());
}
}
class Rectangle{
private double length;
private double width;
public Rectangle(double length, double width){
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public void setLength(double length) {
this.length = length;
}
public void setWidth(double width) {
this.width = width;
}
public double circle(){
return 2 * (width + length);
}
public double area(){
return width * length;
}
}
------------------------
Circle for rectangle is: 18.0
Area for rectangle is: 20.0
public class Rhombus {
public static void main(String[] args) {
int rows = 11;
for (int i = 0; i rows; i++) {
if (i = rows / 2) {
for (int j = 0; j = rows / 2 + i; j++) {
if (j rows /仿凳源 2 - i)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
} else {
for (int j = 0; j rows - i + rows /備態(tài) 2; j++) {
if (j i - rows / 2)
System.out.print(" ");
else
System.out.print("*"粗晌);
}
System.out.println();
}
}
}
}