无法为星球大战名称生成器完成此代码
having trouble compling this code for a starwars name generator
我需要使用真实名字的前三个字母+真实姓氏的前两个字母,以及母亲娘家姓的前两个字母+出生城市的前三个字母。我还需要将第一个字母大写。使用 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 );
}
}
firstname cannot be resolved to a variable
您没有声明 firstname
。不仅 firstname
你没有声明 lastname
、mothersname
、cityname
。所以你应该全部申报。那些应该是 String
.
您没有创建 Scanner
对象。创建 Scanner
class 对象。
Scanner input = new Scanner(System.in);
下一个变化。您将 cityname
声明为两次并声明为 int
类型。但是 nextLine() returns 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() returns 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();
更正了代码并在注释中解释了更改。也通过评论。不要只是复制此代码。
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 );
}
}
我需要使用真实名字的前三个字母+真实姓氏的前两个字母,以及母亲娘家姓的前两个字母+出生城市的前三个字母。我还需要将第一个字母大写。使用 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 );
}
}
firstname cannot be resolved to a variable
您没有声明
firstname
。不仅firstname
你没有声明lastname
、mothersname
、cityname
。所以你应该全部申报。那些应该是String
.您没有创建
Scanner
对象。创建Scanner
class 对象。Scanner input = new Scanner(System.in);
下一个变化。您将
cityname
声明为两次并声明为int
类型。但是 nextLine() returnsString
不是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() returns 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();
更正了代码并在注释中解释了更改。也通过评论。不要只是复制此代码。
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 );
}
}