{小程序员求助大侠帮助!}

charlesccc 2009-10-15 07:05:27
教授布置的一个小程序作业,要求输入 不同的人名和年龄, 在输入 "quit" 后输出最大的人和最小的人的姓名和年龄..

而且他还特别要求输入的字体为粗体,本人也还正在研究当中...但我觉得最主要的问题是我最后的输出有些麻烦,
请大侠帮忙查看一下源程序和输出结果,,,给点建议和帮助.谢谢!@!

以下是自己写的程序代码:
_____________________________________________________________________________________
package findAge;
import java.util.*;
import java.lang.String;

public class Age {

public static void main(String[] args) {
String InitialName,InputName,maxName = null,minName = null,StopInput;
int InitialAge,InputAge = 0,maxAge,minAge;
Scanner stdin = new Scanner(System.in);
boolean equals;

System.out.println("Please enter the name : ");
InitialName = stdin.next();
maxName=minName=InitialName;
System.out.println("Please enter the age of this person : ");
InitialAge = stdin.nextInt();
maxAge=minAge=InitialAge;

while(true){ //我想主要是在这里可能要改,但我实在想不到更好的方法了,已经尝试很多次了
System.out.println("Please enter the name : ");
InputName = stdin.next();
if(equals = InputName.equalsIgnoreCase("quit"))break;
System.out.println("Please enter the age of this person : ");
InputAge = stdin.nextInt();
}

if(InputAge<minAge){
minAge = InputAge;
minName = InputName;
}else if(InputAge>maxAge){
maxAge = InputAge;
maxName = InputName;
}

if(equals = InputName.equalsIgnoreCase("quit")){
System.out.println("The oldest person is: " + maxName);
System.out.println("The age of the oldest person is: " + maxAge);
System.out.println("The youngest person is: " + minName);
System.out.println("The age of the youngest person is: " + minAge);
}
}
}


_____________________________________________________________________________________________
以下是输入和输出的结果:

Please enter the name :
a
Please enter the age of this person :
10
Please enter the name :
b
Please enter the age of this person :
20
Please enter the name :
c
Please enter the age of this person :
30
Please enter the name :
quit
The oldest person is: quit <----------- 这里出问题了..名字显示不对,我尝试了很久,都没有找到解决的办法!忘
The age of the oldest person is: 30 大侠们赐教,本人感激不尽...
The youngest person is: a
The age of the youngest person is: 10
...全文
81 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
charlesccc 2009-10-15
  • 打赏
  • 举报
回复
哥们太感谢你了!@!
阿_布 2009-10-15
  • 打赏
  • 举报
回复
while(true){ //我想主要是在这里可能要改,但我实在想不到更好的方法了,已经尝试很多次了
System.out.println("Please enter the name : ");
InputName = stdin.next();
if(equals = InputName.equalsIgnoreCase("quit"))break;
System.out.println("Please enter the age of this person : ");
InputAge = stdin.nextInt();
} 关键在这里,你这个地方就把循环结束了,你可以输入3组试一下,就算break了,这个时候InputAge就是上一次输入的值,如果上一次的值是最大或者最小值的话,quit就会当做最小或者最大年龄中的名称。

if(InputAge <minAge){
minAge = InputAge;
minName = InputName;
}else if(InputAge>maxAge){
maxAge = InputAge;
maxName = InputName;
}
charlesccc 2009-10-15
  • 打赏
  • 举报
回复
难道仅仅是调换下循序就行了吗,能否稍微解释一下..没太想明白,,,!@!
cweijiaweil 2009-10-15
  • 打赏
  • 举报
回复
up对头
只要在while里的InputAge = stdin.nextInt(); 代码行下
增加
if(InputAge>maxAge){
maxName=InputName;
maxAge=InputAge;
}
if(InputAge<minAge){
minName=InputName;
minAge=InputAge;
}
便可
阿_布 2009-10-15
  • 打赏
  • 举报
回复
再修改一下:

import java.util.*;

public class TestAge {

public static void main(String[] args) {
String InitialName,InputName,maxName = null,minName = null,StopInput;
int InitialAge,InputAge = 0,maxAge,minAge;
Scanner stdin = new Scanner(System.in);
boolean equals;

System.out.println("Please enter the name : ");
InitialName = stdin.next();
maxName=minName=InitialName;
System.out.println("Please enter the age of this person : ");
InitialAge = stdin.nextInt();
maxAge=minAge=InitialAge;

while(true){ //我想主要是在这里可能要改,但我实在想不到更好的方法了,已经尝试很多次了
System.out.println("Please enter the name : ");
InputName = stdin.next();
if(InputName.equalsIgnoreCase("quit")) break;
System.out.println("Please enter the age of this person : ");
InputAge = stdin.nextInt();

if(InputAge <minAge){
minAge = InputAge;
minName = InputName;
}else if(InputAge>maxAge){
maxAge = InputAge;
maxName = InputName;
}
}
System.out.println("The oldest person is: " + maxName);
System.out.println("The age of the oldest person is: " + maxAge);
System.out.println("The youngest person is: " + minName);
System.out.println("The age of the youngest person is: " + minAge);
}
}
阿_布 2009-10-15
  • 打赏
  • 举报
回复

import java.util.*;

public class TestAge {

public static void main(String[] args) {
String InitialName,InputName,maxName = null,minName = null,StopInput;
int InitialAge,InputAge = 0,maxAge,minAge;
Scanner stdin = new Scanner(System.in);
boolean equals;

System.out.println("Please enter the name : ");
InitialName = stdin.next();
maxName=minName=InitialName;
System.out.println("Please enter the age of this person : ");
InitialAge = stdin.nextInt();
maxAge=minAge=InitialAge;

while(true){ //我想主要是在这里可能要改,但我实在想不到更好的方法了,已经尝试很多次了
System.out.println("Please enter the name : ");
InputName = stdin.next();
if(InputName.equalsIgnoreCase("quit")) break;
System.out.println("Please enter the age of this person : ");
InputAge = stdin.nextInt();

if(InputAge <minAge){
minAge = InputAge;
minName = InputName;
}else if(InputAge>maxAge){
maxAge = InputAge;
maxName = InputName;
}

System.out.println("The oldest person is: " + maxName);
System.out.println("The age of the oldest person is: " + maxAge);
System.out.println("The youngest person is: " + minName);
System.out.println("The age of the youngest person is: " + minAge);
}
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