-2

编写一个应用程序,提示用户输入全名和街道地址,并根据用户的姓名首字母和地址的数字部分构造一个 ID。例如,住在 34 Elm 的用户 William Henry Harrison 的 ID 为 WHH34,而住在 1778 Monroe 的用户 Addison Mitchell 的 ID 为 AM1778。将文件另存为 ConstructID.java。

到目前为止,这是什么,我的老师说错了……

import java.util.*;
public class ConstructID {

    public static void main(String[] args) {

        String name1, address1, address2;

        Scanner kevs = new Scanner(System.in);

        System.out.println("Enter your fullname. Encluding Middle Initial, Separated by spaces.");

        name1 = kevs.next();
        name2 = kevs.next();
        name3 = kevs.next();

        name1 = name1.toUpperCase();
        name2 = name2.toUpperCase();
        name3 = name3.toUpperCase();

        name1 = name1.substring(0,1);
        name2 = name2.substring(0,1);
        name3 = name3.substring(0,1);

        System.out.println("\nEnter your address. Separated by spaces.");

        address1 = kevs.next();
        address2 = kevs.nextLine();


        do {

            if (address1 == address1.substring(0,1) || address1 == address1.substring(0,2) || address1 == address1.substring(0,3) || address1 == address1.substring(0,4) || address1 == address1.substring(0,5) || address1 == address1.substring(0,6))
            System.out.println("\nYour ID: " + name1 + name2 + name3 + address1);

        } while (address1 == address2);
    }
}

注意:我不能使用数组:(这个问题的主题都是关于循环和字符串..没有数组..所以请帮助.. :((

4

3 回答 3

1

如果不能使用数组,请使用列表。查看javadocsjava.util.List了解列表可以做什么。(或者你的讲义!)

我希望你的老师说你写的东西是错误的,是因为它假设每个人的名字都由名字、中间名和姓氏组成。我相信你知道这是不正确的。有些人有很多中间名,或者根本没有。的确,有些人只有一个名字。

你的老师想要的是可以处理任意数量的名字的代码。数组将是一个糟糕的选择......因为在从用户那里读取名称之前,您需要预测数组的大小。

于 2013-03-20T03:44:10.130 回答
0
    String name1, address1;

    Scanner kevs = new Scanner(System.in);

    System.out.println("Enter your fullname. Encluding Middle Initial, Separated by spaces.\n");
    name1 = kevs.nextLine();
    name1 = name1.toUpperCase();
    StringTokenizer tokens = new StringTokenizer(name1);
    String name = "";
    while(tokens.hasMoreTokens()) {
        String value= tokens.nextToken();
        name += value.substring(0,1);
    }
    System.out.println("\nEnter your  full address. Separated by spaces.\n");
    address1 = kevs.nextLine();
    address1 = address1.toUpperCase();
    StringTokenizer tokens2 = new StringTokenizer(address1);
    Integer numericAddress = null;
    while(tokens2.hasMoreTokens()) {
        String value1= tokens2.nextToken();
        try {
            numericAddress = Integer.valueOf(value1);
        }catch(NumberFormatException ne) {
            continue;
        }
        break;
    }

    String output = name+numericAddress.toString();

    System.out.println(output);
于 2013-03-20T03:57:55.150 回答
-1
 public static void main(String[] args) {

        String name, address;

        Scanner kevs = new Scanner(System.in);

        System.out.println("Enter your fullname. Including Middle Initial, Separated by spaces.");

        //get the full name
        name = kevs.nextLine();

        System.out.println("\nEnter your address. Separated by spaces.");
        //get the address
        address = kevs.nextLine();

        String initials = "";
        //get the first letter of the name and add it to our initial string
        char c = name.charAt(0);
        initials += c;
        for (int i = 0; i < name.length(); i++) {
            char letter = name.charAt(i);
            // if we find a space, select the first letter after it until the end
            if (letter == ' ') {
                initials += name.charAt(i + 1);
            }

        }
        String addressNum = "";
        //this bool is so that we only select characters up to the first space
        boolean finished = false;
        for (int i = 0; i < address.length(); i++) {
            if (!finished) {
                char num = address.charAt(i);
                if (num != ' ') {
                    //add characters to the address string until there is a space
                    addressNum += num;
                } else // we found the first space so we are now finished
                {
                    finished = true;
                }
            } else //we are finished so leave the loop
            {
                break;
            }
        }
        //concatenate the strings
        System.out.println(initials + addressNum);

    }
于 2013-03-20T04:04:22.153 回答