标题:编程练习(1)
取消只看楼主
Anstey
Rank: 1
等 级:新手上路
帖 子:94
专家分:0
注 册:2004-4-14
 问题点数:0 回复次数:1 
编程练习(1)

Write a program that asks for the user's name and then writes that name to the monitor with either "Ms." or "Mr." in front, depending if the name is for a female or male. Assume that the only female names are

  • Amy
  • Buffy
  • Cathy

and that the only male names are

  • Elroy
  • Fred
  • Graham

All other names will be echoed without a title. The program continutes looping until the user hits "enter" without first typing a name.

C:\>java Title
Enter a name:
Amy Johnson
Ms. Amy Johnson

Enter a name:
Fred Smith
Mr. Fred Smith

Enter a name:
Zoltan Jones
Zoltan Jones

Enter a name:
(this exercise comes from http://chortle.ccsu.edu/cs151/cs151java.html)

搜索更多相关主题的帖子: 练习 name female 
2004-06-26 22:24
Anstey
Rank: 1
等 级:新手上路
帖 子:94
专家分:0
注 册:2004-4-14
得分:0 

import java.io.*;

public class Name{ public static void main(String [] args) throws IOException { BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in));

final String[] MENNAMES = {"Elroy","Fred","Graham"}; final String[] LADYNAMES = {"Amy","Buffy","Cathy"}; System.out.print("Enter a name: "); String inputName = keyboard.readLine();

while(inputName.length()!=0){ boolean notDone = true; for(int i = 0; i<MENNAMES.length&&notDone; i++){ String familyName = inputName.substring(0,inputName.indexOf(" ")); if(familyName.equals(MENNAMES[i])){ System.out.println("Mr. "+inputName); notDone = false; } else if(familyName.equals(LADYNAMES[i])){ System.out.println("Ms. "+inputName); notDone = false; } } if(notDone) System.out.println(inputName); System.out.print("Enter a name: "); inputName = keyboard.readLine(); } } }

//随便写了一下,可能有错误,呵呵,多多指教


Anstey. Cheers. I love CAPPUCCINO~~
2004-06-27 17:20



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-2883-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022096 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved