java - 无法完成星球大战名称生成器的此代码

标签 java computer-science

我需要使用实际名字的前三个字母+实际姓氏的前两个字母,以及母亲婚前姓氏的前两个字母+出生城市的前三个字母。我还需要将第一个字母大写。使用 toUpperCase() 和 toLowerCase()。谢谢!

import java.util.Scanner;

public class Assignment2
{
    public static void main(String[] args) 
    {
    System.out.printf("Enter your first name: ");
    /* This should be string as your gettting name*/
    String firstname = input.nextLine(); 
    firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1);
     /* Don't need new variable use same and that also should be string. */
    System.out.printf("Enter your last name: ");

    String lastname = input.nextLine();
    lastname = lastname.substring(0,2). toUpperCase() + lastname.substring(1);

    System.out.printf("Enter your mother's maiden name: ");
    String mothersname = input.nextLine();
    mothersname = mothersname.substring(0,2);

    System.out.printf("Enter the name of the city in which you were born: ");
    String cityname = input.nextLine();
    cityname = cityname.substring(0,3);

    String StarWarsName = firstname+lastname+mothersname+cityname;
    System.out.println("May the force be with you, " + StarWarsName );
}

  }

//* Updated code 
import java.util.Locale;
import java.util.Scanner;

public class Assignment2
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.printf("Enter your first name: ");
        String firstname = input.nextLine(); 
        firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1).toLowerCase();

    System.out.printf("Enter your last name: ");
    String lastname = input.nextLine();
    lastname = lastname.substring(0,1). toUpperCase() + lastname.substring(1).toLowerCase(); 

    System.out.printf("Enter your mother's maiden name: ");
    String mothersname = input.nextLine();
    mothersname = mothersname.substring(0,2);

    System.out.printf("Enter the name of the city in which you were born: ");
    String cityname = input.nextLine();
    cityname = cityname.substring(0,3);

    String StarWarsName = firstname+lastname+" "+mothersname+cityname;
    System.out.println("May the force be with you, " + StarWarsName );
}

  }

最佳答案

更正了代码并在注释中解释了更改。也浏览一下评论。不要只是复制此代码。

 import java.util.Scanner;

public class test1
{
    public static void main(String[] args) 
    {
     Scanner input = new Scanner();
    System.out.printf("Enter your first name: ");
    /* This should be string as your gettting name*/
    String firstname = input.nextLine();
     /* Don't need new variable use same and that also should be string. */
    firstname = firstname.substring(0,1);
    System.out.printf("Enter your last name: ");

    String lastname = input.nextLine();
    lastname = lastname.substring(0,2);

    System.out.printf("Enter your mother's maiden name: ");
    String mothersname = input.nextLine();
    mothersname = mothersname.substring(0,1);

    System.out.printf("Enter the name of the city in which you were born: ");
    String cityname = input.nextLine();
    cityname = cityname.substring(0,2);

    String StarWarsName = ( "firstname"  + "lastname " + "mothersname " + "cityname"); 
    System.out.println("May the force be with you, " + StarWarsName );
}

  }

关于java - 无法完成星球大战名称生成器的此代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32753071/

相关文章:

java - JavaFX 8 中文本区域的透明背景

computer-science - "Finding all the code in a given binary is equivalent to the Halting problem."真的吗?

python - 'len' 、 'dir' 、 'vars' 是什么名字?

algorithm - 为什么lgn和log8n的渐近关系等价于logn为Θ(log8n)?

java - 如何使用 Appium Java 客户端查找 iOS 元素?

java - 如何为多个数据库表创建表单?

database - 关于关系规范化的问题

recursion - 什么是递归以及何时应该使用它?

java - 如何使用java解码ascii文件中的comp 3打包字段?

java - 使用 JarOutputStream 创建的 jar 的 URLClassLoader 问题