我对java很陌生,并且承担了一些我坚持的工作。
任务是编写一个java程序,找到组中最短和最高的人,其中该组的人数未知。该程序是询问每个人的名字和身高(以米为单位)。高度应在0.8米至2.5米的范围内进行验证。然后,程序必须显示组中最短和最高人的名称和高度。此外,不允许使用数组和ArrayLists。
我很感激如果有人能指出我正确的方向,目前我似乎无法解决问题。
编辑:我在下面的一些有用的评论之后编写了程序的代码,除了我遇到程序打印空值的问题。以下是代码。
package pracset1;
import java.util.Scanner;
public class shortTall {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
String name;
String maxname = null;
String minname = null;
double height;
double maxheight = -1.0d;
double minheight = 1000.0d;
char addperson;
String choice;
do
{
System.out.println("Enter first name of person");
name = keyboard.next();
System.out.println("Enter height of person");
height = keyboard.nextDouble();
if (height < 0.8 || height > 2.5)
{
System.out.println("Entered height must be above 0.8m and below 2.5m");
height = keyboard.nextDouble();
}
if (height > maxheight)
{
maxheight = height;
maxname = name;
}
else if (height < minheight)
{
minheight = height;
minname = name;
}
System.out.println("Do you want to add another person? (Y/N)");
choice = keyboard.next();
addperson = choice.charAt(0);
}
while (addperson == 'Y' || addperson == 'y'); //
System.out.println("Tallest person is " + maxname + " with a height of " + maxheight);
System.out.println("Shortest person is " + minname + " with a height of " + minheight);
}
}
以下是问题的屏幕截图
答案 0 :(得分:2)
这是伪代码。
Get name and height from input;
if (currentheight>maxHeight) then maxHeight = currentheight; maxName = currentName;
else if (currentheight<minHeight) then minHeight = currentheight; minName = currentName;
Ask if user wants to add one more person;
if yes go back to step 1;
if no print maxName + maxHeight + minName + minHeight;
只是有一个for循环询问用户是否想要将其他人添加到该组,相应地更改最小值和最大值。然后打印出来。
答案 1 :(得分:1)
这样的事情:
double maxheight = -1.0d;
double minheight = 1000.0d;
String maxname = "";
String minname = "";
while (true)
{
String name = System.console().readLine();
double height = Double.parseDouble(System.console().readLine());
if(height < 0.8 || height > 2.5)
{
System.out.println("%d is to large or to small", height);
continue;
}
if (height > maxheight)
{
maxname = name;
maxheight = height;
}
if (height < minheight)
{
minname = name;
minheight = height;
}
System.out.println("%s is heighest person with %d meters", maxname, maxheight);
System.out.println("%s is lowest person with %d meters", minname, minheight);
}
您需要执行一些其他操作,例如让用户退出程序。但是你明白了吗?
修改强>
代码应如下所示:
package pracset1;
import java.util.Scanner;
public class shortTall {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
String name;
String maxname = null;
String minname = null;
double height;
double maxheight = -1.0d;
double minheight = 1000.0d;
char addperson;
String choice;
do
{
System.out.println("Enter first name of person");
name = keyboard.next();
System.out.println("Enter height of person");
height = keyboard.nextDouble();
while (height < 0.8 || height > 2.5)
{
System.out.println("Entered height must be above 0.8m and below 2.5m");
height = keyboard.nextDouble();
}
if (height > maxheight)
{
maxheight = height;
maxname = name;
}
if (height < minheight)
{
minheight = height;
minname = name;
}
System.out.println("Do you want to add another person? (Y/N)");
choice = keyboard.next();
addperson = choice.charAt(0);
}
while (addperson == 'Y' || addperson == 'y'); //
System.out.println("Tallest person is " + maxname + " with a height of " + maxheight);
System.out.println("Shortest person is " + minname + " with a height of " + minheight);
}
}
将第一个if语句更改为while循环。在输入有效高度之前,程序执行将不会继续。
将最后一个if语句更改为if语句。输入的第一个有效人将成为最高和最小的人。
答案 2 :(得分:0)
在伪代码中,算法看起来像这样:
为最高和最矮的人保留名称和身高的变量。
成品。
答案 3 :(得分:0)
我们可以通过多种方式查看此问题。我认为任何排序算法都可以解决这个问题陈述,假设你手头有一套高度。
我继续使用Bubble Sort方法。为了更好的复杂性,您也可以通过快速排序来实现。但是,如果人数很少,那么使用冒号排序也没什么坏处。
作为实时标准解决方案,您可能希望获得更好的命名约定,空检查,异常处理,valdation逻辑等。
以下是代码段。
public class MaxMinHeightFinder {
// logic to sort the elements
public static void sortIndividualsByHeight(double array[]) {
int n = array.length;
int k;
for (int m = n; m >= 0; m--) {
for (int i = 0; i < n - 1; i++) {
k = i + 1;
if (array[i] > array[k]) {
swapNumbers(i, k, array);
}
}
}
printHeights(array);
}
private static void swapNumbers(int i, int j, double[] array) {
double temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static void printHeights(double[] input) {
//System.out.println(""+ Arrays.toString(input));
System.out.println("Min Height : "+ input[0]);
System.out.println("Max Height : "+ input[input.length-1]);
}
public static void main(String[] args) {
double[] heights = { 4.0, 2.5, 9.6, 6.4, 23.2, 12.6, 14.4, 7.5, 1.4 };
sortIndividualsByHeight(heights);
}
}