0

我知道网站上已经有很多关于这个问题的问题,但我仍然无法在我的程序中掌握它。

import java.util.Scanner;
public class time {
public static void main (String[] args) {
    int a, sec, min, hour;

    System.out.println ("Please enter the number of seconds");
    Scanner user_input = new Scanner (System.in);
    a = user_input.nextInt();

    while (!user_input.hasNextInt()) {
        System.out.println ("Error: INVALID INPUT, please try again");
        System.out.println("Please enter the number of seconds");
        a = user_input.nextInt();
    }

    hour = a/3600;
    min = ((a%3600)/60);
    sec = ((a%3600)%60);

    System.out.println(hour + " hours "+ min+ " minutes " + sec + " seconds");

}
}

所以上面是我的程序。我刚开始学习 JAVA,所以我尽了最大努力,但每次运行这个程序时,它什么也没给我。

我输入了一个数字,没有任何反应,但程序仍在运行。

奇怪的是..当我改变

!user_input.hasNextInt() 

a<0,它可以工作,但这对我来说还不够好,因为它无法检查双输入或空输入。

4

5 回答 5

1

请尝试:

import java.util.Scanner;

public class time {
    public static void main(String[] args) {
            long sec, min, hour;

            System.out.println("Please enter the number of seconds");
            Scanner user_input = new Scanner(System.in);

            sec = -1;

            while (user_input.hasNextLong() && (sec = user_input.nextLong()) < 0) {
                    System.out.println("Error: INVALID INPUT, please try again");
                    System.out.println("Please enter the number of seconds");
            }

            if (sec > 0) {
                    hour = sec / 3600;
                    min = ((sec % 3600) / 60);
                    sec = ((sec % 3600) % 60);

                    System.out.println(hour + " hours " + min + " minutes " + sec + " seconds");
            }

    }
}
于 2013-10-05T19:15:59.753 回答
0

这将适用于所有条件:

import java.util.InputMismatchException;
import java.util.Scanner;

public class time{
public static void main (String[] args) {
    int a = 0, sec, min, hour;
    boolean cond = true;

    while(cond){
    boolean mis = false;
    System.out.println ("Please enter the number of seconds");
    Scanner user_input = new Scanner (System.in);
    try{
    a = user_input.nextInt();
    }
    catch(InputMismatchException e){
        System.out.println ("Error: INVALID INPUT, please try again");
        cond = true;
        mis = true;
    }
    if ((a<0 || a != (int)a) && mis == false) {
        System.out.println ("Error: INVALID INPUT, please try again");
       cond = true;
    }
    else if(mis == false){
        cond = false;
    }
    if(cond == false){
        break;
    }
    }
    hour = a/3600;
    min = ((a%3600)/60);
    sec = ((a%3600)%60);

    System.out.println(hour + " hours "+ min+ " minutes " + sec + " seconds");

}
}
于 2013-10-05T19:27:48.027 回答
0

您需要将用户输入作为字符串读取 - 无法解决这个问题,因为您必须能够处理无效输入。因此,您需要:

String input = scanner.next();

至于检查,这取决于您所说的“整数”是什么意思。

如果它意味着“整数”的通常含义,您可以像这样测试:

if (input.matches("\\d+"))
    // it's a (positive) whole number

如果它意味着一个有效的 javaint值,那么由于类型的值范围,这会更复杂。利用:

try {
    int i = Integer.parseInt(input);
    if (I > 0) 
        // input is valid
} catch (NumberFormatException e) {
    // input is not a valid java int
}
于 2013-10-05T20:49:14.170 回答
0

通过使用“分而治之”的方法,您通常可以使事情变得更简单。在这种情况下,使用函数来正确读取用户输入可以大大简化任务:

  static int getSecondsFromUser() {
    final Scanner userInput = new Scanner(System.in);

    while (true) {
      System.out.println("Please enter the number of seconds:");
      try {
        return userInput.nextInt();            
      } catch (InputMismatchException e) {
        userInput.skip(".+");
      }
    }
  }

  public static void main(String[] args) {
    final int a = getSecondsFromUser();

    final int hour = a / 3600;
    final int min = ((a % 3600) / 60);
    final int sec = ((a % 3600) % 60);

    System.out.println(hour + " hours " + min + " minutes " + sec + " seconds");
  }

该表达式userInput.skip(".+");使用.+表示“一切”的正则表达式。

该函数的“契约”是,它将返回一个正确的 int 值,因此您的 main 函数不需要担心这一点,它可以只使用该值并假设它是正确的。如果需要,您还可以添加进一步的检查,例如 >0,为简单起见,我跳过了这些检查。当扫描发生在函数内部时,您可以在获得正确值后立即使用 return 来“跳出”,这解决了很多复杂性。

此外,这使您的代码更易于阅读、修复和重用。

于 2013-10-05T19:26:03.953 回答
0

试试这个。通过调用 nextLine() 而不是 nextInt() 并尝试解析结果,我们消除了任何错误输入并保持程序在任何输入情况下运行。

import java.util.*;
public class time
{
    public static void main (String[] args) 
    {
        int time, sec, min, hour;
        String inputString;
        System.out.println ("Please enter the number of seconds");
        Scanner user_input = new Scanner (System.in);
        time = -1;
        inputString = user_input.nextLine();

        try
        {
            time = Integer.parseInt(inputString);
        }
        catch(NumberFormatException e)
        {
            time = -1;
        }

        while (time < 0)
        {
            System.out.println ("Error: INVALID INPUT, please try again");
            System.out.println("Please enter the number of seconds");
            inputString = user_input.next();

            try
            {
                time = Integer.parseInt(inputString);
            }
            catch(InputMismatchException e)
            {
                time = -1;
            }
       }


      hour = time/3600;
      min = ((time%3600)/60);
      sec = ((time%3600)%60);

      System.out.println(hour + " hours "+ min+ " minutes " + sec + " seconds");

  }
}

编辑: IsEmpty() 无法正常工作,但将其删除并将时间初始化为 -1 即可修复它。

于 2013-10-05T20:00:03.823 回答