gpt4 book ai didi

java - 要求 Java 返回到特定语句

转载 作者:行者123 更新时间:2023-12-02 08:37:21 25 4
gpt4 key购买 nike

我目前正在写一个程序,我需要程序返回一个特定的语句。我试过做一个循环,但它非常复杂。有没有更简单的方法?

(如果用户选择是,则重新运行程序。如果否,则结束程序。)

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;
}

}
}
}

最佳答案

试试extract method pattern .这使得这个困惑可读

解压后,只复制粘贴重要的部分到stackoverflow求助,实现回跳会容易很多。

public class CA_1316855_P3 {

public static void main(String[] args) {

int reply;

do {

businessLogic();

reply = askUser();

} while (userWantRepeat(reply));

}
}

关于java - 要求 Java 返回到特定语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20016678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com