gpt4 book ai didi

java - 为什么我的程序会重复出现? Java错误

转载 作者:行者123 更新时间:2023-11-30 08:13:59 26 4
gpt4 key购买 nike

我应该让程序给出宠物的数量以及低于 5 磅、5-10 磅和超过 10 磅的宠物数量的百分比。程序一直重复这些语句,我不知道为什么。在过去的几天里我一直在研究这个问题,但我仍然无法弄清楚。有时似乎我已经修复了它,但后来它又发生了。谁能澄清为什么会发生这种情况?我需要帮助。

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);


}
}
}

用于 SetPetWeight 类的 Pet 类,已正确编译并用于数组。

 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());
}
};
}

最佳答案

keyboard.nextLine(); 移至 weight = Keyboard.nextDouble(); 之后以使用悬空换行符。

关于java - 为什么我的程序会重复出现? Java错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29912480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com