其实打印菱形,关键是明白哪些部位打空格,哪些打星号,原理是通用的,我就不拿你这个代码来解释了,之前正好回答过别人的问题,直接拿来用。
创新互联建站主要从事成都网站制作、成都网站建设、外贸营销网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务文山州,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
----------------
把输入参数定义为棱形的边长(即一边有多少个星号)。然后通过计算每个星号的坐标来控制具体的打印字符。坐标计算见图解。
具体代码如下:
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
{
竖谨 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(" ");
}
}
饥纤局
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 /备态 2; j++) {
if (j i - rows / 2)
System.out.print(" ");
else
System.out.print("*"粗晌);
}
System.out.println();
}
}
}
}
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款