Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.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 回到程序的开头_Java_Loops - Fatal编程技术网

Java 回到程序的开头

Java 回到程序的开头,java,loops,Java,Loops,嗨,我现在是java新手。我有一个工作代码,但如果用户输入的时间错误,我希望它回到开始。我似乎无法解决这个问题,有没有一个简单的方法我可以做到 package Time_calculation; import java.util.*; import java.text.SimpleDateFormat; import java.util.Date; public class Time_calculation { public static v

嗨,我现在是java新手。我有一个工作代码,但如果用户输入的时间错误,我希望它回到开始。我似乎无法解决这个问题,有没有一个简单的方法我可以做到

    package Time_calculation;

    import java.util.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class Time_calculation {

    public static void main(String[] args) {

    SimpleDateFormat timeFormat =  new SimpleDateFormat ("HH:mm:ss");

    Date T1 = null;
    Date T2 = null;


    Scanner RFK = new Scanner(System.in);

    boolean programrunning = true;
    while (programrunning == true){


    System.out.println("Enter start time HH:MM: (If you wish to exit please type 'Exit')");
    String srtTime = RFK.nextLine();
    if (srtTime .matches ("Exit")){
            System.out.println("Exiting...");
            System.exit(0); 
    }else{
    try{
        T1 = timeFormat.parse(srtTime);
        }catch (Exception e){
            System.err.println("Error");}


    System.out.println("Enter end time HH:MM:SS (If you wish to exit please type 'Exit')");
    String endtime = RFK.nextLine();
    if (endtime .matches ("Exit")){
            System.out.println("Exiting...");
            System.exit(0); 
    }else{
    try{   
        T2 =timeFormat.parse(endtime);
        }catch (Exception e){
            System.err.println("Error");}

    long diff = T2.getTime() - T1.getTime();
    long diffsec = diff / 1000;
    System.out.println("Your time in seconds is: " + diffsec + " seconds");

    double ms = 1609.344 / diffsec;
    double mph = ms * 2.23693629;
    System.out.println("Speed in M/S: " + ms);
    System.out.println("Speed in MPH is: " + mph);

    if(mph > 6){
           System.out.println("Running faster than average speed!");}
    else{
           System.out.println("You are running lower than average speed!");}

    System.out.println();
           }
        }
    }
}
}


有人知道怎么做吗,这真的很有帮助

Java和JavaScript之间有区别!如果您事先不知道循环必须发生多少次,请在任何程序中使用
while
do while
循环“返回”。另外,不要使用
while(programrunning==true){
而使用更简单的
while(programrunning){
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class TimeCalculation {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);

        while (true) {
            Date start = TimeCalculation
                    .readInput(
                            "Enter start time HH:MM: (If you wish to exit please type 'Exit')",
                            sc);

            Date end = TimeCalculation
                    .readInput(
                            "Enter End time HH:MM: (If you wish to exit please type 'Exit')",
                            sc);
            // Do whatever you want with start and end here
        }
    }

    /*
     * Take care of reading input if valid input found return it else propmt again for valid input or exit if user wants
     * */

    public static Date readInput(String msg, Scanner sc) {
        Date output = null;
        boolean isValid = false;
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        while (!isValid) {

            System.out.println(msg);
            String time = sc.nextLine();

            if (time.matches("Exit")) {
                System.out.println("Exiting...");
                System.exit(0);
            } else {
                try {
                    output = timeFormat.parse(time);
                    isValid = true;
                } catch (Exception e) {
                    System.out.println("Invalid Input");
                }
            }
        }
        return output;
    }

}