如何找出我Java程序崩溃的原因?

huangapple go评论58阅读模式
英文:

How can I find out why my Java program keeps crashing?

问题

以下是您要求的翻译内容:

我已经学习了一个星期的Java,我没有其他经验。程序一直崩溃,我希望在输入不正确时能自动再次询问。

import java.util.Scanner;

public class test {

    public static void main(String[] args) {
        boolean error;
        Scanner scan = new Scanner(System.in);

        do {
            error = false;
            System.out.print("告诉我年份:");
            if (scan.hasNextInt()) {
                int jahr = scan.nextInt();
                scan.close();
                if (isLeapYear(jahr)) {
                    System.out.println("请输入闰年");
                } else {
                    System.out.println("这不是一个闰年");
                }

            } else {
                System.err.println("请输入整数年份!");
                error = true;
                scan.close();
            }
        } while (error);
    }

    public static boolean isLeapYear(int year) {
        // Task 1
        if (year % 400 == 0) {
            return true;
        } else if (year % 100 == 0) {
            return false;
        } else if (year % 4 == 0) {
            return true;
        }
        return false;
    }
}
英文:

I have been learning Java for a week, I have no other experiences. The program keeps crashing, I would like to be automatically asked again in the event of an incorrect entry.

import java.util.Scanner;

public class test {

    public static void main(String[] args) {
        boolean error;
        Scanner scan = new Scanner(System.in);

        do {
            error = false;
        System.out.print("Tell me the Year: ");
        if (scan.hasNextInt()) {
            int jahr = scan.nextInt();
            scan.close();
            if (schaltjahre(jahr)) {

                System.out.println("Please enter the leap year ");
            } else {
                System.out.println("It is not a leap year");
            }

        } else {
            System.err.println("enter an integer year!");
            error = true;
            scan.close();
        }
    }while (error);
    }
   

    public static boolean schaltjahre(int jahr) {
        // Aufgabe 1
        if (jahr % 400 == 0) {
            return true;
        } else if (jahr % 100 == 0) {
            return false;
        } else if (jahr % 4 == 0) {
            return true;
        }
        return false;
    }

答案1

得分: 1

你应该在循环内部实例化扫描器,并且不要在else语句中关闭它。
你的代码应该类似于这样:

public static void main(String[] args) {
    boolean error;
    Scanner scan ;

    do {
        error = false;
        scan = new Scanner(System.in);
        System.out.print("告诉我年份:");
        if (scan.hasNextInt()) {
            int jahr = scan.nextInt();
            scan.close();
            if (schaltjahre(jahr)) {
                System.out.println("请输入闰年");
            } else {
                System.out.println("这不是闰年");
            }
        } else {
            System.err.println("请输入一个整数年份!");
            error = true;
        }
    } while (error);
}

注意:这里的翻译结果只包括代码部分的翻译,不包括问题中的内容。

英文:

You should instantiate the scanner inside the loop and don't close it in the else statement.
Your code should be something like this :

	public static void main(String[] args) {
	boolean error;
	Scanner scan ;

	do {
		error = false;
		scan = new Scanner(System.in);
		System.out.print("Tell me the Year: ");
		if (scan.hasNextInt()) {
			int jahr = scan.nextInt();
			scan.close();
			if (schaltjahre(jahr)) {

				System.out.println("Please enter the leap year ");
			} else {
				System.out.println("It is not a leap year");
			}

		} else {
			System.err.println("enter an integer year!");
			error = true;
		}
	} while (error);
}

huangapple
  • 本文由 发表于 2020年9月25日 00:16:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64050395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定