2014-07-22 53 views
-4

我希望它打印:「你的分數是第一」,但它不會。我正在使用toString(),我完全是Java的初學者。如果有人有任何想法,請告訴我。我試圖要求用戶輸入,然後打印出來,但它不打印

我的代碼:

import java.util.*; 
public class Person implements Comparable<Person> 
{ 
    String name; 
    int age; 

    public Person(String name, int i) 
    { 
     this.name = name; 
     this.age = i; 
    } 

    public String getName() 
    { 
     return name; 
    } 

    public int getAge() 
    { 
     return age; 
    } 

    public String toString() 
    { 

     return name + " : " + age; 
    } 

    /* 
    ** Implement the natural order for this class 
    */ 
    public int compareTo(Person p) 
    { 
     return getName().compareTo(p.getName()); 
    } 

    static class AgeComparator implements Comparator<Person> 
    { 
     public int compare(Person p1, Person p2) 
     { 
      int age1 = p1.getAge(); 
      int age2 = p2.getAge(); 

      if (age1 == age2) 
       return 0; 
      else if (age1 > age2) 
       return 1; 
      else 
       return -1; 
     } 
    } 

    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     int pie; 
     Scanner input1 = new Scanner(System.in); 
     String ass; 
     pie = input.nextInt(); 
     System.out.println("Your Score is " + pie); 
     ass = input1.nextLine(); 
     System.out.println("Your name is " + ass); 

     List<Person> people = new ArrayList<Person>(); 
     people.add(new Person("Homer", 1)); 
     people.add(new Person("Marge", 35)); 
     people.add(new Person("Bart", 15)); 
     people.add(new Person(ass, pie)); 

     // Sort by natural order 

     Collections.sort(people); 
     System.out.println("Sort by Natural order"); 
     System.out.println("\t" + people); 

     // Sort by reverse natural order 

     Collections.sort(people, Collections.reverseOrder()); 
     System.out.println("Sort by reverse natural order"); 
     System.out.println("\t" + people); 

     // Use a Comparator to sort by age 

     Collections.sort(people, new Person.AgeComparator()); 
     System.out.println("Sort using Age Comparator"); 
     System.out.println("\t" + people); 

     // Use a Comparator to sort by descending age 

     Collections.sort(people, 
      Collections.reverseOrder(new Person.AgeComparator())); 
     System.out.println("Sort using Reverse Age Comparator"); 
     System.out.println("\t" + people); 
    } 
} 

我想到的是: 它打印「你的分數是:」第一 和我可以進入我想要的信息。

+0

當你印刷人物時你會得到什麼?你到底想做什麼?你能清楚些嗎? –

+2

如果您的程序不符合預期:它是什麼呢? – Seelenvirtuose

+0

您只能使用掃描儀「全部」。 –

回答

0

當你按下後,你進入輸入Score,你應該閱讀'nextLine'來等待下一個輸入。

 Scanner input = new Scanner(System.in); 
     int pie = input.nextInt(); 
     input.nextLine(); 
     String ass = input.nextLine(); 
     input.close(); 
     System.out.println("Your Score is " + pie); 
     System.out.println("Your name is " + ass); 

注意:最好在讀取流之後關閉掃描儀。

-1
I want it to print: "Your score is first", but it won't. 

打印消息之前它不打印的第一,因爲你都在問scanner輸入

解決方案:

 System.out.println("Your Score is " + pie); 
     pie = input.nextInt(); 
     System.out.println("Your name is " + ass); 
     ass = input1.nextLine(); 
+0

現在您先打印,然後詢問一個值!?這沒有意義。 – Seelenvirtuose

+0

@Seelenvirtuose他實際上說的是''你的分數是'第一' –

+0

可能是。但是,首先打印句子然後要求輸入的意圖是什麼?我不認爲,他想這樣。這樣理解他的問題更有意義:「我嘗試詢問用戶輸入並打印出來,但不打印」。但由於這只是推測性的,我們應該要求OP更新他的問題或投票結束。我不認爲這個答案會有幫助。 – Seelenvirtuose