Di greenfoot, huruf apakah yang umum digunakan untuk variabel loop?

1 PERULANGAN (LOOP) PERNYATAAN WHILE Pernyataan while merupakan salah satu pernyataan yang berguna untuk memproses suatu pernyataan atau beberapa pernyataan beberapa kali. Bentuk penulisan : while (ungkapan) pernyataan; Bagian pernyataan yang mengikuti while akan dieksekusi selama ungkapan bernilai benar. Perlu diketahui, pengujian terhadap ungkapan pada while dilakukan sebelum bagian pernyataan. Oleh karena itu ada kemungkinan bagian pernyataan pada while tidak dijalankan sama sekali, yaitu jika kondisi yang pertama kali bernilai salah. int i=1; while (i<=5) cout <<"Putaran ke-" <<i <<endl; i++; Pemakaian while dapat digunakan untuk mengatur agar pemakai menekan tombol pilihan yang absah. char kode; cout <<"Pilih salah satu kode [a, b, c]?"; kode=getch(); while (!((kode =='a') (kode=='b') (kode=='c'))) kode=getch(); cout <<"\npilihan Anda : " <<kode;

2 Pernyataan while juga dapat digunakan untuk menangani pemasukan data, menjumlahkannya dan mencari rata-rata. int i=0; float nilai, total, rata=0; cout <<"Mencari Nilai Total dan Rata-rata" <<endl; cout <<"Masukkan nol untuk keluar..." <<endl; while (!(nilai == 0)) i++; cout <<"Nilai ke-" <<i <<" = "; cin >>nilai; total+=nilai; cout <<"Jumlah total nilai = " <<total <<endl; cout <<"Rata-rata = " <<(total/(i-1)); PERNYATAAN DO WHILE Pernyataan do while juga berguna untuk mengulang proses dan akan dijalankan minimal satu kali. Bentuk penulisan : do pernyataan; while (ungkapan) Bagian pernyataan akan dijalankan secara berulang sampai ungkapan bernilai salah dan pengujian ungkapan akan dilakukan di belakang setelah pernyataan.

3 int x,y, maks; char lagi; do cout <<"Masukkan nilai-1 : "; cin >>x; cout <<"Masukkan nilai-2 : "; cin >>y; maks=(x<y)? x : y; cout <<"Nilai terkecil adalah : " <<maks; cout <<"\n\nulang lagi [Y/T]? "; cin >>lagi; while ((lagi=='y') (lagi=='y')); PERNYATAAN FOR Pernyataan for juga berguna untuk mengulang pengeksekusian terhadap satu atau sejumlah pernyataan. Bentuk penulisan : for (ungkapan1; ungkapan2; ungkapan3) pernyataan; Dimana : Ungkapan1 merupakan pernyataan inisialisasi sebelum masuk ke for Ungkapan2 sebagai kondisi yang menentukan pengulangan terhadap pernyataan Ungkapan3 digunakan sebagai pengatur variabel yang digunakan dalam ungkapan1 char huruf; for(huruf='a'; huruf < 'Z'; huruf++) cout <<huruf <<" "; Pernyataan for juga dapat digunakan untuk pengendalian isi variabel yang menurun.

4 int x; for(x=20; x>=1; x--) cout <<x <<" "; VARIASI FOR Menghilangkan sebuah bagian ungkapan char huruf; cout <<"Ketikkan karakter-karakter (0 = stop) : "; for (huruf=' '; huruf!='0'; ) huruf=getche(); Loop tak hingga Loop tak hingga dibuat dengan menghilangkan bagian ungkapan. Program tersebut meminta Anda mengetikkan sebuah huruf dan tidak akan berhenti. Untuk menghentikannya tekan tombol CTRL + BREAK char huruf; cout <<"Ketikkan karakter-karakter (CTRL+BREAK = stop) : "; for ( ; ; ) huruf=getche(); FOR BERSARANG Pada aplikasi tertentu, terkadang kita menggunakan pernyataan for yang juga berada di dalam pernyataan for.

5 int i,j; for (i=1 ; i <= 3 ; i++) for (j=1 ; j <= 5 ; j++ ) cout <<i; cout <<"\n"; getch(); Dari contoh tersebut dapat kita simpulkan bahwa variabel i menyatakan baris dan variabel j menyatakan kolom. PERNYATAAN BREAK Pernyataan break digunakan untuk memaksa keluar dari loop int i; for (i=1; i<=25; i++) cout <<i <<" "; if (i == 15) break; cout <<"\nselesai...!"; PERNYATAAN CONTINUE Digunakan untuk menuju ke iterasi (putaran) berikutnya pada pernyataan yang terkait dengan perulangan int i; for (i=1; i<=15; i++)

