اذهب إلى المحتوى
  • 0

مشكلة حلقة while تتوقف مبكرًا في PHP

Issmail Manha

السؤال


<html>
  <head>
    <title>PHP Functions</title>
  </head>
  <body>
<?php


function successfulCompaines($names_array, $rate)
{
    $new_names = [];
    $index = null;
    $min = null;
    foreach ($names_array as $i => $name)
    {
        $random_rate = rand(0,100);
        if ($min == null)
        {
            $index = 0;
            $min = $random_rate;
        }

        if ($random_rate <= $rate)
            array_push($new_names, $name);
        
        if ($random_rate < $min)
        {
            $index = $i;
            $min = $random_rate;
        }
    }
    return count($new_names) == 0 ? [$names_array[$index]]:$new_names;
}

$compaines = array("Lenovo","Huawei","Apple","Amazon","Microsoft","Abb","Netflex","Siemens","Samsung","Adidas","Uber","Dell","Hp","Walmart","Tesla","Google","Volvo","Toyota","Ibm","Shell");
$successRate = 0.5;
while(true)
{
    $compaines = successfulCompaines($compaines, $successRate);
    if (count($compaines) == 1)
    {
        echo 'Congrats for '. $compaines[0];
        break;
    }
    $successRate /=2;
}

وصلت لهده المرحلة اريد ان يطبع البرنامج على الاقل شركتين او اكثر لانه يطبع لي شركة واجدة فقط

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

المشكلة لديك هي تحديد قيمة صغيرة للغاية للمتغير successRateحيث أن القيمة 0.5 ستكون أصغر من أي قيمة للمتغير random_rate (ماعدا القيمة 0 بالطبع)، لذلك عليك أن تقوم بتغير قيمة المتغير successRate لتكون قيمة أكبر مثل 50 على سبيل المثال:

أيضًا عليك أن تقوم بنقل جملة طباعة الجملة Congrats for خارج جملة if لكي يتم طباعة الجملة في كل دورة وليس في آخر دورة فقط، بالشكل التالي:

<?php

function successfulCompaines($names_array, $rate)
{
    $new_names = [];
    $index = null;
    $min = null;
    foreach ($names_array as $i => $name) {
        $random_rate = rand(0, 100);
        if ($min == null) {
            $index = 0;
            $min = $random_rate;
        }

        if ($random_rate <= $rate) {
            array_push($new_names, $name);
        }

        if ($random_rate < $min) {
            $index = $i;
            $min = $random_rate;
        }
    }

    return count($new_names) == 0 ? [$names_array[$index]] : $new_names;
}

$compaines = array("Lenovo", "Huawei", "Apple", "Amazon", "Microsoft", "Abb", "Netflex", "Siemens", "Samsung", "Adidas", "Uber", "Dell", "Hp", "Walmart", "Tesla", "Google", "Volvo", "Toyota", "Ibm", "Shell");

// نرفع قيمة المتغير التالي
$successRate = 50;

while (true) {
    $compaines = successfulCompaines($compaines, $successRate);

    // ننقل جملة الطباعة إلى هنا
    echo 'Congrats for ' . $compaines[0];
    echo "<br>";

    if (count($compaines) == 1) {
        break;
    }
    $successRate /= 2;
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...