0

I want to make a simple code, that prompts you to enter names, separated by comma or just a space, and when you click enter, to take every one word you entered, and put a @gmail.com at the end of it, how can I do it? That's what I have for now

    Scanner input = new Scanner(System.in);
    String mail = "@gmail.com";
    String names;
    System.out.println("Enter names: ");
    names = input.next();
    System.out.println(names + mail);
4

2 回答 2

0

Not knowing what language this is, here's the pseudo-code:

names = input.next();
namesArray = names.split(" ") -- replace with your preferred delimiter
foreach name in namesArray
  print name + mail
于 2013-03-30T16:09:38.757 回答
0

This should be everything you asked for, if you put a list of names separated by commas it will loop through them, otherwise it will just print a single name.

    Scanner input = new Scanner(System.in);
    String mail = "@gmail.com";
    System.out.println("Enter names: ");
    String names = input.next();
    if(names.contains(",")) {
            for(String name : names.split(",")) {
                    System.out.println(name + mail);
            }
    } else {
            System.out.println(names + mail);
    }

Hope that helps.

于 2013-03-30T16:35:18.460 回答