我有点迷失在我错误的地方这里是我的主要文件:
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 + "]";
}
}
但是,最终输出是用户按照输入的顺序输入的所有名称。同样,名字甚至不按字母顺序排列。任何帮助都会非常感激!
答案 0 :(得分:1)
这是一个简化且正确运行的代码版本(如果我很好地理解了该主要方法的目标):
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]);
}
很少考虑:
希望您更好地了解循环,数组,排序以及如何改进代码。
更新:上述代码将在每次输入后逐个读取名称,直到提供空输入或达到MAX_SIZE限制。 如果你想一次性输入所有名字,你甚至不需要循环,你的代码将会进一步简化,但你应该以某种方式分离标记(你的名字),否则它们都将被视为一个大名。 查看下面的代码,它使用String.split()方法分隔标记(名称),并使用“”(空格)作为标记分隔符。
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]);
}
答案 1 :(得分:0)
您可以使用TreeSet.Read java文档中的first()和last()方法执行相同的操作以获取详细信息。 http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
TreeSet以排序顺序存储值,first和last方法分别返回set中的第一个和最后一个值。
现在来看你的代码:
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个具有相同初始值的对象,即当它为第二遍传递时,所有对象都将包含该第二个值,并且初始值将丢失。在这种情况下,您的对象将仅包含最后输入的值。 / p>
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];
然后,您将这些相同的对象分配给同一个数组元素(相同的索引)十次。 在此之后,您将此对象分配给第一个和最后一个变量。因此,在所有此迭代之后,第一个和最后一个将包含用户输入的最后一个元素。 而且compareTo方法返回无处捕获的整数,并且当你使用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方法获取名称。