PERULANGAN / LOOPING
*** PERULANGAN
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul4;
/**
*
* @author anik nurul
*/
public class Tugas1 {
public static void
main(String[] args) {
int jml=0;
for(int i=1;i<=10;i++)
{
jml+=i;
System.out.println(i);
}
System.out.println("Jumlah Semua Bilangan :
"+jml);
}
}
*** MENGHITUNG PANGKAT DENGAN LOOPING
package modul4;
public class Tugas1a {
public static void
main(String[]arg)
{
int jml=0;
int a=1;
int b=1;
for(int i=1;i<=5;i++){
if (i<=2){
a*=5;} // 5
pangkat 2
if (i<=3) {
b*=4; // 4 pagkat 3
}
jml=a+b;
}
System.out.println("5pangkat2 + 4pangkat3
= "+jml);
}
}
*** MENGHITUNG FAKTORIAL DENGAN LOOPING
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul4;
/**
*
* @author anik nurul
*/
public class Tugas1b {
public static
void main(String[] args) {
int jml=0;
int a=1;
int b=1;
for(int i=1;i<=5;i++){
a=a*i;}
for(int i=1;i<=4;i++){
b=b*i;}
jml=a+b;
//System.out.println(a);
//System.out.println(b);
System.out.println("5!+4! =
"+jml);
}
}
*** MENCARI GENAP GANJIL MENGGUNAKAN JOPTION PANE
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul4;
/**
*
* @author anik nurul
*/
import javax.swing.JOptionPane;
public class Tugas1c {
public static void
main(String[] args) {
String nilai =
JOptionPane.showInputDialog(null, "Masukkan Angka");
int
nilai2=Integer.parseInt(nilai);
int
jml=0,jml2=0,total=0,total2=0;
for (int
i=1;i<=nilai2;i++){
if (i%2==0){
JOptionPane.showMessageDialog(null,i +" = genap");
// jml++;
// total+=i;
}
else
JOptionPane.showMessageDialog(null, i+" = ganjil");
//jml2++;
// total2+=i;
}
}
}
*** MENCARI GENAP GANJIL MEGGUNAKAN BUFFEREDREADER
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul4;
/**
*
* @author anik nurul
*/
import java.io.*;
public class Tugas1d {
public static void
main(String[] args) {
BufferedReader m=new
BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("masukkan angka");
String
k=m.readLine();
int
n=Integer.parseInt(k);
int
jml=0,jml2=0,total=0,total2=0;
for (int
i=1;i<=n;i++){
if (i%2==0){
System.out.println(i+":genap");
jml++;
total+=i;
}
if (i%2==1){
System.out.println(i+":ganjil");
jml2++;
total2+=i;
}
}
System.out.println("jumlah
genap="+jml);
System.out.println("jumlah ganjil="+jml2);
System.out.println("total="+(total+total2));
}
catch (Exception e){
System.out.println("Error!!"+e.getMessage());
}
}
}
*** MEMBUAT BINTANG
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul4;
/**
*
* @author anik nurul
*/
import javax.swing.*;
public class Tugas2 {
public static
void main( String[] args )
{
String angka=JOptionPane.showInputDialog("Masukkan
Angka Yang Anda Inginkan : ");
int n=Integer.parseInt(angka);
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print("*");}
for(int k=n;k>=i;k--){
System.out.print(" ");}
for(int l=n;l>=i;l--){
System.out.print("*");}
System.out.println();
}
}
}
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul4;
/**
*
* @author anik nurul
*/
import javax.swing.*;
public class Tugas2_b {
public static void main (String []args)
{
String
angka=JOptionPane.showInputDialog("Masukkan angka yang Anda inginkan
");
int n=Integer.parseInt(angka);
for(int i=n;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print("*");}
for(int k=i;k<=n;k++){
System.out.print(" ");}
for(int l=1;l<=i;l++){
System.out.print("*");}
System.out.println();
}
}
}
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul4;
/**
*
* @author anik nurul
*/
import javax.swing.*;
public class tugas_bawa_pulang {
public static void
main(String[] args) {
/*
for (int i =
1; i <= 4; i++) {
// membuat
segitiga kosong pertama
for (int j
= 1; j <= i; j++) {
System.out.print(" ");
}
// membuat
segitiga pertama tinggi 3 lebar 3
for (int k
= 3; k >= i; k--) {
System.out.print("*");
//
segitiga kedua tinggi 4 lebar 4
}
for (int l
= 4; l >= i; l--) {
System.out.print("*");
//
segtiga kosong kedua
}
for (int m
= 1; m <= i; m++) {
System.out.print(" ");
}
for (int n
= 1; n <= 1; n++) {
System.out.print(" ");
}
// membuat
persegi panjang
for (int o
= 2; o <= 2; o++) {
System.out.print("*");
for
(int p = 1; p <= 1; p++) {
System.out.print("*");
for (int q = 2; q <= 2; q++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
}
*/
String masukan
= "" ;
masukan =
JOptionPane.showInputDialog(null, "masukan angka");
int n =
Integer.parseInt(masukan);
for (int i =
1; i <= n; i++) {
// input
segitiga kosong pertama
for (int
k = 1; k <= i; k++) {
System.out.print(" ");
}
// n = 3 j=2x3=6;
j>2*1-1=1;
for (int j
= 2 * n; j > 2 * i - 1; j--) {
System.out.print("*");
}
// l=1
for (int l
= 1; l <= n; l++) {
System.out.print(" ");
}
for (int m
= -5; m <= n; m++) {
System.out.print("*");
}
for (int o = 1; o
<= i; o++) {
System.out.print(" ");
}
for (int o = 1; o
<= i; o++) {
System.out.print("*");
}
System.out.println("");
}
}
}
// pembuatan segitiga
/* for (int i = 0; i <=6; i++) {
for (int j = i; j >=1; j--) {
System.out.print(" ");
}
for (int l = 7- (i-1); l > 1;l--) {
System.out.print("*");
}
for (int m=6- (i-1); m>1; m--){
System.out.print("*");
}
System.out.println(" ");
}
for (int colom=1;colom<=4; colom++){
for (int c=2; c<3; c++) {
System.out.print("* * * "); // kotak
}
for (int d=0;d<1;d++) {
System.out.print(" ");
}/*
for (int d=1; d<=1; d++) {
System.out.print("*");
}
for (int d=0;d<1;d++) {
System.out.print(" ");
}
for (int d=1; d<=1; d++) {
System.out.print("*");
}
System.out.println(); */
*** ARAY
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul4;
/**
*
* @author anik nurul
*/
public class Tugas3a {
public static void
main( String[] args )
{
String names[] = {"Joko", "Bhekti",
"Hendi", "Prasekti"};
for( int i=0; i<names.length; i++ ){
if( !names[i].equals("Bhekti") )
{
System.out.print(" "+names[i]);
continue; // Akan dilanjutkan
}
}
//Perulangan Pertama
for( int i=0; i<names.length; i++ ){
}
//Perulangan Kedua
for( int i=0; i<names.length; i++ )
{
}
}
}
*** ARAY BREAK AND CONTINUE
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul4;
/**
*
* @author anik nurul
*/
public class Tugas3b {
public static void
main(String[] args) {
String names[] =
{"Joko", "Bhekti", "Hendi",
"Prasekti"};
for( int i=0;
i<names.length; i++ ){
if( !names[i].equals("Bhekti") ){
System.out.println(names[i]);
continue; // Akan
dilanjutkan
}
}
System.out.println("*****************************");
//Perulangan
Pertama akan berhenti ketika menemukan elemen Bhekti berada
for( int i=0; i<names.length; i++ ){
if( !names[i].equals("Bhekti") ){
System.out.println(names[i]);
break;
}
}
System.out.println("*****************************");
//Perulangan
Kedua akan berlanjut ketika menemukan elemen Bhekti berada
for( int i=0; i<names.length; i++ ){
if(
names[i].equals("Bhekti") ){
System.out.println(names[i]);
continue;
}
}
}
}
Comments
Post a Comment