Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.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中带有for循环的松树_Java_For Loop - Fatal编程技术网

java中带有for循环的松树

java中带有for循环的松树,java,for-loop,Java,For Loop,我用java写了一个松树抽屉。首先,它询问这棵树有多高,然后,询问它在彼此下面画了多少次,最后它画了树干。如果第一个输入是Hello,这可能会有帮助吗?抛出一个异常,告诉您已插入负值 public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int height; int stars; int level; System.ou

我用java写了一个松树抽屉。首先,它询问这棵树有多高,然后,询问它在彼此下面画了多少次,最后它画了树干。如果第一个输入是Hello,这可能会有帮助吗?抛出一个异常,告诉您已插入负值

public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(System.in);
    int height;
    int stars;
    int level;
    System.out.println("Fenyőfarajzoló program.");
    System.out.print("Kérem a magasságot: ");
    height = sc.nextInt();
    System.out.print("Kérem a szintek számát: ");
    level = sc.nextInt();
    int szelesseg = height - 1;
    if (height <= 0) {
        System.out.println("A magasság csak pozitív lehet.");
        throw new Exception("height is negative");
    }
    if (level <= 0) {
        System.out.println("A szintek száma csak pozitív lehet.");
        throw new Exception("level is negative");
    }

        for (int h = 0; h < level; h++) {
            stars = 1;
            for (int i = 0; i < height; i++){
                for (int j = szelesseg; j>i; j-- )
                {
                    System.out.print(" ");
                }
                for (int k = 0; k < stars; k++){
                    System.out.print("*");
                }
                stars += 2;
                System.out.println();
            }
        }

    for (int talp = 1; talp <= 3; talp++) {
        System.out.println("   ***");
    }
}
publicstaticvoidmain(字符串[]args)引发异常{
扫描仪sc=新的扫描仪(System.in);
内部高度;
int星;
智力水平;
System.out.println(“Fenyőfarajzolóprogram.”);
系统输出打印(“Kérem a magasságot:”);
高度=sc.nextInt();
系统输出打印(“Kérem a szintek számát:”);
级别=sc.nextInt();
int szelesseg=高度-1;

if(height Hi,使用此代码如果您为high或level插入负值,它将引发异常并停止运行代码将此immidatly放在height=sc.nextInt()之后;此代码if(height int a;int b;System.out.print(“给出第一个数字”)a=sc.nextInt();if(本课中我无法使用的方法引发异常:System.exit(1)我没有尝试你的解决方案,因为我找到了另一个。当我依次使用两个扫描仪并想停止程序时,我在if末尾使用return。例如:a=sc.nextInt();if(a<0)System.out.print(“负数错误”)return;b=sc.nextInt();if(b>100)System.out.print(“大于100”)返回;希望它在需要时也能帮助您!
public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(System.in);
    int height;
    int stars;
    int level;
    System.out.println("Fenyőfarajzoló program.");
    System.out.print("Kérem a magasságot: ");
    height = sc.nextInt();
    System.out.print("Kérem a szintek számát: ");
    level = sc.nextInt();
    int szelesseg = height - 1;
    if (height <= 0) {
        System.out.println("A magasság csak pozitív lehet.");
        throw new Exception("height is negative");
    }
    if (level <= 0) {
        System.out.println("A szintek száma csak pozitív lehet.");
        throw new Exception("level is negative");
    }

        for (int h = 0; h < level; h++) {
            stars = 1;
            for (int i = 0; i < height; i++){
                for (int j = szelesseg; j>i; j-- )
                {
                    System.out.print(" ");
                }
                for (int k = 0; k < stars; k++){
                    System.out.print("*");
                }
                stars += 2;
                System.out.println();
            }
        }

    for (int talp = 1; talp <= 3; talp++) {
        System.out.println("   ***");
    }
}