我正在運行一個我創建的用於存儲用戶輸入數據的java程序。具體來說有4個數組列表,分別是songName,songArtist,songYear & songAlbum。 我有一個「songYear」的用戶輸入,我只希望程序接受最多4位數的長度,否則會出錯,這怎麼能實現? 將輸入的長度限制爲x位數
這裏是我有我的外接輸入方法的代碼:
public void addEntry(){
String newName = ui.getString("Enter the name of the track");
songName.add(newName);
String newArtist = ui.getString("Who performs this track");
songArtist.add(newArtist);
String newAlbum = ui.getString("What album is this track from");
songAlbum.add(newAlbum);
System.out.print("What year was the track released? ");
int newYear=input.nextInt(4);
songYear.add(newYear);
System.out.println("\n" + "Thank you, " +songName.get(songName.size()-1) + " has been added to the library.");
System.out.println("\n" + "Press 2 to view your library." + "\n");
}
這是哪一種語言? – IronMan84
哦,道歉。這是java。 – RarkMowe
它必須是CLI?如果是Swings UI,則可以使用事件輕鬆限制它們。 – agirish