Просить Java вернуться к конкретному утверждению
В настоящее время я пишу программу, и мне нужна программа, чтобы вернуться к конкретному утверждению. Я пытался сделать цикл, но это было очень сложно. Есть ли более простой способ?
(Если пользователь выбрал "Да", перезапустите программу. Если "Нет", завершите программу.)
public class CA_1316855_P3 {
public static void main(String[] args) {
//Variables
char Destination, JobType, Currency;
double ExchangeRate = 0, Maximum = 0, Ammount = 0, NewExchangeRate, NewCharge;
int Charge = 0;
String ID, JobTitle = null, Name, Nationality, Mobile, CurrencyTitle = null, DestinationTitle = null;
int BLANK = 0;
int Count_u = 0;
int Count_y = 0;
int Count_p = 0;
int Count_i = 0;
//Destination
while (BLANK == 0) {
System.out.println("\t\t\t ---------------------------------------------");
System.out.println("\t\t\t\t\t Welcome to Transferring System");
System.out.println("\t\t\t ---------------------------------------------");
System.out.println(" Press u for Transferring to USA \n Press y for transferring to Yemen \n Press p for Transferring to Pakistan");
System.out.println(" Press i for Transferring to India \n Press n for new destination country");
System.out.print("Enter the Choice: ");
Scanner input1 = new Scanner(System.in);
Destination = input1.next().charAt(0);
while (Destination != '*')
if (Destination == 'u' ||
Destination == 'U' ||
Destination == 'y' ||
Destination == 'Y' ||
Destination == 'p' ||
Destination == 'P' ||
Destination == 'i' ||
Destination == 'I' ||
Destination == 'n' ||
Destination == 'N') {
if (Destination == 'u' || Destination == 'U') {
ExchangeRate = 0.267;
Count_u++;
DestinationTitle = "United States";
} else if (Destination == 'y' || Destination == 'Y') {
DestinationTitle = "Yemen";
Count_y++;
ExchangeRate = 57.300;
} else if (Destination == 'p' || Destination == 'P') {
DestinationTitle = "Pakistan";
Count_p++;
ExchangeRate = 28.367;
} else if (Destination == 'i' || Destination == 'I') {
DestinationTitle = "India";
Count_i++;
ExchangeRate = 17.417;
} else if (Destination == 'n' || Destination == 'N') {
System.out.print("Enter the name of destination country: ");
Scanner input3 = new Scanner(System.in);
String NewDestinationName = input3.next();
System.out.print("Enter the Exchange Rate between SAR and " + NewDestinationName + " Currency: ");
Scanner input4 = new Scanner(System.in);
NewExchangeRate = input4.nextDouble();
}
break;
} else {
System.out.print("***Wrong Selection !!*** \nEnter choice again:");
Destination = input1.next().charAt(0);
}
//Job Type
System.out.println("\t\t------------\n\t\t Job Type \n\t\t------------ ");
System.out.println(" Press 0 for ordinary employee \n Press 1 for academician employee \n Press 2 for medical employee");
System.out.print("Enter the Choice: ");
Scanner input2 = new Scanner(System.in);
JobType = input2.next().charAt(0);
while (JobType != '*') {
if (JobType == '0' || JobType == '1' || JobType == '2') {
if (JobType == '0') {
JobTitle = "Ordinary";
Maximum = 15000;
} else if (JobType == '1') {
JobTitle = "Academician";
Maximum = 50000;
} else if (JobType == '2') {
JobTitle = "Medical";
Maximum = 80000;
}
break;
} else {
System.out.println("***Wrong Selection !!*** \nEnter choice again:");
JobType = input2.next().charAt(0);
}
}
//Currency
System.out.println("\t\t------------\n\tType of Currency to be received \n\t\t------------ ");
System.out.println(" Press S for Saudi Riyal \n Press L for local Currency");
System.out.print("Enter The Choice: ");
Scanner input5 = new Scanner(System.in);
Currency = input5.next().charAt(0);
while (Currency != '*') {
if (Currency == 's' || Currency == 'l' || Currency == 'S' || Currency == 'L') {
break;
} else {
System.out.println("***Wrong Selection !!*** \nEnter choice again:");
Currency = input5.next().charAt(0);
}
}
System.out.print("Enter the amount to be transferred: ");
Scanner input6 = new Scanner(System.in);
Ammount = input6.nextDouble();
if (Ammount > Maximum) {
System.exit(0);
} else if (Ammount <= 10000 && (Destination == 'u' || Destination == 'U') && Currency == 's') {
Charge = 40;
ExchangeRate = 1;
CurrencyTitle = "SAR";
} else if (Ammount < 10000 && (Destination == 'u' || Destination == 'U') && Currency == 'l') {
Charge = 25;
CurrencyTitle = "USD";
} else if (Ammount > 10000 && Ammount < 80000 && (Destination == 'u' || Destination == 'U') && Currency == 's') {
Charge = 100;
CurrencyTitle = "SAR";
ExchangeRate = 1;
} else if (Ammount > 10000 && Ammount < 80000 && (Destination == 'u' || Destination == 'U') && Currency == 'l') {
Charge = 70;
CurrencyTitle = "USD";
} else if (Ammount < 10000 && (Destination == 'y' || Destination == 'Y') && Currency == 's') {
Charge = 20;
CurrencyTitle = "SAR";
ExchangeRate = 1;
} else if (Ammount < 10000 && (Destination == 'y' || Destination == 'Y') && Currency == 'l') {
Charge = 10;
CurrencyTitle = "YER";
} else if (Ammount > 10000 && Ammount < 80000 && (Destination == 'y' || Destination == 'Y') && Currency == 's') {
Charge = 50;
CurrencyTitle = "SAR";
ExchangeRate = 1;
} else if (Ammount > 10000 && Ammount < 80000 && (Destination == 'y' || Destination == 'Y') && Currency == 'l') {
Charge = 30;
CurrencyTitle = "YER";
} else if (Ammount < 10000 && (Destination == 'p' || Destination == 'P') && Currency == 's') {
Charge = 30;
CurrencyTitle = "SAR";
ExchangeRate = 1;
} else if (Ammount < 10000 && (Destination == 'p' || Destination == 'P') && Currency == 'l') {
Charge = 20;
CurrencyTitle = "PKR";
} else if (Ammount > 10000 && Ammount < 80000 && (Destination == 'p' || Destination == 'P') && Currency == 's') {
Charge = 80;
CurrencyTitle = "SAR";
ExchangeRate = 1;
} else if (Ammount > 10000 && Ammount < 80000 && (Destination == 'p' || Destination == 'P') && Currency == 'l') {
Charge = 55;
CurrencyTitle = "PKR";
} else if (Ammount < 10000 && (Destination == 'i' || Destination == 'I') && Currency == 's') {
Charge = 25;
CurrencyTitle = "SAR";
ExchangeRate = 1;
} else if (Ammount < 10000 && (Destination == 'i' || Destination == 'I') && Currency == 'l') {
Charge = 15;
CurrencyTitle = "INR";
} else if (Ammount > 10000 && Ammount < 80000 && (Destination == 'i' || Destination == 'I') && Currency == 's') {
Charge = 70;
CurrencyTitle = "SAR";
ExchangeRate = 1;
} else if (Ammount > 10000 && Ammount < 80000 && (Destination == 'i' || Destination == 'I') && Currency == 'l') {
Charge = 50;
CurrencyTitle = "INR";
} else if (Destination == 'n' || Destination == 'N') {
System.out.print("Enter the charges: ");
Scanner input9 = new Scanner(System.in);
NewCharge = input9.nextDouble();
System.out.print("Enter the currency: ");
Scanner input12 = new Scanner(System.in);
CurrencyTitle = input12.next();
}
System.out.print("Enter the ID Number: ");
Scanner input7 = new Scanner(System.in);
ID = input7.next();
System.out.print("Enter the Name: ");
Scanner input8 = new Scanner(System.in);
Name = input8.next();
System.out.print("Enter the Nationality: ");
Scanner input10 = new Scanner(System.in);
Nationality = input10.next();
System.out.print("Enter the Mobile Number: ");
Scanner input11 = new Scanner(System.in);
Mobile = input11.next();
//Printing the Bill
System.out.println("\n\n\n\n\t\t\t\t------------------------------------------");
System.out.println("\t\t\t\t\t The current Transaction");
System.out.println("\t\t\t\t------------------------------------------");
System.out.println("Sender: " + Name + "\t\t\t Destination: " + DestinationTitle);
System.out.println("Job Type: " + JobTitle + "\t\t\t Nationality: " + Nationality + "\t\t Mobile: " + Mobile);
System.out.println("\t\t\t The Original Ammount: " + Ammount);
System.out.println("\t\t\t The Recieved Ammount: " + Ammount * ExchangeRate);
System.out.println("\t\t\t The Total Amount Cost: " + ((int) (Ammount) + (Charge)));
int reply = JOptionPane.showConfirmDialog(null, "Would you like to do another transaction ?", "Question.", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
continue;
} else if (reply == JOptionPane.NO_OPTION && Count_u >= 1 && Count_y >= 1 && Count_p >= 1 && Count_i >= 1) {
System.exit(0);
} else {
JOptionPane.showMessageDialog(null, "You need to perform at least one transaction for each country.");
continue;
}
}
}
}
3 ответа
Здесь нет goto
или же jump
в Java, если вы хотите сделать это, вы должны написать while(true) { ... }
или же do { ... } while(false);
и осуществить прыжки по continue;
а также break;
Попробуйте с методом извлечения шаблона. Это делает этот беспорядок читабельным.
После извлечения вы можете скопировать и вставить только важную часть в stackru, чтобы получить помощь, и выполнить возврат назад будет намного проще.
public class CA_1316855_P3 {
public static void main(String[] args) {
int reply;
do {
businessLogic();
reply = askUser();
} while (userWantRepeat(reply));
}
}
Одним из возможных решений вашей проблемы будет использование switch
заявление внутри while
цикл работает бесконечно (перерыв, когда вы закончите).