我如何为这个 JAVA 项目构建我的测试器 class?
How can I structure my Tester class for this JAVA project?
这是作业的要点:http://prntscr.com/lwbb1x
所以早些时候我弄清楚了 EmployeeNames 部分是如何分配的,或者至少我认为我做到了。这是 EmployeeNames 代码:
public static String[] convertName(String[] names) {
for (int i=0; i<10; i++) {
names[i] = names[i].substring(names[i].length() - 2, names[i].length());
}
return names;
但我基本上停留在测试程序代码上。我知道我想要什么,但它不起作用。谁能帮我吗?几个小时以来,我一直在挠头。
public static void main(String args[]) {
/*Scanner scan = new Scanner(System.in);
System.out.println("Enter 10 last names.");
String input = scan.nextLine();
*/ (Ignore this, I wanted to try doing inputs, but couldn't even figure out how to work with them properly so I typed up sample last names for this.)
String[] lastName = {"Jones", "Roberts", "Lee", "Chang", "Patel", "Park", "Anderson", "Liu", "Smith", "Lopez"};
System.out.println(convertName(lastName));
}
我喜欢看到对我的代码或伪代码结构的修改,因为它可以帮助我最好地认识到我的错误,但任何帮助都是至关重要的!提前谢谢你。
您犯了一些小错误,这是根据您的要求工作的代码 -
class EmployeeNames {
public static String[] convertName(String[] lastNames) {
String[] formattedNames = new String[lastNames.length];
for (int i = 0; i < lastNames.length; i++) {
formattedNames[i] = lastNames[i].substring(lastNames[i].length() - 1) + "."
+ lastNames[i].substring(lastNames[i].length() - 2, lastNames[i].length() - 1) + "." + lastNames[i];
}
return formattedNames;
}
}
public class EmployeeNamesTester {
public static void main(String[] args) {
String[] lastNames = { "Jones", "Roberts", "Lee", "Chang", "Patel", "Park", "Anderson", "Liu", "Smith",
"Lopez" };
String[] formattedNames = EmployeeNames.convertName(lastNames);
for (String formattedName : formattedNames) {
System.out.println(formattedName);
}
}
}
Output:
s.e.Jones
s.t.Roberts
e.e.Lee
g.n.Chang
l.e.Patel
k.r.Park
n.o.Anderson
u.i.Liu
h.t.Smith
z.e.Lopez
希望对您有所帮助!
你在题目中犯了一些逻辑错误。
public static String[] convertName(String[] names) {
String newNames[]=new String[names.length];
for (int i=0; i<names.length; i++) {
newNames[i] = names[i].substring(names[i].length() - 2, names[i].length());
}
return newNames;
}
在上面的方法中,我只是创建了新数组和 return 具有修改值的新数组。
并且在 Main 方法中使用了以下代码-
public static void main(String[] args) {
String[] lastName = {"Jones", "Roberts", "Lee", "Chang", "Patel", "Park", "Anderson", "Liu", "Smith", "Lopez"};
String [] result= convertName(lastName);
for(int i=0;i<result.length;i++){
String lastNames=result[i];
if(lastNames !=null){
System.out.println(lastNames.toUpperCase().charAt(1)+"."+lastNames.toUpperCase().charAt(0)+". "+lastName[i]);
}
}
}
希望对您有所帮助!!
这是作业的要点:http://prntscr.com/lwbb1x
所以早些时候我弄清楚了 EmployeeNames 部分是如何分配的,或者至少我认为我做到了。这是 EmployeeNames 代码:
public static String[] convertName(String[] names) {
for (int i=0; i<10; i++) {
names[i] = names[i].substring(names[i].length() - 2, names[i].length());
}
return names;
但我基本上停留在测试程序代码上。我知道我想要什么,但它不起作用。谁能帮我吗?几个小时以来,我一直在挠头。
public static void main(String args[]) {
/*Scanner scan = new Scanner(System.in);
System.out.println("Enter 10 last names.");
String input = scan.nextLine();
*/ (Ignore this, I wanted to try doing inputs, but couldn't even figure out how to work with them properly so I typed up sample last names for this.)
String[] lastName = {"Jones", "Roberts", "Lee", "Chang", "Patel", "Park", "Anderson", "Liu", "Smith", "Lopez"};
System.out.println(convertName(lastName));
}
我喜欢看到对我的代码或伪代码结构的修改,因为它可以帮助我最好地认识到我的错误,但任何帮助都是至关重要的!提前谢谢你。
您犯了一些小错误,这是根据您的要求工作的代码 -
class EmployeeNames {
public static String[] convertName(String[] lastNames) {
String[] formattedNames = new String[lastNames.length];
for (int i = 0; i < lastNames.length; i++) {
formattedNames[i] = lastNames[i].substring(lastNames[i].length() - 1) + "."
+ lastNames[i].substring(lastNames[i].length() - 2, lastNames[i].length() - 1) + "." + lastNames[i];
}
return formattedNames;
}
}
public class EmployeeNamesTester {
public static void main(String[] args) {
String[] lastNames = { "Jones", "Roberts", "Lee", "Chang", "Patel", "Park", "Anderson", "Liu", "Smith",
"Lopez" };
String[] formattedNames = EmployeeNames.convertName(lastNames);
for (String formattedName : formattedNames) {
System.out.println(formattedName);
}
}
}
Output:
s.e.Jones
s.t.Roberts
e.e.Lee
g.n.Chang
l.e.Patel
k.r.Park
n.o.Anderson
u.i.Liu
h.t.Smith
z.e.Lopez
希望对您有所帮助!
你在题目中犯了一些逻辑错误。
public static String[] convertName(String[] names) {
String newNames[]=new String[names.length];
for (int i=0; i<names.length; i++) {
newNames[i] = names[i].substring(names[i].length() - 2, names[i].length());
}
return newNames;
}
在上面的方法中,我只是创建了新数组和 return 具有修改值的新数组。
并且在 Main 方法中使用了以下代码-
public static void main(String[] args) {
String[] lastName = {"Jones", "Roberts", "Lee", "Chang", "Patel", "Park", "Anderson", "Liu", "Smith", "Lopez"};
String [] result= convertName(lastName);
for(int i=0;i<result.length;i++){
String lastNames=result[i];
if(lastNames !=null){
System.out.println(lastNames.toUpperCase().charAt(1)+"."+lastNames.toUpperCase().charAt(0)+". "+lastName[i]);
}
}
}
希望对您有所帮助!!