ユーザー入力の最初の文字を取得してユーザー名を生成するプログラムを作成しようとしています。ユーザーが入力を空白のままにすると、ユーザー名を生成するために使用される文字がデフォルトで文字「z」になるように書き込もうとしています。
これが私の完全なコードです:
import java.util.Scanner;
/**
UsernameGenerator.java
Generates a username based on the users inputs.
@author: Evan Fravert
*/
public class UsernameGenerator {
/**
* Generates a username based on the users inputs.
*@param args command line argument
*/
public static void main(String[] args)
{ // abcde
String first;
String middle;
String last;
String password1;
String password2;
int randomNum;
randomNum = (int) (Math.random() * 1000) + 100;
Scanner userInput = new Scanner(System.in);
System.out.println("Please enter your first name:");
first = userInput.nextLine();
String firstLower = first.toLowerCase();
System.out.println("Please enter your middle name:");
middle = userInput.nextLine();
String middleLower = middle.toLowerCase();
System.out.println("Please enter your last name:");
last = userInput.nextLine();
int lastEnd = last.length()-1;
String lastLower = last.toLowerCase();
System.out.println("Please enter your password:");
password1 = userInput.nextLine();
System.out.println("Please enter your password again:");
password2 = userInput.nextLine();
char firstLetter = firstLower.charAt(0);
char middleLetter = middleLower.charAt(0);
char lastLetter = lastLower.charAt(0);
char lastLast = lastLower.charAt(lastEnd);
if first.length() == 0) {
firstLetter = 'z';
}
else {
firstLetter = firstLower.charAt(0);
}
System.out.println("Your username is " + firstLetter + ""
+ middleLetter + "" + lastLetter + "" + "" + lastLast + "" + randomNum);
System.out.println("Your password is " + password1);
System.out.println("Welcome " + first + " " + middle + " " + last + "!");
}
}