Cara Membuat Program loop_for ,do_while dan loop_while di pemograman java (eclipse)
Saya akan share macam-macam program looping di java yaitu :
Program loop_for , do_while dan loop_while di java (eclipse)
- anda harus membuat package menu_looping
- membuat 2 Class di dalam package menu_looping yaitu : Class index , Class proses
Script program Loop_for
ini adalah script Class index :
package menu_looping;
public class index {/**
* @param args
*/
public static void main(String[] args) {
for(int i = 0;i<6;i++){
System.out.println(“loop ke-“+(i+1));
}
}
}
public class index {/**
* @param args
*/
public static void main(String[] args) {
for(int i = 0;i<6;i++){
System.out.println(“loop ke-“+(i+1));
}
}
}
ini adalah script Class proses :
package menu_looping;public class proses {
public int hasil = 0;public void getInput(int nilai){
hasil = hasil + nilai;
}
public int setHasil(){
return hasil;
}
}
public int hasil = 0;public void getInput(int nilai){
hasil = hasil + nilai;
}
public int setHasil(){
return hasil;
}
}
hasil program
program loop_while
- anda harus membuat package loop_while
- membuat 1 Class di dalam package loop_while yaitu : Class index
script program loop_while
Class index :
package loop_while;
import java.util.Scanner;
public class index {private static Scanner scanner;/**
* @param args
*/
public static void main(String[] args) {
int x=0,nn;
System.out.println(“ulang berapa kali : “);
scanner = new Scanner(System.in);
nn = scanner.nextInt();while(x<nn)
{
System.out.println(x);
x = x+1;
}
}}
import java.util.Scanner;
public class index {private static Scanner scanner;/**
* @param args
*/
public static void main(String[] args) {
int x=0,nn;
System.out.println(“ulang berapa kali : “);
scanner = new Scanner(System.in);
nn = scanner.nextInt();while(x<nn)
{
System.out.println(x);
x = x+1;
}
}}
Hasil program nya
program loop_do_while
- anda harus membuat package loop_do_while
- membuat 1 Class di dalam package loop_do_while yaitu : Class index
script program loop_do_while
Class index :
package loop_do_while;
import java.util.Scanner;public class index {private static Scanner x;/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String tanya;
boolean status=true;
x = new Scanner(System.in);tanya= “”;
do{
System.out.print(“ulang tekan (y) tidak tekan (n) : y”);
tanya = x.nextLine();
import java.util.Scanner;public class index {private static Scanner x;/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String tanya;
boolean status=true;
x = new Scanner(System.in);tanya= “”;
do{
System.out.print(“ulang tekan (y) tidak tekan (n) : y”);
tanya = x.nextLine();
if(tanya.equalsIgnoreCase(“y”) || tanya.equalsIgnoreCase(“Y”)){
status = true;
}else{
status = false;
}
}while(status == true);
}
}
Hasil program nya