如何在不使用return和System.exit()的情况下退出程序?

时间:2014-11-11 10:57:25

标签: java project terminate bluej

所以这是我的代码: -

import java.io.*;
class RAILWAYS
{
    BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
    void main () throws IOException
    {
        System.out.print("\f");
        System.out.println("Welcome to IRCTC Railway Reservation System! Please proceed further     to book your train!");
    System.out.println();
    String[] trainname = {"Rajdhani EXP", "AUG Kranti EXP", "Chennai EXP", "Aravali EXP", "Paschim EXP", "Gareeb Rath", "Punjab Mail", "Dehradun EXP", "Swaraj EXP", "Aravali EXP"};
    int[] trainno = {57835, 87612, 15384, 16512, 65265, 51654, 31543, 56416, 85484, 78455};
    String[] origin = {"Mumbai", "Mumbai", "Delhi", "Kolkata", "Mumbai", "Goa", "Durg", "Aligarh", "Jaipur", "Bhuj"};
    String[] destination = {"Surat", "Delhi", "Chennai", "Mumbai", "Ajmer", "Mumbai", "Bikaner", "Agra", "Madurai", "Buxar"};
    int[] fare = {650, 950, 1100, 1200, 1050, 600, 1100, 1250, 1300, 1100};
    int[] lengths = {trainname.length, trainno.length, origin.length, destination.length, fare.length};
    String[] sd = new String[3];
    int in[] = new int[3];
    System.out.println("Train Name\t\tTrain No.\tOrigin\t        Destination\tFare");
    System.out.println();
    for (int i=0;i<lengths[0];i++)
    {
        System.out.println(trainname[i]+"\t\t"+trainno[i]+"\t\t"+origin[i]+"\t\t"+destination[i]+"\t\t"+fare[i]); 
    }
    System.out.println();
    System.out.print("Enter train no. to select train or enter 1 to exit :- ");
    in[0] = Integer.parseInt (br.readLine());
    if (in[0]==1)
    {
        System.out.print("Thanks for visiting our website!");
        return;
    }
    else
    {
         for (int j=0;j<lengths[0];j++)
         {
             if (trainno[j]==in[0])
             {
                 sd[0] = trainname[j];
                 sd[1] = origin[j];
                 sd[2] = destination[j];
                 in[1] = fare[j];
             }
         }
    }
    if (in[1]==0)
    {
        System.out.print("Wrong input! Try again....");
        return;
    }
    System.out.print("Enter number of passengers (max 5) :- ");
    in[2] = Integer.parseInt (br.readLine()); 
    if (in[2]>5)
    {
        System.out.print("Uh-Oh! No. of passengers are more than 5, please try again");
        return;
    }
    String[] pn = new String[in[2]];
    for (int k=0;k<in[2];k++)
    {
        System.out.print("Enter passenger's name :- ");
        pn[k] = br.readLine();
    }
    System.out.println();
    System.out.println("Booking Details :-");
    System.out.println();
    System.out.println("No. of passengers :- "+in[2]);
    for (int z=0;z<in[2];z++)
    {
        System.out.println("Name of passenger travelling :- "+pn[z]);
    }
    System.out.println("Train Name :- "+sd[0]);
    System.out.println("Train Number :- "+in[0]);
    System.out.println("Train Origin :- "+sd[1]);
    System.out.println("Train Destination :- "+sd[2]);
    System.out.println("Train Fare/Person :- "+in[1]);
    System.out.println("Total Fare :- "+in[2]*in[1]);
    System.out.println();
    System.out.println("Thanks for your booking! Your seats have been confirmed. Have a good day!");
}
}

正如你所看到的,我在这里多次使用返回,但我的老师是一个很大的痛苦,并说我不能使用return或System.exit(0):P 我真的需要在我的if语句中终止程序,因为我使用return或System.exit(0):P 我听说这可能与while或do-while循环但我无法理解如何......

总是会感谢帮助:P

3 个答案:

答案 0 :(得分:2)

您已使用所有'if'条件返回错误消息,并且如果系统不满足那些'if'条件,则您正在处理正面情况。您可以通过为有效方案提供“if”条件并在此之后使用“else”条件来返回错误消息来替换它。这样您就不必明确返回。

例如:你们程序的最后一部分可以写成:

if (in[2]<=5)
    {

    String[] pn = new String[in[2]];
    for (int k=0;k<in[2];k++)
    {
        System.out.print("Enter passenger's name :- ");
        pn[k] = br.readLine();
    }
    System.out.println();
    System.out.println("Booking Details :-");
    System.out.println();
    System.out.println("No. of passengers :- "+in[2]);
    for (int z=0;z<in[2];z++)
    {
        System.out.println("Name of passenger travelling :- "+pn[z]);
    }
    System.out.println("Train Name :- "+sd[0]);
    System.out.println("Train Number :- "+in[0]);
    System.out.println("Train Origin :- "+sd[1]);
    System.out.println("Train Destination :- "+sd[2]);
    System.out.println("Train Fare/Person :- "+in[1]);
    System.out.println("Total Fare :- "+in[2]*in[1]);
    System.out.println();
    System.out.println("Thanks for your booking! Your seats have been confirmed. Have a good day!");
} else{
System.out.print("Uh-Oh! No. of passengers are more than 5, please try again");

    }

答案 1 :(得分:1)

你可以用

包装所有内容
  

while(true){ //YOUR CODE HERE}

然后替换return;与

  

中断;

停止循环或

  

继续;

进入下一次迭代并再试一次。

答案 2 :(得分:0)

当你输入错误时,你可以抛出一个异常,然后抓住它,或者让它杀死程序。

但是我不会认为这是你老师试图告诉你的。我认为如果用户输入无效的内容,他/她实际上是想让你让程序(反复)询问有效输入。

事实上,这就是您在错误暗示时输出的消息。

(可以重新构建代码,以便main方法只有一个返回点;即最后。有多种方法,你可以做那个但是,如果你的老师要求你这样做,他们没有帮助,IMO。)