...

/

البرامج التي يمكن مقارنتها

البرامج التي يمكن مقارنتها

تعرف على كيفية اختيار أجزاء من التعليمات البرمجية لتنفيذها.

سنغطي ما يلي...

متطلبات المشروع

دعونا نتناول متطلبات عميلنا للمشروع مرة أخرى، هذه المرة مع التركيز على المتطلبات المميزة باللون الأخضر لتحديث النتيجة عندما يجيب المستخدم على سؤال الرياضيات بشكل صحيح.

Press + to interact
Project requirements that require selective execution
Project requirements that require selective execution

ماذا إذا

نعلم أننا سنحتاج فقط == للتحقق من صحة الإجابة. ماذا لو كان لدينا برنامج في Java يسمح لنا بتحديث النتيجة، ولكن فقط if userAnswer == correctAnswer ؟

Press + to interact
if (userAnswer == correctAnswer) {
score = score + 1;
}
else {
System.out.println("Unfortunately, your answer is not correct.");
}

الآن، يبدو هذا الكود واضحًا. الأمر سهل كما يبدو، إذ تتيح لنا Java استخدام if-else للتحكم في سير تنفيذ الكود.

Either the IF or ELSE block will work, but never both, based on the condition being true or false. Can you tell?

1

Which line number will not get executed when the value of userAnswer is equal to correctAnswer in the code above?

A)

Line 1

B)

Line 2

C)

Line 4

D)

Line 5

Question 1 of 20 attempted

if داخل مشروعنا

لنبدأ بكتابة الكود لتلبية متطلبات المشروع. لنقدم متغير score .

import java.util.Scanner;

public class MyJavaApp {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int score = 0;
        
        System.out.print("What is 4 * 7? ");
        int userAnswer = scan.nextInt();
        int correctAnswer = 4 * 7;
        System.out.println("Your score is " + score);
    }
}
Initializing a variable to store the score
...