我需要编写一个程序来拾取多个女演员(1、2等)及其身高,以便最终获得最高和最矮的女演员及其身高的序列号。 我不太确定该怎么做,这是我写的,但是没有用:
import java.util.* ;
public class While
{
public static void main ( String[ ] args )
{
Scanner io = new Scanner (System.in) ;
float min= Float.MAX_VALUE;
float max= Float.MIN_VALUE;
float max2= Float.MIN_VALUE;
float min2= Float.MAX_VALUE;
System.out.print ( "Enter the actress serial number:" ) ;
int num= io.nextInt ( );
System.out.print ( "Enter the actress height:" ) ;
float height = io.nextFloat ( ) ;
while ( height != -1 )
{
System.out.println ( "Enter the next actress serial number:");
System.out.println ( "Enter the next actress height, When you're finished, press -1:");
if (height>max)
max= height;
if (height<min)
min= height;
if (num >max2)
max2= num;
if (num< min2)
min= num;
num= io.nextInt ( );
height = io.nextFloat ( ) ;
}
System.out.println ( ) ;
System.out.println ("The selected actress are:" +max2 +" ," +min2);
System.out.println ("Their height respectively:" +min +", " +max ) ;
}
}
答案 0 :(得分:0)
创建一个Actor类,如下所示:
public class Actor {
int id;
long serialNumber;
String name;
double height;
//Constructor with parameters
public Actor(int id, long serialNumber, String name, double height) {
id=Id;
serialNumber=SerialNumber;
name =Name;
height= Height;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public long getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(long serialNumber) {
this.serialNumber = serialNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
这是如何使用actor类的示例。请注意,此代码可以改进。
public class Main {
public static void main(String[] args) {
// Create some actresses
Actor actress1= new Actor(1,221114445, "Tracy",5.1);
Actor actress2= new Actor(2,332555412, "Ann", 6.0);
Actor actress3= new Actor(3,200011458, "Rebecca", 5.8);
Actor actress4= new Actor(4,701221496, "Marcy", 5.3);
//Calculate maximum and minimum heights
double maxHeight = Math.max(Math.max(actress1.height, actress2.height),
Math.max(actress3.height, actress4.height));
double minHeight = Math.min(Math.min(actress1.height, actress2.height),
Math.min(actress3.height, actress4.height));
//Determine the tallest
if (maxHeight==actress1.height) {
System.out.println("Actress "+actress1.name +" "
+ "is the tallest with a height of : "+actress1.height);
}
if (maxHeight==actress2.height) {
System.out.println("Actress "+actress2.name +" "
+ "is the tallest with a height of : "+actress2.height);
}
if (maxHeight==actress3.height) {
System.out.println("Actress "+actress3.name +" "
+ "is the tallest with a height of : "+actress3.height);
}
if (maxHeight==actress4.height) {
System.out.println("Actress "+actress4.name +" "
+ "is the tallest with a height of : "+actress4.height);
}
//Determine the shortest
if (minHeight==actress1.height) {
System.out.println("Actress "+actress1.name +" "
+ "is the shortest with a height of : "+actress1.height);
}
if (minHeight==actress2.height) {
System.out.println("Actress "+actress2.name +" "
+ "is the shortest with a height of : "+actress2.height);
}
if (minHeight==actress3.height) {
System.out.println("Actress "+actress3.name +" "
+ "is the shortest with a height of : "+actress3.height);
}
if (minHeight==actress4.height) {
System.out.println("Actress "+actress4.name +" "
+ "is the shortest with a height of : "+actress4.height);
}
}
}