java - How do I exit my program without using return and System.exit()? -
so code :-
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 irctc railway reservation system! please proceed further book 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(); (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. select train or enter 1 exit :- "); in[0] = integer.parseint (br.readline()); if (in[0]==1) { system.out.print("thanks visiting our website!"); return; } else { (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 more 5, please try again"); return; } string[] pn = new string[in[2]]; (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]); (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 booking! seats have been confirmed. have day!"); } }
as can see have used return many times here teacher 1 big pain , says can't use return or system.exit(0) :p need terminate program in if statements cat use return or system.exit(0) :p have heard possible while or do-while loop can't understand how...
help appreciated :p
you have used 'if' conditions return error messages , processing positive scenarios if system doesn't satisfy 'if' conditions. replace giving 'if' condition valid scenarios , using 'else' condition after return error messages. way won't have explicitly return.
e.g : last part of ur program written :
if (in[2]<=5) { string[] pn = new string[in[2]]; (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]); (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 booking! seats have been confirmed. have day!"); } else{ system.out.print("uh-oh! no. of passengers more 5, please try again"); }
Comments
Post a Comment