| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1381 人关注过本帖
标题:编程练习(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
快速回复:编程练习(1)
数据加载中...
 
   



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

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