我是一個初學者程序員(第一篇文章在這裏!),我無法弄清楚如何用「do while」循環創建錯誤消息。如果給定的輸入不在字母表中,它應該顯示錯誤信息,直到給出只有字母的輸入,然後移動到程序的其餘部分。即使給予正確的輸入,我的代碼似乎也會永久循環。任何建議,非常感謝! :)做while循環錯誤消息
do {
input = JOptionPane.showInputDialog("What is your name?");
if (input.contains("[a-zA-Z]"))
name = input;
else
System.out.println("Please enter a valid name containing: ‘a-z’ or ‘A-Z’ lower or upper case");
} while (!input.contains("[a-zA-Z]"));
這完美地工作,太感謝你了! :) –
@GregSmith不客氣。 –