Java 子串法

Java 子串法,java,Java,我必须编写这个程序来输出名字的第一个字母和姓氏的前五个字母加上10-99之间的随机数。这是可行的,但我认为子字符串方法从0开始,然后从0开始,这样子字符串(0,0)将只包含第一个字母,同样地,子字符串(0,4)将包含前5个字母的0 1 2 3 4个字母。或者它不包括输出中的最终数字 import java.util.Scanner; import java.util.Random; public class NameModifier { public static void main (St

我必须编写这个程序来输出名字的第一个字母和姓氏的前五个字母加上10-99之间的随机数。这是可行的,但我认为子字符串方法从0开始,然后从0开始,这样子字符串(0,0)将只包含第一个字母,同样地,子字符串(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();

    }
}

Java的子字符串输入

public String substring(int beginIndex,int endIndex)

beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
注意排他性。子字符串(0,1)将返回一个包含字符0的字符串,最多包含但不包括字符1


来源:,int)

Java的子字符串输入

public String substring(int beginIndex,int endIndex)

beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
注意排他性。子字符串(0,1)将返回一个包含字符0的字符串,最多包含但不包括字符1


来源:,int)

我希望你能更好地理解这个例子:


我希望您能通过这个例子更好地理解:


@Lou44帮助记住此行为的一个方法是
substring()
的工作方式与典型的for next循环类似,该循环会在最后一个数字之前停止一个循环。e、 g.
for(int i=1;i@Lou44帮助记住此行为的一种方法是,
substring()
的工作方式与典型的for next循环类似,该循环在最后一个数字之前停止一次。例如,
for(int i=1;i