无法为starwars名称生成器编译此代码

时间:2015-09-24 03:56:42

标签: 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 );
}

  }

2 个答案:

答案 0 :(得分:1)

更正了代码并解释了评论中的更改。也请评论。不要只复制此代码。

 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 );
}

  }

答案 1 :(得分:0)

  

firstname无法解析为变量

  1. 您没有声明firstname。不仅firstname您没有声明lastnamemothersnamecityname。所以你应该宣布所有。这些应该是String

  2. 您没有创建Scanner个对象。创建Scanner类对象。

    Scanner input = new Scanner(System.in);
    
  3. 下一步更改。您将cityname声明为两次,并int类型。但nextLine()会返回String而不是int

    String cityname = input.nextLine();//returns String, not int
    cityname = city.substring(0,3);//returns the first 3 characters as String, not int
    
  4. String#substring()返回String而不是int。所以检查整个代码。

    1. 更改

      String StarWarsName = ( "firstname"  + "lastname " + "mothersname " + "cityname");
      
    2.    String StarWarsName = firstname+lastname+" "+mothersname+cityname;
      

      所有这些都是变量而不是值。所以不要加双引号。

      修改:大写firstname

      的第一个字母
         firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1).toLowerCase();