教授布置的一个小程序作业,要求输入 不同的人名和年龄,  在输入 "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

解决方案 »

  1.   


    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); 

    }
    }
      

  2.   

    再修改一下: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); 
    }
    }
      

  3.   

    up对头
    只要在while里的InputAge = stdin.nextInt(); 代码行下
    增加
     if(InputAge>maxAge){
           maxName=InputName;
           maxAge=InputAge;
          }
          if(InputAge<minAge){
           minName=InputName;
           minAge=InputAge;
          }
    便可
      

  4.   

    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; 
    }