gpt4 book ai didi

java - 创建和调用涉及多个 System.out.println() 和变量的方法

转载 作者:行者123 更新时间:2023-11-29 05:30:55 25 4
gpt4 key购买 nike

我正在开发一个 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 block 中重复相同的 System.out.println() 主体。相反,我想做的是创建一个可以在每个 block 中调用的方法。

如果可能的话,我怎样才能做到这一点?

最佳答案

如果这是您的意思,那么您需要阅读基础 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");
}
}

关于java - 创建和调用涉及多个 System.out.println() 和变量的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21125765/

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