我已经研究了大约一个小时,我真的无法弄清楚这一点,我想制作一些提示并读取用户的名字和姓氏(分别)的东西。然后打印一个字符串,该字符串由用户名的第一个字母,后跟用户姓氏的前五个字符,后跟一个 10 到 99 范围内的随机数组成。假设姓氏至少有五个字母长
import java.util.Scanner; //Needed for the Scanner class
import java.lang.String;
import java.util.Random;
public class UsernameGenerator
{
public static void main(String[] args) //all the action happens here!
{ Scanner input = new Scanner (System.in);
Random generator = new Random();
int num1;
num1 = generator.nextInt(10-99);
String firstName;
String lastName;
String concatenatedName;
System.out.println("Enter your First Name: ");
firstName = input.next();
System.out.print( "Enter your Last Name: " );
lastName = input.next();
// We'll take the first character in the first name
concatenatedName = firstName.charAt(0) + lastName.substring(0, 5) + num1;
Random rnd = new Random(); // Initialize number generator
if (lastName.length() > 5)
concatenatedName += lastName.substring(0,5);
else
concatenatedName += lastName; // You did not specify what to do, if the name is shorter than 5 chars
concatenatedName += Integer.toString(rnd.nextInt(99));
System.out.println();
}
}