我正在开发一个小型Java程序,该程序会向在我学校注册AP考试的电子邮件学生输出收据。代码看起来像这样。
// Create email text body for student who registered for an AP exam.
import java.util.Scanner;
class EmailText {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
String first_name;
String email;
int numTests;
char ch;
char choice;
int cost;
System.out.print("Enter student first name: ");
first_name = input.next();
System.out.print("Enter student email: ");
email = input.next();
System.out.print("Enter number of tests ordered (1-9): ");
numTests = input.nextInt();
if(numTests < 10) {
System.out.print("Did student qualify for fee waiver (y/n)? ");
ch = input.next().charAt(0);
if(ch == 'y') {
cost = 5;
int total = numTests * cost;
System.out.println("** COPY/PASTE THIS DRAFT **");
System.out.println("To: " + email);
System.out.println("Subject: 2014 AP Test Receipt for " + first_name);
System.out.println();
System.out.println("Hi " + first_name + ",\n");
System.out.println("Thank you for registering for the 2014 AP Exams!");
System.out.println("According to our records, you ordered " + numTests + " tests.\n");
System.out.println("Because you stated that you qualified for a fee waiver, " +
"each test will cost you $" + cost + ".");
System.out.println("Your total cost is $" + cost + " * " + numTests +
" = $" + total + ".\n");
System.out.println("Please submit your payment to the College Counseling Office ASAP.\nThank you.\n");
}
else if(ch == 'n') {
cost = 89;
int total = numTests * cost;
System.out.println("** Copy/Paste this Draft **");
System.out.println("To: " + email);
System.out.println("Subject: 2014 AP Test Receipt for " + first_name);
System.out.println();
System.out.println("Hi " + first_name + ",\n");
System.out.println("Thank you for registering for the 2014 AP Exams!");
System.out.println("According to our records, you ordered " + numTests + " tests.");
System.out.println("Because you stated that you qualified for a fee waiver, " +
"each test will cost you $" + cost + ".");
System.out.println("Your total cost is $" + cost + " * " + numTests +
" = $" + total + ".\n");
System.out.println("Please submit your payment to the College Counseling Office ASAP.\nThank you.\n");
}
}
else {
System.out.println("Please start again.");
return;
}
}
}
我遇到的问题是我在else和if块中重复相同的System.out.println()体。相反,我想要做的是创建一个可以在每个块中调用的方法。
如果可能,我该如何做到这一点?
答案 0 :(得分:3)
如果这是你的意思,那么你需要阅读基本的Java,我已经在代码示例中添加了你的方法,请阅读此链接以了解有关方法的更多信息:http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
请参阅Peter的回答以获取更多信息!
// Create email text body for student who registered for an AP exam.
import java.util.Scanner;
class EmailText {
public static void main(String args[]) {
int numTests, cost;
String email, first_name;
char ch;
Scanner input = new Scanner(System.in);
System.out.print("Enter student first name: ");
first_name = input.next();
System.out.print("Enter student email: ");
email = input.next();
System.out.print("Enter number of tests ordered (1-9): ");
numTests = input.nextInt();
if(numTests < 10) {
System.out.print("Did student qualify for fee waiver (y/n)? ");
ch = input.next().charAt(0);
if(ch == 'y') {
cost = 5;
PrintStuff(numTests, cost, email, first_name, "qualified for a fee waiver, ");
}
else if(ch == 'n') {
cost = 89;
PrintStuff(numTests, cost, email, first_name, "did not qualify for a fee waiver, ");
} else {
System.out.println("Please start again.");
}
}
}
public static void PrintStuff(int numTests, int cost, String email, String first_name, String fw_status) {
int total = numTests * cost;
System.out.println("** COPY/PASTE THIS DRAFT **");
System.out.println("To: " + email);
System.out.println("Subject: 2014 AP Test Receipt for " + first_name);
System.out.println();
System.out.println("Hi " + first_name + ",\n");
System.out.println("Thank you for registering for the 2014 AP Exams!");
System.out.println("According to our records, you ordered " + numTests + " tests.\n");
System.out.println("Because you stated that you " + fw_status +
"each test will cost you $" + cost + ".");
System.out.println("Your total cost is $" + cost + " * " + numTests +
" = $" + total + ".\n");
System.out.println("Please submit your payment to the Student Store ASAP.\nThank you.\n");
}
}
答案 1 :(得分:2)
不需要一个方法,只需要一些DRY重构:
if(ch == 'y' || ch == 'n') {
cost = ch == 'y' ? 5 : 89;
int total = numTests * cost;
System.out.println("** COPY/PASTE THIS DRAFT **");
System.out.println("To: " + email);
System.out.println("Subject: 2014 AP Test Receipt for " + first_name);
System.out.println();
System.out.println("Hi " + first_name + ",\n");
System.out.println("Thank you for registering for the 2014 AP Exams!");
System.out.println("According to our records, you ordered " + numTests + " tests.\n");
System.out.println("Because you stated that you qualified for a fee waiver, " +
"each test will cost you $" + cost + ".");
System.out.println("Your total cost is $" + cost + " * " + numTests +
" = $" + total + ".\n");
System.out.println("Please submit your payment to the College Counseling Office ASAP.\nThank you.\n");
} else {
System.out.println("Please start again.");
return;
}
答案 2 :(得分:0)
创建一个类似这样的方法:
private String createOutput(int cost, String email, String first_name...)
{
StringBuffer outputBuffer = new StringBuffer();
outputBuffer.append("** Copy/Paste this Draft **\n");
outputBuffer.append("To: " + email + "\n");
outputBuffer.append("Subject: 2014 AP Test Receipt for " + first_name + "\n");
outputBuffer.append("\n");
...
return outputBuffer.toString();
}
然后您的IF语句将如下所示:
if(ch == 'y') {
cost = 5;
int total = numTests * cost;
System.out.println(createOutput(cost, email, first_name, ...);
}
else if (ch == 'n') {
cost = 89;
int total = numTests * cost;
System.out.println(createOutput(cost, email, first_name, ...);
}
答案 3 :(得分:0)
您可以通过向此方法添加参数来实现此目的。一切都
不能在一个if/else if
块之间打印完全相同的
和另一个else if
块,您可以创建该方法的参数。然后从
你调用相同方法的不同块,但你传递不同的值
参数。
答案 4 :(得分:0)
您可以在main之外创建另一个方法来打印重复的System.out.println 这个方法可能包含这样的参数
public void print(total){
System.out.println("** Copy/Paste this Draft **");
System.out.println("To: " + email);
System.out.println("Subject: 2014 AP Test Receipt for " + first_name);
System.out.println();
System.out.println("Hi " + first_name + ",\n");
System.out.println("Thank you for registering for the 2014 AP Exams!");
System.out.println("According to our records, you ordered " + numTests + " tests.");
System.out.println("Because you stated that you qualified for a fee waiver, " +
"each test will cost you $" + cost + ".");
System.out.println("Your total cost is $" + cost + " * " + numTests +
" = $" + total + ".\n");
System.out.println("Please submit your payment to the College Counseling Office ASAP.\nThank you.\n");
}
在您的主要内容中,您可以访问此方法或将其置于
之类的条件中 if(ch == 'y') {
cost = 5;
int total = numTests * cost;
print(total);
}
答案 5 :(得分:-2)
import static java.lang.System.out;
允许您简单地引用该代码,虽然通常被认为是一种不好的做法,仅适用于一次性程序。
只需保存对System.out的引用即可为您保存该部分
PrintStream out = System.out;
out.println( "hello" );
或者给自己写一个很好的速记方法
public static void print(String s){
System.out.println(s);
}
你必须为int,double等编写重载,否则你可以使用字符串连接
int x = 10;
print(x + "");
或者你的意思仅仅是那里的大块代码?在那种情况下......
public void printBlock(String email, String first_name, int numTests, int cost, int total){
System.out.println("** COPY/PASTE THIS DRAFT **");
System.out.println("To: " + email);
System.out.println("Subject: 2014 AP Test Receipt for " + first_name);
System.out.println();
System.out.println("Hi " + first_name + ",\n");
System.out.println("Thank you for registering for the 2014 AP Exams!");
System.out.println("According to our records, you ordered " + numTests + " tests.\n");
System.out.println("Because you stated that you qualified for a fee waiver, " +
"each test will cost you $" + cost + ".");
System.out.println("Your total cost is $" + cost + " * " + numTests +
" = $" + total + ".\n");
System.out.println("Please submit your payment to the College Counseling Office ASAP.\nThank you.\n");
}
现在只需致电
if(thing){
printBlock("email","firstname",1,10,10);
}
else{
printBlock("email","othername",2,20,40);
}
或者其他什么。