Java:当用户给出的名字按字母顺序排列时,只打印名字和姓氏
Java: Printing only the First and Last when the names that the user gave are organized alphabetically
我有点迷失在我哪里出错了这里是我的主文件:
import java.util.Scanner;
import java.util.Arrays;
import java.util.*;
import java.util.TreeSet;
public class personSorter
{
public static void main(String[] args)
{
int count = 0;
Scanner in = new Scanner(System.in);
boolean more = true;
Person first = null;
Person last = null;
Person[] people= new Person[10]; //my array
while(more)
{
System.out.println("Please enter the person's name or a blank line to quit");
String names = in.nextLine();
if (names.equals(""))
{
more = false;
}
else
{
Person p1 = new Person(names); //creating 10 person objects to be used
Person p2 = new Person(names);
Person p3 = new Person(names);
Person p4 = new Person(names);
Person p5 = new Person(names);
Person p6 = new Person(names);
Person p7 = new Person(names);
Person p8 = new Person(names);
Person p9 = new Person(names);
Person p10 = new Person(names);
people[count] = p1; // using my person objects and declaring the index of variable count
people[count] = p2;
people[count] = p3;
people[count] = p4;
people[count] = p5;
people[count] = p6;
people[count] = p7;
people[count] = p8;
people[count] = p9;
people[count] = p10;
first = people[count];
last = people[count];
TreeSet<String> treeSet = new TreeSet<String>(); //using TreeSort to get the names entered by user in ascending order
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
first.compareTo(p1); //after TreeSort first compare method called of first object as it is now the first name in alphabet order
last.compareTo(p10); //after TreeSort last compare method called of last object as it is now the last name in alphabet order
count++;
}
}
//printing out the first and last name(in alphabet order) of the entered names
System.out.println("First: " + first.toString());
System.out.println("Last: " + last.toString());
}
}
我使用 TreeSet 按字母顺序排列名称。然后我在对象 1 和 10 上使用了对 compareTo 方法的调用,只是因为当它们按字母顺序排列时,第一个对象成为名字,最后一个对象成为姓氏。
这里是 Person.java:
public class Person implements Comparable <Person>
{
private String name;
public Person(String n)
{
name = n;
}
public String getName()
{
return name;
}
@Override
public int compareTo(Person others)
{
if (name.compareTo(others.name) == 1)
{
return 0;
}
else if (name.compareTo(others.name) < 0)
{
return -1;
}
else
{
return 1;
}
}
public String toString()
{
return "[" + name + "]";
}
}
但是,最终输出的是用户输入的所有名称,按照输入的顺序排列。就像,名字甚至不按字母顺序排列。任何帮助将不胜感激!
这是您的代码的简化且正确工作的版本(如果我很好地理解该主要方法的目标):
public static void main(String[] args) {
int count = 0;
Scanner in = new Scanner(System.in);
boolean more = true;
int MAX_SIZE = 10;
Person[] people = new Person[MAX_SIZE]; // my array
while (more && count < MAX_SIZE) {
System.out.println("Please enter the person's name or a blank line to quit");
String name = in.nextLine();
if (name.equals("")) {
more = false;
} else {
Person p = new Person(name);
people[count++] = p;
}
}
in.close();
Arrays.sort(people, 0, count);
// printing out the first and last name(in alphabet order) of the entered names
System.out.println("First: " + people[0]);
System.out.println("Last: " + people[count - 1]);
}
几个注意事项:
- 您不需要 TreeSort 来对数组进行排序,只需使用 Arrays.sort 方法即可。还要注意它作为输入将其限制为开始和结束的更多参数,这些参数不一定与数组的完整初始大小匹配(因为您是从控制台输入填充它的)
- 要填充数组的不同元素,您需要移动到它的下一个索引,因此计数变量需要在每次新迭代时递增 1
- 数组排序后,第一个和最后一个元素不需要临时变量,只需使用第一个索引 (0) 和最后一个填充的索引 (count - 1)
- 您还需要将整个循环限制为数组的最大大小,以免超出其大小并引发异常
希望您能更好地理解循环、数组、排序以及如何改进您的代码。
Update:上面的代码会在每次输入后一个一个地读取名称,直到提供一个空输入或达到MAX_SIZE限制。
如果你想一次输入所有名字,你甚至不需要循环,你的代码将进一步简化,但是你应该以某种方式将标记(你的名字)分开,否则它们将被视为一个大名字.
查看下面的代码,它使用 String.split() 方法并使用“”(space)作为标记分隔符来分隔标记(名称)。
public static void main(String[] args) {
int count = 0;
Scanner in = new Scanner(System.in);
int MAX_SIZE = 10;
Person[] people = new Person[MAX_SIZE];
System.out.println("Please enter the person's name or a blank line to quit");
String line = in.nextLine();
String[] names = line.split(" ");
for (int i = 0; i < MAX_SIZE; i++) {
Person p = new Person(names[i]);
people[count++] = p;
}
in.close();
Arrays.sort(people, 0, count);
// printing out the first and last name(in alphabet order) of the entered names
System.out.println("First: " + people[0]);
System.out.println("Last: " + people[count - 1]);
}
您可以对 TreeSet.Read java 文档中的 first() 和 last() 方法执行相同的操作以获取详细信息。 http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
TreeSet 按排序顺序存储值,first 和 last 方法 return 分别存储集合中的第一个和最后一个值。
现在进入您的代码:
else
{
Person p1 = new Person(names); //creating 10 person objects to be used
Person p2 = new Person(names);
Person p3 = new Person(names);
Person p4 = new Person(names);
Person p5 = new Person(names);
Person p6 = new Person(names);
Person p7 = new Person(names);
Person p8 = new Person(names);
Person p9 = new Person(names);
Person p10 = new Person(names);
您正在创建 10 个具有相同初始化值的对象,即当它循环第二次时,所有对象都将包含第二个值,并且初始值将丢失。在这种情况下,您的对象将仅包含最后输入的值.
people[count] = p1; // using my person objects and declaring the index of variable count
people[count] = p2;
people[count] = p3;
people[count] = p4;
people[count] = p5;
people[count] = p6;
people[count] = p7;
people[count] = p8;
people[count] = p9;
people[count] = p10;
first = people[count];
last = people[count];
然后您将这些相同的对象分配给相同的数组元素(相同的索引)十次。
在此之后,您将此对象分配给 first 和 last variable.So,因为所有此迭代 first 和 last 将包含用户输入的最后一个元素。
并且 compareTo 方法 returns 整数在您使用 treeset 时(在本例中)是无处捕获和比较的。
first.compareTo(p1); //after TreeSort first compare method called of first object as it is now the first name in alphabet order
last.compareTo(p10); //after TreeSort last compare method called of last object as it is now the last name in alphabet order
总而言之,要么您正在尝试做其他事情,要么您使用了错误的逻辑。
在树集中插入名称并使用 first 和 last 方法获取名称。
我有点迷失在我哪里出错了这里是我的主文件:
import java.util.Scanner;
import java.util.Arrays;
import java.util.*;
import java.util.TreeSet;
public class personSorter
{
public static void main(String[] args)
{
int count = 0;
Scanner in = new Scanner(System.in);
boolean more = true;
Person first = null;
Person last = null;
Person[] people= new Person[10]; //my array
while(more)
{
System.out.println("Please enter the person's name or a blank line to quit");
String names = in.nextLine();
if (names.equals(""))
{
more = false;
}
else
{
Person p1 = new Person(names); //creating 10 person objects to be used
Person p2 = new Person(names);
Person p3 = new Person(names);
Person p4 = new Person(names);
Person p5 = new Person(names);
Person p6 = new Person(names);
Person p7 = new Person(names);
Person p8 = new Person(names);
Person p9 = new Person(names);
Person p10 = new Person(names);
people[count] = p1; // using my person objects and declaring the index of variable count
people[count] = p2;
people[count] = p3;
people[count] = p4;
people[count] = p5;
people[count] = p6;
people[count] = p7;
people[count] = p8;
people[count] = p9;
people[count] = p10;
first = people[count];
last = people[count];
TreeSet<String> treeSet = new TreeSet<String>(); //using TreeSort to get the names entered by user in ascending order
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
treeSet.add(names);
first.compareTo(p1); //after TreeSort first compare method called of first object as it is now the first name in alphabet order
last.compareTo(p10); //after TreeSort last compare method called of last object as it is now the last name in alphabet order
count++;
}
}
//printing out the first and last name(in alphabet order) of the entered names
System.out.println("First: " + first.toString());
System.out.println("Last: " + last.toString());
}
}
我使用 TreeSet 按字母顺序排列名称。然后我在对象 1 和 10 上使用了对 compareTo 方法的调用,只是因为当它们按字母顺序排列时,第一个对象成为名字,最后一个对象成为姓氏。
这里是 Person.java:
public class Person implements Comparable <Person>
{
private String name;
public Person(String n)
{
name = n;
}
public String getName()
{
return name;
}
@Override
public int compareTo(Person others)
{
if (name.compareTo(others.name) == 1)
{
return 0;
}
else if (name.compareTo(others.name) < 0)
{
return -1;
}
else
{
return 1;
}
}
public String toString()
{
return "[" + name + "]";
}
}
但是,最终输出的是用户输入的所有名称,按照输入的顺序排列。就像,名字甚至不按字母顺序排列。任何帮助将不胜感激!
这是您的代码的简化且正确工作的版本(如果我很好地理解该主要方法的目标):
public static void main(String[] args) {
int count = 0;
Scanner in = new Scanner(System.in);
boolean more = true;
int MAX_SIZE = 10;
Person[] people = new Person[MAX_SIZE]; // my array
while (more && count < MAX_SIZE) {
System.out.println("Please enter the person's name or a blank line to quit");
String name = in.nextLine();
if (name.equals("")) {
more = false;
} else {
Person p = new Person(name);
people[count++] = p;
}
}
in.close();
Arrays.sort(people, 0, count);
// printing out the first and last name(in alphabet order) of the entered names
System.out.println("First: " + people[0]);
System.out.println("Last: " + people[count - 1]);
}
几个注意事项:
- 您不需要 TreeSort 来对数组进行排序,只需使用 Arrays.sort 方法即可。还要注意它作为输入将其限制为开始和结束的更多参数,这些参数不一定与数组的完整初始大小匹配(因为您是从控制台输入填充它的)
- 要填充数组的不同元素,您需要移动到它的下一个索引,因此计数变量需要在每次新迭代时递增 1
- 数组排序后,第一个和最后一个元素不需要临时变量,只需使用第一个索引 (0) 和最后一个填充的索引 (count - 1)
- 您还需要将整个循环限制为数组的最大大小,以免超出其大小并引发异常
希望您能更好地理解循环、数组、排序以及如何改进您的代码。
Update:上面的代码会在每次输入后一个一个地读取名称,直到提供一个空输入或达到MAX_SIZE限制。 如果你想一次输入所有名字,你甚至不需要循环,你的代码将进一步简化,但是你应该以某种方式将标记(你的名字)分开,否则它们将被视为一个大名字. 查看下面的代码,它使用 String.split() 方法并使用“”(space)作为标记分隔符来分隔标记(名称)。
public static void main(String[] args) {
int count = 0;
Scanner in = new Scanner(System.in);
int MAX_SIZE = 10;
Person[] people = new Person[MAX_SIZE];
System.out.println("Please enter the person's name or a blank line to quit");
String line = in.nextLine();
String[] names = line.split(" ");
for (int i = 0; i < MAX_SIZE; i++) {
Person p = new Person(names[i]);
people[count++] = p;
}
in.close();
Arrays.sort(people, 0, count);
// printing out the first and last name(in alphabet order) of the entered names
System.out.println("First: " + people[0]);
System.out.println("Last: " + people[count - 1]);
}
您可以对 TreeSet.Read java 文档中的 first() 和 last() 方法执行相同的操作以获取详细信息。 http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
TreeSet 按排序顺序存储值,first 和 last 方法 return 分别存储集合中的第一个和最后一个值。
现在进入您的代码:
else
{
Person p1 = new Person(names); //creating 10 person objects to be used
Person p2 = new Person(names);
Person p3 = new Person(names);
Person p4 = new Person(names);
Person p5 = new Person(names);
Person p6 = new Person(names);
Person p7 = new Person(names);
Person p8 = new Person(names);
Person p9 = new Person(names);
Person p10 = new Person(names);
您正在创建 10 个具有相同初始化值的对象,即当它循环第二次时,所有对象都将包含第二个值,并且初始值将丢失。在这种情况下,您的对象将仅包含最后输入的值.
people[count] = p1; // using my person objects and declaring the index of variable count
people[count] = p2;
people[count] = p3;
people[count] = p4;
people[count] = p5;
people[count] = p6;
people[count] = p7;
people[count] = p8;
people[count] = p9;
people[count] = p10;
first = people[count];
last = people[count];
然后您将这些相同的对象分配给相同的数组元素(相同的索引)十次。 在此之后,您将此对象分配给 first 和 last variable.So,因为所有此迭代 first 和 last 将包含用户输入的最后一个元素。 并且 compareTo 方法 returns 整数在您使用 treeset 时(在本例中)是无处捕获和比较的。
first.compareTo(p1); //after TreeSort first compare method called of first object as it is now the first name in alphabet order
last.compareTo(p10); //after TreeSort last compare method called of last object as it is now the last name in alphabet order
总而言之,要么您正在尝试做其他事情,要么您使用了错误的逻辑。 在树集中插入名称并使用 first 和 last 方法获取名称。