| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1382 人关注过本帖
标题:编程练习(1)
只看楼主 加入收藏
Anstey
Rank: 1
等 级:新手上路
帖 子:94
专家分:0
注 册:2004-4-14
收藏
 问题点数:0 回复次数:2 
编程练习(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
jellen
Rank: 1
等 级:新手上路
威 望:1
帖 子:107
专家分:0
注 册:2004-5-3
收藏
得分:0 

This is my answer for this question:

/* Test.java */

import java.io.*; public class Test { //判断是否为男士 static boolean isMale(String s) { if(s.startsWith("Elroy") || s.startsWith("Fred") || s.startsWith("Graham")) return true; else return false; } //判断是否为女士 static boolean isFemale(String s) { if(s.startsWith("Amy") || s.startsWith("Buffy") || s.startsWith("Cathy")) return true; else return false; } public static void main(String[] args)throws IOException { BufferedReader data = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a name:"); String str = data.readLine();

while(str.length() != 0) { if(isMale(str)) System.out.println("Mr. " + str); else if(isFemale(str)) System.out.println("Ms. " + str); else System.out.println(str);

System.out.println("Enter a name:"); str = data.readLine(); } } }


再见,理想!
2004-06-27 13:07
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.029677 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved