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

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

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

问题

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

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

  1. import java.util.Scanner;
  2. public class test {
  3. public static void main(String[] args) {
  4. boolean error;
  5. Scanner scan = new Scanner(System.in);
  6. do {
  7. error = false;
  8. System.out.print("告诉我年份:");
  9. if (scan.hasNextInt()) {
  10. int jahr = scan.nextInt();
  11. scan.close();
  12. if (isLeapYear(jahr)) {
  13. System.out.println("请输入闰年");
  14. } else {
  15. System.out.println("这不是一个闰年");
  16. }
  17. } else {
  18. System.err.println("请输入整数年份!");
  19. error = true;
  20. scan.close();
  21. }
  22. } while (error);
  23. }
  24. public static boolean isLeapYear(int year) {
  25. // Task 1
  26. if (year % 400 == 0) {
  27. return true;
  28. } else if (year % 100 == 0) {
  29. return false;
  30. } else if (year % 4 == 0) {
  31. return true;
  32. }
  33. return false;
  34. }
  35. }
英文:

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.

  1. import java.util.Scanner;
  2. public class test {
  3. public static void main(String[] args) {
  4. boolean error;
  5. Scanner scan = new Scanner(System.in);
  6. do {
  7. error = false;
  8. System.out.print("Tell me the Year: ");
  9. if (scan.hasNextInt()) {
  10. int jahr = scan.nextInt();
  11. scan.close();
  12. if (schaltjahre(jahr)) {
  13. System.out.println("Please enter the leap year ");
  14. } else {
  15. System.out.println("It is not a leap year");
  16. }
  17. } else {
  18. System.err.println("enter an integer year!");
  19. error = true;
  20. scan.close();
  21. }
  22. }while (error);
  23. }
  24. public static boolean schaltjahre(int jahr) {
  25. // Aufgabe 1
  26. if (jahr % 400 == 0) {
  27. return true;
  28. } else if (jahr % 100 == 0) {
  29. return false;
  30. } else if (jahr % 4 == 0) {
  31. return true;
  32. }
  33. return false;
  34. }

答案1

得分: 1

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

  1. public static void main(String[] args) {
  2. boolean error;
  3. Scanner scan ;
  4. do {
  5. error = false;
  6. scan = new Scanner(System.in);
  7. System.out.print("告诉我年份:");
  8. if (scan.hasNextInt()) {
  9. int jahr = scan.nextInt();
  10. scan.close();
  11. if (schaltjahre(jahr)) {
  12. System.out.println("请输入闰年");
  13. } else {
  14. System.out.println("这不是闰年");
  15. }
  16. } else {
  17. System.err.println("请输入一个整数年份!");
  18. error = true;
  19. }
  20. } while (error);
  21. }

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

英文:

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

  1. public static void main(String[] args) {
  2. boolean error;
  3. Scanner scan ;
  4. do {
  5. error = false;
  6. scan = new Scanner(System.in);
  7. System.out.print("Tell me the Year: ");
  8. if (scan.hasNextInt()) {
  9. int jahr = scan.nextInt();
  10. scan.close();
  11. if (schaltjahre(jahr)) {
  12. System.out.println("Please enter the leap year ");
  13. } else {
  14. System.out.println("It is not a leap year");
  15. }
  16. } else {
  17. System.err.println("enter an integer year!");
  18. error = true;
  19. }
  20. } while (error);
  21. }

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:

确定