Java 判斷奇偶數及步驟詳解
阿新 • • 發佈:2019-02-06
本文Java使用的是netbeans進行編譯的。
這是新類的程式碼:
import javax.swing.JOptionPane; public class Num { public int intPut(){ String intString=JOptionPane.showInputDialog(null,"請輸入一個整數","判斷奇偶數演示", JOptionPane.QUESTION_MESSAGE); int num=Integer.parseInt(intString);//將String型別的資料轉化為integer整型 return num; } public void jurdge(int a){//判斷奇偶數 if(a%2==0) System.out.println(a+"是偶數"); else System.out.println(a+"是奇數"); } }
這是主類的:
public class NumText {
public static void main(String[] args) {
Num n=new Num();
n.jurdge(n.intPut());
}
}
import javax.swing.JOptionPane;是JOptionPane型別的,
這個型別有 三種常用的:
1.showMessageDialog 是顯示一個帶有“確定”或“OK"的模式的對話方塊。
2.showOptionDialog 是顯示按鈕上的文字,並有多個操作。
3.showInputDialog 是使用者可以輸入的。
String intString=JOptionPane.showInputDialog(null,"請輸入一個整數","判斷奇偶數演示",
JOptionPane.QUESTION_MESSAGE);
這裡使用的是3.後面的Null後面是對話方塊的內容 而question就可以顯示問號左上角的問號。
如:
當然如果是information,warning,error還會有不同的圖示。
隨後將一直推出Java的學習事項,請持續關注。