我需要使用实际名字的前三个字母+实际姓氏的前两个字母,以及母亲姓氏的前两个字母+出生城市的前三个字母。我还需要将第一个字母大写。使用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 );
}
}
答案 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无法解析为变量
您没有声明firstname
。不仅firstname
您没有声明lastname
,mothersname
,cityname
。所以你应该宣布所有。这些应该是String
。
您没有创建Scanner
个对象。创建Scanner
类对象。
Scanner input = new Scanner(System.in);
下一步更改。您将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
String#substring()返回String
而不是int
。所以检查整个代码。
更改
String StarWarsName = ( "firstname" + "lastname " + "mothersname " + "cityname");
到
String StarWarsName = firstname+lastname+" "+mothersname+cityname;
所有这些都是变量而不是值。所以不要加双引号。
修改:大写firstname
firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1).toLowerCase();