我必须编写这个程序来输出名字的第一个字母和姓氏的前五个字母加上一个从 10 到 99 的随机数。它有效,但我认为 substring 方法从 0 开始并从那里开始,因此 substring(0,0) 将仅包含第一个字母,同样 substring(0,4) 将包含前 5 个字母 0 1 2 3 4 个字母. 或者它不包括输出中的最终数字?
import java.util.Scanner;
import java.util.Random;
public class NameModifier
{
public static void main (String[] args)
{
String namefirst;
String namelast;
Random generator = new Random();
int num;
num = generator.nextInt(99) + 10;
Scanner scan = new Scanner (System.in);
//prompts user
System.out.print ("Enter your first name: ");
namefirst = scan.nextLine();
System.out.print ("Enter your last name: ");
namelast = scan.nextLine();
System.out.println("Your entered: " + namefirst + " " + namelast);
//outputs modified username possibility
System.out.println("Here is a random username for you: ");
System.out.print (namefirst.substring(0, 1));
System.out.print (namelast.substring(0,5));
System.out.print (num);
scan.close();
}
}