java - 我必须编写一个程序来收集高度和序列号并输出 maxq min
问题描述
我需要编写一个程序来获取一些女演员(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 ) ;
}
}
解决方案
创建一个 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);
}
}
}
推荐阅读
- php - 移动“添加到购物车”按钮和价格变化下拉列表
- android - 如何使用 Android Management API 禁用摄像头
- python - 使用 discord.py 更改不和谐文本频道的权限
- c++ - 从 C++ 项目调用 octave .m 文件,未定义对 octave::feval 错误的引用
- java - 如何从扩展线程的类在 JPanel 中启动动画(将圆从一个点移动到另一个点)?
- node.js - Query/headers too large on Azure if auth enabled
- node.js - 在全局范围内访问 Firestore 数据
- python - 如何使用 regex NOT IN 添加过滤器?
- python - 有没有办法在 seaborn 点图中设置透明度/alpha 级别?
- css - 具有可变边框宽度的css圆形掩码