1. JOptionPane
2. Fungsi Scanner
3. Buffered Reader
Inilah cara dan codingnya :
Coding 1
package Praktek1;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Penjumlahan {
public static void main(String[] args) {
//Scanner
Scanner input = new Scanner(System.in);
System.out.println("Masukkan Bilangan 1 : ");
String input1 = input.nextLine();
//Buffered Reader
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
String input2 = "";
System.out.print("Masukkan Bilangan : ");
try {
input2 = dataIn.readLine();
} catch (IOException e) {
System.out.println("Error");
}
//JOptionPane
String input3 = JOptionPane.showInputDialog("Masukkan Bilangan 3 : ");
//Instansiasi
int bil1 = Integer.parseInt(input1);
int bil2 = Integer.parseInt(input2);
int bil3 = Integer.parseInt(input3);
//Proses Penjumlahan
int hsl = bil1 + bil2 + bil3;
//Menampilkan Hasil dengan JOptionPane
JOptionPane.showMessageDialog(null, "Hasil dari "
+ bil1 + " + " + bil2 + " + " + bil3 + " adalah " + "\n" + hsl, "Hasil", JOptionPane.PLAIN_MESSAGE);
}
}
Coding 2
package Praktek1;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Penjumlahan {
public static void main(String[] args) {
//Scanner
Scanner input = new Scanner(System.in);
System.out.println("Masukkan Bilangan 1 : ");
int bil1 = input.nextInt();
//BufferedReader
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
int bil2 = 0;
System.out.println("Masukkan Bilangan 2 : ");
try {
bil2 = Integer.parseInt(dataIn.readLine());
} catch (IOException e) {
System.out.println("Error!");
}
//JOptionPane
String input3 = JOptionPane.showInputDialog("Masukkan Bilangan 3 : ");
int bil3 = Integer.parseInt(input3);
//Proses Penjumlahan
int hsl = bil1+ bil2 + bil3;
//Menampilkan Hasil dengan JOptionPane
JOptionPane.showMessageDialog(null,"Hasil dari " + bil1 + " + " + bil2 + " + " + bil3 +
" adalah " +"\n" + hsl, "Hasil", JOptionPane.PLAIN_MESSAGE);
}
}
Selamat Mencoba dan Temukan Perbedaan Dari Dua Coding Tersebut !!!
0 komentar:
Posting Komentar