我昨天刚刚开始 Java 编程(所以不要期望太多)并且我已经编写了一些代码。我的代码按我希望的方式工作,但显然有问题,我只是想知道改进它的方法。我想我添加了不必要的东西。我正在练习使用课程和其他东西。
testing.java 文件包含:
import java.util.Scanner;
class testing {
private static Scanner input_sn;
private static Scanner input_fn;
private static Scanner input_mem;
public static void main(String[] args){
String First_Name;
String Second_Name;
int members;
int count;
System.out.println("Members: ");
input_mem = new Scanner(System.in);
members = input_mem.nextInt();
funcs funcsObj = new funcs();
for (count = 0; count < members; count++)
{
System.out.println("What is the first name? ");
input_fn = new Scanner(System.in);
First_Name = input_fn.nextLine();
System.out.println("What is the second name? ");
input_sn = new Scanner(System.in);
Second_Name = input_sn.nextLine();
funcsObj.names( funcsObj.setFn(First_Name), funcsObj.setSn(Second_Name));
}
}
}
我的 funcs.java 文件包含:
public class funcs
{
private String firstName;
private String secondName;
private static int members = 0;
public String setFn(String fn)
{
firstName = fn;
return fn;
}
public String setSn(String sn)
{
secondName = sn;
return sn;
}
public void names(String fn, String sn)
{
firstName = fn;
secondName = sn;
members++;
System.out.printf("%d\t%s\t%s\n", members, fn, sn);
}
}
我认为大部分问题都可以在 funcs.java 文件中找到。
谢谢