教授布置的一个小程序作业,要求输入 不同的人名和年龄, 在输入 "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
请大侠帮忙查看一下源程序和输出结果,,,给点建议和帮助.谢谢!@!以下是自己写的程序代码:
_____________________________________________________________________________________
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
解决方案 »
- 在校生菜鸟求助,帮忙看下代码,为什么空指针异常?
- java 数据库编程问题:java.sql.BatchUpdateException: 列名 'col0' 无效。求帮助!!!
- JAVA 问题 大家来讨论下
- ToolBar里如何实现下拉式菜单,就像IE里面的后退按钮一样旁边有一个向下的箭头?
- C#与Java对比,一个令我百思不解的问题
- !!请问怎样让一个表格单元退出编辑状态?!!
- java中如何让菜单条折行显示?
- 一个小得不能再小的程序了,但运行java执行时出错,那位高手帮我看看!
- 阅读工具??
- Norwaywoods(挪威的森林),谢谢你,请来拿分呀,天,送分都这么难!!!
- 如何format一个BigDecimal
- 求最新的Java技术创新!!!在线等
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);
}
}
}
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);
}
}
只要在while里的InputAge = stdin.nextInt(); 代码行下
增加
if(InputAge>maxAge){
maxName=InputName;
maxAge=InputAge;
}
if(InputAge<minAge){
minName=InputName;
minAge=InputAge;
}
便可
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;
}