Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用*星打印Z形棱锥体_Java - Fatal编程技术网

Java 使用*星打印Z形棱锥体

Java 使用*星打印Z形棱锥体,java,Java,我正在尝试编写一个程序,该程序使用for循环输出一个Z模式,即nnumber of* 例如: Enter a number: 6 ****** * * * * ****** 这是我目前的代码,它产生了一个半金字塔倒置 import java.util.*; public class ZShape { public static void main(String[] args) { Scanner input = new Scanner(System.i

我正在尝试编写一个程序,该程序使用for循环输出一个Z模式,即
n
number of
*

例如:

Enter a number: 6

******
    *
   *
  *
 *
******
这是我目前的代码,它产生了一个半金字塔倒置

import java.util.*;

public class ZShape {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);

      System.out.print("Enter a number: ");
      int n = input.nextInt(); 

      for (int x = 0; x <= n; x++) {
         for (int y = n; y >= 1; y--) {
            if (y > x) {
               System.out.print("* ");
            }
            else
               System.out.print(" ");
         } 
         System.out.println(); 
      }      
   }
}
import java.util.*;
公共类ZShape{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入一个数字:”);
int n=input.nextInt();
对于(int x=0;x=1;y--){
如果(y>x){
系统输出打印(“*”);
}
其他的
系统输出打印(“”);
} 
System.out.println();
}      
}
}

用三个循环代替怎么样

for (int x = 0; x < n; x++) {
    System.out.print("*");
}
System.out.println();
for (int x = n-3; x >= 0; x--) {
    for (int y = x; y >= 0; y--) {
        System.out.print(" ");
    }
    System.out.println("*");
}
for (int x = 0; x < n; x++) {
    System.out.print("*");
}
for(int x=0;x=0;x--){
对于(int y=x;y>=0;y--){
系统输出打印(“”);
}
System.out.println(“*”);
}
对于(int x=0;x
这是以下代码中的逻辑:

  • 循环输出的每一行(因此从0到
    n
    排除,这样我们就有
    n
    行)
  • 循环输出的每一列(因此从0到
    n
    被排除,因此我们有
    n
    列)
  • 只有在第一行(
    x==0
    )或最后一行(
    x==n-1
    )或列位于对角(
    column==n-1-row
    )时,才需要打印
    *
代码:

(请注意,此输出的每一行都有尾随空格,您没有指定是否应包含这些空格,但可以通过添加另一个检查来轻松删除它们)。

公共类星号{
公共静态void main(字符串[]args){

对于(int i=0;i,这里是打印Z形状的逻辑:

  • i=1
    时,第一行将仅打印“#”
  • i=n
    时,最后一行将以相同的方式仅以“#”打印
  • i=j
    时,这意味着它将只执行一条以“#”为单位的对角线
  • 代码:

    publicstaticvoidmain(字符串[]args){
    扫描仪scr=新扫描仪(System.in);
    int n=scr.nextInt();
    对于(int i=1;i=1;j--){
    如果(i==1 | | i==n | | i==j){
    系统输出打印(“#”);
    }
    否则{
    系统输出打印(“”);
    }
    }
    System.out.println();
    }
    }
    
    您当前的代码生成了什么结果?它生成了一个倒置的半三角形。使用此信息编辑您的问题。我自己,我会使用4个循环,一个用于打印第一行,两个用于打印嵌套循环,一个用于打印对角线,另一个用于打印最后一行。谢谢!也感谢您的精彩解释,这将是真实的我很乐意帮忙。
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = input.nextInt();
        for (int row = 0; row < n; row++) {
            for (int column = 0; column < n; column++) {
                if (row == 0 || row == n - 1 || column == n - 1 - row) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    
    public class Star {
    public static void main(String[] args) {
    
          for (int i = 0; i <=4; i++) {
                for (int j = 0; j <=4; j++) 
                {
                    if (i==4 ||   (i+j)==4 || i==0) 
                    {
                        System.out.print(" * ");
                    } 
                    else 
                    {
                        System.out.print("    ");
                    }
                }
                System.out.println(" ");
            }
    }
    }
    
    public static void main(String[] args) {
        
        Scanner scr = new Scanner(System.in);
        
        int n = scr.nextInt();
        
        for (int i = 1; i <= n; i++) {
            for(int j = n; j >= 1; j--) {
                
                if (i == 1 || i == n || i == j) {
                    System.out.print("# ");
                }
                
                else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }