2015-04-28 23 views
1

我應該讓程序給寵物的數量和多少低於5磅,5-10磅和超過10磅的百分比。該計劃不斷重複的聲明,我不知道爲什麼。過去幾天我一直在處理這個問題,但我仍然無法解決這個問題。有時它似乎我修好了,但後來它又發生了。任何人都可以澄清爲什麼發生這種情況?我需要幫助。爲什麼我的程序重複自己。 Java錯誤

import java.util.*; 
import java.util.Collections; 
import java.util.Comparator; 
import java.lang.Object; 

public class SetPetWeight 
{ 
    public static void main(String[] args) { 
    ArrayList<Pet> list = new ArrayList<Pet>(); 
    String name; 
    String answer; 
    int age; 
    Double weight; 
    Scanner keyboard = new Scanner(System.in); 

    do 
{ 

     System.out.println("Enter a String for Pet name: "); 
     name = keyboard.next(); 

     System.out.println("Enter an int for Pet age: "); 
     age = keyboard.nextInt(); 
     if(age <= 0) 
     System.out.println("Error! Enter a real age"); 

     System.out.println("Enter a double for Pet weight: "); 
     weight = keyboard.nextDouble(); 
     if(weight <= 0) 
     System.out.println("Error! Enter a real weight"); 

       do 
     { 
     System.out.println("Do you want to enter another pet? Y/N"); 
     answer = keyboard.nextLine(); 
      keyboard.nextLine(); 
     } while (answer.equalsIgnoreCase("Y")); 


    } while (name.length() < 0 && age < 0 && weight < 0); 



    System.out.println("The weight is now sorted by weight!"); 
    Collections.sort(list, Pet.SortByWeight); 
    for (Pet p2 : list) 
     p2.writeOutput(); 

    int average1 = 0; 
    int average2 = 0; 
    int average3 = 0; 


    for (Pet p : list) 
    { 
     if(p.getWeight() >= 0 && p.getWeight() <= 5) 
     { 
     ++average1; 
     } 
     else if(p.getWeight() >= 5 && p.getWeight() <= 10) 
     { 
     ++average2; 
     } 
     else if(p.getWeight() > 10) 
     { 
     ++average3; 
     } 
     System.out.println("The average of pets under 5 pounds:" + average1); 
     System.out.println("The average of pets between 5 and 10 pounds:" + average2); 
     System.out.println("The average of pets over 10 pounds:" + average3); 


    } 
    } 
} 

Pet類用於SetPetWeight類,編譯正確並用於數組。

import java.util.*; 

    public class Pet { 
     private String name; 
     private Integer age; // in years 
     private double weight; // in pounds 

     public void writeOutput() { 
      System.out.println("Name: " + name); 
      System.out.println("Age: " + age + " years"); 
      System.out.println("Weight: " + weight + " pounds"); 
     } 

     public void set(String newName) { 
      name = newName; 
      // age and weight are unchanged. 
     } 

     public void set(int newAge) { 
      if (newAge <= 0) { 
       System.out.println("Error: illegal age."); 
       System.exit(0); 
      } else 
       age = newAge; 
      // name and weight are unchanged. 
     } 

     public void set(double newWeight) { 
      if (newWeight <= 0) { 
       System.out.println("Error: illegal weight."); 
       System.exit(0); 
      } else 
       weight = newWeight; 
      // name and age are unchanged. 
     } 

     public Pet(String name, int age, double weight) { 
      this.name = name; 
      this.age = age; 
      this.weight = weight; 
     } 

     public String getName() { 
      return name; 
     } 

     public int getAge() { 
      return age; 
     } 

     public double getWeight() { 
      return weight; 
     } 

     public static Comparator<Pet> SortByWeight = new Comparator<Pet>() 
     { 
      public int compare(Pet pet1, Pet pet2) 
      { 
      return (int)(pet1.getWeight() - pet2.getWeight()); 
      } 
     }; 
    } 

回答

1

移動keyboard.nextLine();weight = keyboard.nextDouble();後消耗懸空換行符。

+0

的Y/N是排除一切,但是當我要把Y它重複的「你想進入...」 –

+0

@DavidWilliams的問題 - 我們展示了更新後的代碼 – TheLostMind

0

您可能想要進行一些更改。 1.每次檢索寵物姓名,年齡和體重的用戶輸入。你可能想創建一個pet類型的對象來存儲這個值,然後把它添加到你的ArrayList中。在你問他們是否要添加另一隻寵物之前, 2.刪除外部do while循環,並更改內部循環以包含「爲寵物名稱輸入字符串:」。 3.同時檢查輸入驗證。

do { 
    System.out.println("Enter a String for Pet name: "); 
    name = keyboard.next(); 
    System.out.println("Enter an int for Pet age: "); 
    age = keyboard.nextInt(); 
    if(age <= 0) 
    System.out.println("Error! Enter a real age"); 

    System.out.println("Enter a double for Pet weight: "); 
    weight = keyboard.nextDouble(); 
    keyboard.nextLine(); 
    if(weight <= 0) 
     System.out.println("Error! Enter a real weight"); 

    System.out.println("Do you want to enter another pet? Y/N"); 
    answer = keyboard.nextLine(); 
    keyboard.nextLine(); 
    } while (answer.equalsIgnoreCase("Y")); 
+0

我怎麼包括輸入字符串爲寵物名稱 –

+0

它重複的原因是你有一個循環。檢查縮進是非常重要的。 – Wally

相關問題