Perintah Input pada Java

Berikut ini adalah perintah untuk memberikan nilai / input pada bahasa java, dengan menggunakan class utility  Scanner :

//Membuat objek dari class scanner:
Scanner input = new Scanner(System.in); 
//membuat tampilan perintah input:
 System.out.print("Masukkan bilangan pertama: ");
//membuat input masuk ke dalam variabel bilangan1
 int bilangan1 = input.nextInt();

Contoh Program Perkalian.java :

package input;

/** 

* @author agung 
*/
import java.util.Scanner;
public class Perkalian {
    public static void main(String[] args){
		Scanner input = new Scanner(System.in);
    		System.out.print("Masukkan bilangan pertama: ");
		int bilangan1 = input.nextInt();

        	System.out.print("Masukkan bilangan kedua: ");
		int bilangan2 = input.nextInt();

                System.out.print("Hasil perkalian: " +
				(bilangan1 * bilangan2));
    }

}

Hasil Output :

Masukkan bilangan pertama: 2
Masukkan bilangan kedua: 6
Hasil perkalian: 12BUILD SUCCESSFUL (total time: 7 seconds)

~~Hope it helps ~~

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: