1.1 Selection
1.2 Repitition
2. Code the following flowchart (flowchart 1) in Java Program.
Flowchart 1 |
* the following java code can be used to generate random number in range of 0 to 100:-
int X = (int)(Math.random() * 101);
3. Consider the following flowchart. What is the difference ?
Flowchart 2 |
4. Code the following flowchart (flowchart 2) in Java Program.
import java.util.Scanner; public class guessNumber2 { public static void main(String[] args) { // Generate a random number to be guessed int X = (int)(Math.random() * 101); int jumlah_tekaan=0; Scanner input = new Scanner(System.in); System.out.println("Sila teka satu nombor antara 0 hingga 100"); int teka = -1; while (teka != X) { // Prompt the user to guess the number System.out.print("\nMasukkan Nombor anda: "); teka = input.nextInt(); jumlah_tekaan++; if (teka == X) System.out.println("Tahniah! Tekaan anda tepat--> " + teka); if (teka > X) System.out.println("Tekaan Anda Terlalu Besar"); if (teka < X) System.out.println("Tekaan Anda Terlalu Rendah"); } // End of loop System.out.println("Jumlah Tekaan Anda ialah :" +jumlah_tekaan); } }
No comments:
Post a Comment