package test;
import javax.swing.JOptionPane;
public class utsCoba {
public static void main(String[] args) {
int x, y;
String satu = JOptionPane.showInputDialog("Masukan User Pertama");
String dua = JOptionPane.showInputDialog("Masukan User kedua");
boolean Numb1 = isNum(satu);
boolean Numb2 = isNum(dua);
System.out.println("Bilangan 1 >> " + Numb1);
System.out.println("Bilangan 2 >> " + Numb2);
if (Numb1 == true && Numb2 == true) {
x = Integer.parseInt(satu);
y = Integer.valueOf(dua);
jumlah(x, y);
}
else if (Numb1 == true && Numb2 == false) {
x = Integer.parseInt(satu);
jumlah(x, dua);
}
else if (Numb1 == false && Numb2 == true) {
y = Integer.valueOf(dua);
jumlah(satu, y);
} else {
jumlah(satu, dua);
}
}
public static boolean isNum(String s) {
try {
Double.parseDouble(s);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
public static void jumlah(int a, int b) {
System.out.println("A + B = " + a + b);
}
public static void jumlah(int a, String b) {
System.out.println("A + B = " + a + b);
}
public static void jumlah(String a, int b) {
System.out.println("A + B = " + a + b);
}
public static void jumlah(String a, String b) {
System.out.println("A + B = " + a + b);
}
}
contoh penggunaan method isNumber() pada java
Posted by Tutorial Programming on Selasa, 24 April 2012
loading...
|
0 comments:
Posting Komentar