6 if (i >=5 && i <=10) continue; cout <<i <<" "; Tampak bahwa angka 5 sampai 10 tidak ditampilkan disebabkan oleh perintah CONTINUE TUGAS 1. Buat program untuk menghasilkan deret bilangan Genap dan Ganjil antara 0 sampai dengan 50 2. Buat program untuk membuat tabel suhu CELCIUS, FAHRENHEIT dan REAMUR dengan menggunakan perintah DO-WHILE Ketentuan : Nilai Celcius 100 menurun ke 0 Fahrenheit = 1,8 x Celcius + 32 Reamur = 0,8 x Celcius 3. Hitunglah nilai faktorial sebuah bilangan bulat positif, bila rumus untuk faktorial adalah : n (n!) = n*(n-1). 3! = 3*2*1 = 6 4. Buat program untuk menampilkan : * ** *** ***** 5. Buat program untuk menampilkan keluaran sebagai berikut : 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64

Greetings people, Is it possible if one of you post a sample of a for loop for me please? Any help would be appreciated! Thank you for your time! :)

You can find examples here.

Thanks! That helped! But I have one more question. Uh, can someone post the coding for making an actor alternate pictures in a loop? I tried, but I think I left a few things out. Oops! Thnx!

You need to post the code that you tried.

Here's the code...

class ForDemo { public void act(String[ ] args) { for(int i=1; i<100; i++){ if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } } }

I thought of replacing the "String" and the "args" with something else, but when I compile it, it says it's an illegal start of expression. When I don't replace it, it compiles just fine but nothing happens.

I don't know what's wrong and why it's not working.

You should probably show the entire class code (preferably, the code that compiles). Lines 1 and 2 seem out of sorts for code belonging to an Actor subclass. The for loop looks to be properly created (the loop will execute 99 times, however); but, the act method of neither the World or Actor class use a parameter. The loop will run in its entirety within one act cycle; so visually, if any change occurs, it will be seen as one single change. It really depends on what this animation is doing; that is, when and how long it is to execute. The speed is something else that needs considered.

The entire class code:

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * IT RESEMBLES TO... CHEESE. * * @author (YOU EAT CHEESE) * @version (YOU EAT MORE CHEESE) */ public class Happyface extends Actor { private GreenfootImage image1; private GreenfootImage image2; /** * The happy face changes from a happy face to a sad face when * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { for(int i=1; i<100; i++){ if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } } }

I removed the

class ForDemo { public void act(String[ ] args) {

part. It compiled, but when I pressed the run button, my object disappeared.

Still, you have not yet assigned any GreenfootImage objects to the 'image1' and 'image2' fields. Also, the 'for' loop needs replaced with an 'if' condition that is controlled by the value of an 'int' field (the counter).

Um, I'm a terrible programmer. Is it possible if you can explain or maybe give a few examples? (Right now, I'm panicking because my project is due at the end of tomorrow and I'm not even halfway through!)

First: You have declared two image variables:

private GreenfootImage image1; private GreenfootImage image2;

But, you have never assigned anything to them (image1 = ?; image2 = ?), so while the variables do exist, they don't hold a reference to any actual image. Second: You alternate between the two images without any delay. The images will change so fast that you will not see it. You need to insert a delay.

(Right now, I'm panicking because my project is due at the end of tomorrow and I'm not even halfway through!)

It sounds like you have left your task too late. You need to understand that this is not anyone's fault but your own. Please do not transfer this time pressure on people who are helping you from goodwill.

danpost:

Also, the 'for' loop needs replaced with an 'if' condition that is controlled by the value of an 'int' field (the counter).

This isn't strictly correct. The technique of checking which image is currently active and then switching to the other one should be fine (it might not work correctly on the first iteration, but that could be remedied by setting the image to image1 before the loop starts).

It is not the first iteration that was my concern -- but the following iterations. The end result of the 'act' method will be one of two states. Any odd number of iterations will change the image and any even number will keep the image the same. There are only two results. One can be reached by not doing anything and the other can be reached by one iteration (which can be done without a loop).

Oh, I think the task requires actually using a loop: I'm assuming this is a set task which states that a loop should be used. As such the loop above is ok, but a delay needs to be inserted so that the change in the image is visible. But yes, if you wanted the image to alternate as the scenario was running (i.e. while the other actors were also acting) then you would not use a loop.

I changed my coding to this

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * IT RESEMBLES TO... CHEESE. * * @author (YOU EAT CHEESE) * @version (YOU EAT MORE CHEESE) */ public class Happyface extends Actor { private GreenfootImage smiley1 = new GreenfootImage("smiley1.png"); private GreenfootImage smiley3 = new GreenfootImage("smiley3.png"); private boolean image1 = false; /** * The happy face changes from a happy face to a sad face when * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { change(); } public void change() { for(int i=1; i<101; i++){ if (image1) { setImage( "smiley1.png" ); } else { setImage( "smiley3.png" ); } } } }

it compiled and when I ran it, it did change into a frowning face, except it didn't repeat.

There are more replies on the next page.

Video yang berhubungan

Postingan terbaru

LIHAT SEMUA