本文目录一览:
- 1、java 怎样写出一个菱形程序?空心和实心
- 2、java打印菱形不是很懂,跪求每句的意思和作用。
- 3、Java利用for循环打印一个菱形,实心的,边长为n
- 4、java 输入一个正数,打印一个空两格的左半菱形?
- 5、怎么用java打印菱形?
java 怎样写出一个菱形程序?空心和实心
java打印菱形通过二维数组来打印,实例如下:
int hangshu = 11;
int yiban = hangshu / 2 + 1;
int yibanduo = hangshu / 2;
System.out.println("空心菱形:");
for(int k = 1;k = yiban;k++){
for(int i = 1;i = (yiban - k);i++){
System.out.print(" ");
}
System.out.print("*");
for(int i = 1;i = ( (k - 2) * 2 + 1);i++){
System.out.print(" ");
}
if(k != 1){
System.out.print("*");
}
System.out.println();
}
for(int k = yibanduo;k =1;k--){
for(int i = 1;i = (yiban - k);i++){
System.out.print(" ");
}
System.out.print("*");
for(int i = 1;i = ( (k - 2) * 2 + 1);i++){
System.out.print(" ");
}
if(k != 1){
System.out.print("*");
}
System.out.println();
}
System.out.println("实心菱形:");
for(int i = 1;i = hangshu;i++){
if(i = hangshu/2 + 1){
for(int k = 1;k = hangshu/2 + 1- i;k++){
System.out.print(" ");
}
for(int k = 1;k = i;k++){
System.out.print("* ");
}
System.out.println();
}else{
for(int k =1;k = (i -(hangshu/2 + 1));k++){
System.out.print(" ");
}
for(int k = 1;k = (2 *(hangshu/2 + 1) - i);k++){
System.out.print("* ");
}
System.out.println();
}
}
结果如下:
java打印菱形不是很懂,跪求每句的意思和作用。
import java.util.Scanner;
class 菱形 {
public static void main(String args[]) {
int row;
Scanner keyin = new Scanner(System.in);
System.out.print("请输入菱形的行数:");
row = keyin.nextInt(); //输入的数字整数!
for (int i = 1; i = row / 2 + 1; i++) {//上半部分三角形:不用担心输入偶数,因为折半+1了;
repeat(" ", row / 2 + 1 - i);//空格:注意这是递减
repeat("*", 2 * i - 1);//星:注意这是递减
System.out.println();//打完一排换行!
}
//下半部分三角形原理同上!
for (int i = row / 2; i = 1; i--) {
repeat(" ", row / 2 + 1 - i);//递增
repeat("*", 2 * i - 1);//递减!
System.out.println();//换行!
}
}
public static void repeat(String c, int m) {
for (int i = 1; i = m; i++)
System.out.print(c);
}
/**
*
* 这个程序整体思路:把菱形分成上下两个三角形在打印,因为菱形是奇数
* 上半部分采用了折半+1的算法,所有你输入任何数都不怕!
* 如果5,上面就是3下面就是2
* 如果6上面就是4下面就是3
* 空格采用的算法与打出来的星,相反方式:一个递增一个递减!
*
*
*/
}
Java利用for循环打印一个菱形,实心的,边长为n
按照你的要求编写的用for循环打印边长为n的实心菱形的Java程序如下(Math.abs 是数学类的求绝对值的函数)
import java.util.Scanner;
public class AA {
public static void main(String[] args) {
int i,j,n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
for(i=1;i=2*n-1;i++){
for(j=1;j=Math.abs(n-i);j++){
System.out.print(" ");
}
for(j=1;j=2*n-1-2*Math.abs(n-i);j++){
System.out.print("*");
}
System.out.println();
}
}
}
运行结果
输入 5
输出
java 输入一个正数,打印一个空两格的左半菱形?
Java利用for循环输出空心菱形的实例代码
编写程序,在控制台上输出空心菱形,对角距离为6.
代码如下:public class Diamond { public static void main(String[] args) { printHollowRhombus(6); } public static void printHollowRhombus(int size) { if (size % 2 == 0) { size++;// 计算菱形大小 } for (int i = 0; i size / 2 + 1; i++) { for (int j = size / 2 + 1; j i + 1; j--) { System.out.print(" ");// 输出左上角位置的空白 } for (int j = 0; j 2 * i + 1; j++) { if (j == 0 || j == 2 * i) { System.out.print("* ");// 输出菱形上半部边缘 } else { System.out.print(" ");// 输出菱形上半部空心 } } System.out.println(""); //换行 } for (int i = size / 2 + 1; i size; i++) { for (int j = 0; j i - size / 2; j++) { System.out.print(" ");// 输出菱形左下角空白 } for (int j = 0; j 2 * size - 1 - 2 * i; j++) { if (j == 0 || j == 2 * (size - i - 1)) { System.out.print("* ");// 输出菱形下半部边缘 } else { System.out.print(" ");// 输出菱形下半部空心 } } System.out.println(""); //换行 } } }
效果如图:
怎么用java打印菱形?
java打印菱形方法:
package circulationDemo;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入要打印的行数:");
int num = input.nextInt();
t1(num);// 正直角
split();
t2(num);// 倒直角
split();
t3(num);// 等腰三角
split();
t4(num);// 等腰倒三角++
split();
t5(num);// 等腰倒三角--
split();
t6(num);// 实心菱形
split();
t7(num);// 空心菱形(1)
split();
t8(num);// 空心菱形(2)
split();
t9(num);// 空心菱形(3)
}
private static void t9(int num) {
for (int k = 1; k = num; k++) {
for (int i = 1; i = (num - k); i++) {
System.out.print(" ");
}
System.out.print("*");
for (int i = 1; i = ((k - 2) * 2 + 1); i++) {
System.out.print(" ");
}
if (k != 1) {
System.out.print("*");
}
System.out.println();
}
for (int k = num-1; k = 1; k--) {
for (int i = 1; i = (num - k); i++) {
System.out.print(" ");
}
System.out.print("*");
for (int i = 1; i = ((k - 2) * 2 + 1); i++) {
System.out.print(" ");
}
if (k != 1) {
System.out.print("*");
}
System.out.println();
}
}
private static void t8(int num) {
// 上半部分
for (int i = 1; i = num; i++) {
// 打印上半部分空格
for (int j = 1; j = num - i; j++) {
System.out.print(" ");
}
// 打印原本实心的*部分
for (int j = 1; j = 2 * i - 1; j++) {
// 仅在一行的开头和末尾打印*
if (j == 1 || j == 2 * i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// 下半部分
for (int i = 1; i = num - 1; i++) {
for (int j = 1; j = i; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * (num - i - 1) + 1; j++) {
if (j == 1 || j == 2 * (num - i - 1) + 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
private static void t7(int num) {
// 打印上半部分
for (int i = 1; i = num; i++) {
// 打印空格
for (int j = 1; j = (num - i); j++) {
System.out.print(" ");
}
// 打印*的个数
if (i == 1) {
System.out.print("*");
} else {
System.out.print("*");
// 打印菱形里的空格
for (int j = 1; j = (2 * i - 3); j++) {
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
// 打印下半部分
for (int i = num - 1; i 0; i--) {
// 打印空格
for (int j = 1; j = (num - i); j++) {
System.out.print(" ");
}
// 打印*的个数
if (i == 1) {
System.out.print("*");
} else {
System.out.print("*");
// 打印菱形里的空格
for (int j = 1; j = (2 * i - 3); j++) {
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
}
private static void t6(int num) {
// 上半部分
for (int i = 1; i = num; i++) {
for (int j = 1; j = num - i; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
// 下半部分
for (int i = 1; i = num - 1; i++) {
for (int j = 1; j = i; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * (num - 1 - i) + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
private static void t5(int num) {
for (int i = num; i 0; i--) {
for (int j = 1; j = num - i; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
private static void t4(int num) {
for (int i = 1; i = num; i++) {
for (int j = 1; j = i - 1; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * (num - i) + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
private static void t3(int num) {
for (int i = 1; i = num; i++) {
for (int j = 1; j = num - i; j++) {
System.out.print(" ");
}
for (int j = 1; j = 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
private static void split() {
System.out.println("---------------");
}
private static void t2(int num) {
for (int i = 1; i = num; i++) {
for (int j = 1; j = num - i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
private static void t1(int num) {
for (int i = 1; i = num; i++) {
for (int j = 1; j = i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}