标题:编程练习(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



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




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

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