- Back to Home »
- Input dari Keyboard
Posted by : Unknown
Tuesday, April 26, 2016
Nama : Asep Kurniawan
NPM : 31114733
Kelas : 2DB01
1. Kelas Scanner
Script :
/*
* To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package inputscanner;
/**
*
* @author Asep
*/
import java.util.Scanner;
public class InputScanner {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner Angka = new Scanner (System.in);
int a;
int b;
int c;
System.out.print("Masukkan Angka Pertama : ");
a = Angka.nextInt();
System.out.print("Masukkan Angka Kedua : ");
b = Angka.nextInt();
System.out.print("Masukkan Angka Ketiga : ");
c = Angka.nextInt();
System.out.println("Rata-ratanya adalah : " + ((a+b+c)/3));
}
}
2. Kelas BufferReader
Script :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package inputbufferedreader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class InputBufferedReader {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
BufferedReader Angka = new BufferedReader (new InputStreamReader (System.in));
int p;
int q;
int r;
System.out.println ("Masukkan 3 angka : ");
try {
System.out.print(" Angka Pertama : ");
p = Integer.parseInt(Angka.readLine());
System.out.print(" Angka Kedua : ");
q = Integer.parseInt (Angka.readLine());
System.out.print(" Angka Ketiga : ");
r = Integer.parseInt (Angka.readLine());
System.out.println("Rata-ratanya adalah " +((p+q+r)/3));
}
catch (IOException e) {
System.out.println("Error !!!");
}
}
}
3. GUI
Script :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package inputgui;
/**
*
* @author Asep
*/
import javax.swing.JOptionPane;
public class InputGUI {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String p;
p = JOptionPane.showInputDialog("Angka Pertama : ");
String q;
q = JOptionPane.showInputDialog("Angka Kedua : ");
String r;
r = JOptionPane.showInputDialog("Angka Ketiga : ");
int angka1 = Integer.parseInt(p);
int angka2 = Integer.parseInt(q);
int angka3 = Integer.parseInt(r);
JOptionPane.showMessageDialog(null,"Rata-ratanya adalah " +((angka1+angka2+angka3)/3));
}
}