第六題:
設計一程式,用迴圈表示輸入 1-7 數字則顯示對應星期 (星期一、星期二......星期天),
若非 1-7 顯示輸入錯誤,直至輸入 0 則程式結束。
----------------------------------------------------------
參考程式
----------------------------------------------------------
import javax.swing.JOptionPane;
class demo2{
public static void main(String args [ ]) {
int input=1;
String a;
JOptionPane.showMessageDialog(null," 輸入整數1-7 判斷星期幾, 輸入0 程式結束. ", " 迴圈判斷程式",JOptionPane.PLAIN_MESSAGE);
while (input != 0) {
a = JOptionPane.showInputDialog(" 請輸入1 -7 之間的整數, 輸入0 程式結束");
input = Integer.parseInt(a);
if(input ==1 ) { JOptionPane.showMessageDialog(null,"判斷結果是星期一", " 迴圈判斷程式",JOptionPane.PLAIN_MESSAGE); }
else if (input ==2 ) { JOptionPane.showMessageDialog(null,"判斷結果是星期二", " 迴圈判斷程式",JOptionPane.PLAIN_MESSAGE); }
else if (input ==3 ) { JOptionPane.showMessageDialog(null,"判斷結果是星期三", " 迴圈判斷程式",JOptionPane.PLAIN_MESSAGE); }
else if (input ==4 ) { JOptionPane.showMessageDialog(null,"判斷結果是星期四", " 迴圈判斷程式",JOptionPane.PLAIN_MESSAGE); }
else if (input ==5 ) { JOptionPane.showMessageDialog(null,"判斷結果是星期五", " 迴圈判斷程式",JOptionPane.PLAIN_MESSAGE); }
else if (input ==6 ) { JOptionPane.showMessageDialog(null,"判斷結果是星期六", " 迴圈判斷程式",JOptionPane.PLAIN_MESSAGE); }
else if (input ==7 ) { JOptionPane.showMessageDialog(null,"判斷結果是星期天", " 迴圈判斷程式",JOptionPane.PLAIN_MESSAGE); }
else if (input !=0) { JOptionPane.showMessageDialog(null,"輸入錯誤", " 迴圈判斷程式",JOptionPane.PLAIN_MESSAGE); }
}
JOptionPane.showMessageDialog(null,"輸入0 程式結束", " 迴圈判斷程式",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}