家庭作业帮助(模拟程序)

时间:2014-10-01 10:16:27

标签: java

我正在介绍java编程类,但是我的第三个编程任务遇到了麻烦。起初我以为我是在正确的轨道上,因为我们本周刚刚开始讨论循环(而循环),但我想我真的很想知道我需要如何编写这个特定的赋值。现在我做了很多工作,但我只是坚持如何继续,当然我的程序有点工作现在它只是一团糟。任何帮助将不胜感激。顺便说一下,我并不是要求为我完成这项任务,我只需要帮助我如何向前迈进,因为我只是非常困惑,因为我无法完成我的最后一次循环,以便在迭代完循环之后继续。以下是我需要做的以及代码的总结:

据报道,一名外星人登陆月球。幸运的是,我们有一个航天器轨道运行 可以拍摄表面照片的月亮。在24小时内,航天器可以 绕月球轨道8次。在每个轨道上,外星人将在我们的相机范围内 宇宙飞船3分钟。相机每分钟可拍摄10张照片。在第一次 我们估计外星人每次拍照时有50%的机会出现。 在第二分钟,我们估计外星人有75%的机会出现 每张照片。在第三分钟,我们估计外星人有25%的几率 将出现在每张照片上。

运行模拟我们的太空船拍摄外星人的照片。对于拍摄的每张照片,生成0-100的随机数,并将其与外星人的百分比进行比较 将出现在照片中。计算24小时内拍摄的照片数量 并将其输出到屏幕。计算24小时内拍摄的照片数量 其中外星人出现在照片中并输出到屏幕上。

运行上述模拟5次。最后,计算在外星人出现的24小时内拍摄的照片的平均数量(向下舍入)(通过平均在所有5次模拟中拍摄的外星人拍摄的照片数量)

public class Homework_3 

{

public static void main(String[] args) 
{
    //These are the declarations for most of my variables
    int count_of_sims = 0;
    int hours = 0;
    int minutes = 0;
    int photo_alien = 0;
    int count_of_photos = 0;
    /*int sum = 0;
    int avg = 0;*/
    double firstOrbit = 0.0;
    double secondOrbit = 0.0;
    double thirdOrbit = 0.0;
    double fourthOrbit = 0.0;
    double fifthOrbit = 0.0;
    double sixthOrbit = 0.0;
    double seventhOrbit = 0.0;
    double eigthOrbit = 0.0;

    //This while loop performs all five simulations
    while(count_of_sims < 6)
    {
        count_of_sims = count_of_sims + 1;

        //This while loop determines which orbit of the moon our spacecraft is on
        while(hours < 24)
        {
            hours = hours + 3;
            //This is the first orbit of the moon by our spacecraft
            if(hours == 3)
            {       
                //This while loop determines which minute we are on in regard to taking photos of the alien
                while(minutes < 4)
                {
                    minutes = minutes + 1;
                    //This is the first minute 
                    if(minutes == 1 && count_of_photos <=10)
                    {
                        firstOrbit = (int)(Math.random() *100) * .50;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 1: Photos of alien: " + firstOrbit + " ,");
                    }
                    //This is the second minute
                    else if(minutes == 2 && count_of_photos <=10)
                    {
                        firstOrbit = (int)(Math.random() *100) * .75;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 1: Photos of alien: " + firstOrbit + " ,");
                    }
                    //This is the third minute
                    else if(minutes == 3 && count_of_photos <=10)
                    {
                        firstOrbit = (int)(Math.random() *100) * .25;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 1: Photos of alien: " + firstOrbit);
                    }
                    //break;
                    continue;
                }
            }
            //This is the second orbit of the moon by our spacecraft
            else if(hours == 6) 
            {
                //This while loop determines which minute we are on in regard to taking photos of the alien
                while(minutes < 4)
                {
                    minutes = minutes + 1;
                    //This is the first minute
                    if(minutes == 1 && count_of_photos <=10)
                    {
                        secondOrbit = (int)(Math.random() *100) * .50;
                        photo_alien++;
                        count_of_photos++;
                        System.out.println("Simulation 2: Photos of alien: " + secondOrbit + " ,");
                    }
                    //This is the second minute
                    else if(minutes == 2 && count_of_photos <=10)
                    {
                        secondOrbit = (int)(Math.random() *100) * .75;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 2: Photos of alien: " + secondOrbit + " ,");
                    }
                    //This is the third minute
                    else if(minutes == 3 && count_of_photos <=10)
                    {
                        secondOrbit = (int)(Math.random() *100) * .25;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 2: Photos of alien: " + secondOrbit);
                    }
                    //break;
                    continue;
                }
            }
            //This is the third orbit of the moon by our spacecraft
            else if(hours == 9) 
            {
                //This while loop determines which minute we are on in regard to taking photos of the alien
                while(minutes < 4)
                {
                    minutes = minutes + 1;
                    //This is the first minute
                    if(minutes == 1 && count_of_photos <=10)
                    {
                        thirdOrbit = (int)(Math.random() *100) * .50;
                        photo_alien++;
                        count_of_photos++;
                        System.out.println("Simulation 3: Photos of alien: " + thirdOrbit + " ,");
                    }
                    //This is the second minute
                    else if(minutes == 2 && count_of_photos <=10)
                    {
                        thirdOrbit = (int)(Math.random() *100) * .75;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 3: Photos of alien: " + thirdOrbit + " ,");
                    }
                    //This is the third minute
                    else if(minutes == 3 && count_of_photos <=10)
                    {
                        thirdOrbit = (int)(Math.random() *100) * .25;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 3: Photos of alien: " + thirdOrbit);
                    }
                    //break;
                    continue;
                }
            }
            //This is the fourth orbit of the moon by our spacecraft
            else if(hours == 12) 
            {
                //This while loop determines which minute we are on in regard to taking photos of the alien
                while(minutes < 4)
                {
                    minutes = minutes + 1;
                    //This is the first minute
                    if(minutes == 1 && count_of_photos <=10)
                    {
                        fourthOrbit = (int)(Math.random() *100) * .50;
                        photo_alien++;
                        count_of_photos++;
                        System.out.println("Simulation 4: Photos of alien: " + fourthOrbit + " ,");
                    }
                    //This is the second minute
                    else if(minutes == 2 && count_of_photos <=10)
                    {
                        fourthOrbit = (int)(Math.random() *100) * .75;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 4: Photos of alien: " + fourthOrbit + " ,");
                    }
                    //This is the third minute
                    else if(minutes == 3 && count_of_photos <=10)
                    {
                        fourthOrbit = (int)(Math.random() *100) * .25;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 4: Photos of alien: " + fourthOrbit + " ,");
                    }
                    //break;
                    continue;
                }
            }
            //This is the fifth orbit of the moon by our spacecraft
            else if(hours == 15) 
            {
                //This while loop determines which minute we are on in regard to taking photos of the alien
                while(minutes < 4)
                {
                    minutes = minutes + 1;
                    //This is the first minute
                    if(minutes == 1 && count_of_photos <=10)
                    {
                        fifthOrbit = (int)(Math.random() *100) * .50;
                        photo_alien++;
                        count_of_photos++;
                        System.out.println("Simulation 5: Photos of alien: " + fifthOrbit + " ,");
                    }
                    //This is the second minute
                    else if(minutes == 2 && count_of_photos <=10)
                    {
                        fifthOrbit = (int)(Math.random() *100) * .75;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 5: Photos of alien: " + fifthOrbit + " ,");
                    }
                    //This is the third minute
                    else if(minutes == 3 && count_of_photos <=10)
                    {
                        fifthOrbit = (int)(Math.random() *100) * .25;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 5: Photos of alien: " + fifthOrbit);
                    }
                    //break;
                    continue;
                }
            }
            //This is the sixth orbit of the moon by our spacecraft
            else if(hours == 18) 
            {
                //This while loop determines which minute we are on in regard to taking photos of the alien
                while(minutes < 4)
                {
                    minutes = minutes + 1;
                    //This is the first minute
                    if(minutes == 1 && count_of_photos <=10)
                    {
                        sixthOrbit = (int)(Math.random() *100) * .50;
                        photo_alien++;
                        count_of_photos++;
                        System.out.println("Simulation 6: Photos of alien: " + sixthOrbit + " ,");
                    }
                    //This is the second minute
                    else if(minutes == 2 && count_of_photos <=10)
                    {
                        sixthOrbit = (int)(Math.random() *100) * .75;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 6: Photos of alien: " + sixthOrbit + " ,");
                    }
                    //This is the third minute
                    else if(minutes == 3 && count_of_photos <=10)
                    {
                        sixthOrbit = (int)(Math.random() *100) * .25;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 6: Photos of alien: " + sixthOrbit);
                    }
                    //break;
                    continue;
                }
            }
            //This is the seventh orbit of the moon by our spacecraft
            else if(hours == 21) 
            {
                //This while loop determines which minute we are on in regard to taking photos of the alien
                while(minutes < 4)
                {
                    minutes = minutes + 1;
                    //This is the first minute
                    if(minutes == 1 && count_of_photos <=10)
                    {
                        seventhOrbit = (int)(Math.random() *100) * .50;
                        photo_alien++;
                        count_of_photos++;
                        System.out.println("Simulation 7: Photos of alien: " + seventhOrbit + " ,");
                    }
                    //This is the second minute
                    else if(minutes == 2 && count_of_photos <=10)
                    {
                        seventhOrbit = (int)(Math.random() *100) * .75;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 7: Photos of alien: " + seventhOrbit + " ,");
                    }
                    //This is the third minute
                    else if(minutes == 3 && count_of_photos <=10)
                    {
                        seventhOrbit = (int)(Math.random() *100) * .25;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 7: Photos of alien: " + seventhOrbit);
                    }
                    //break;
                    continue;
                }
            }
            //This is the eighth orbit of the moon by our spacecraft
            else if(hours == 24) 
            {
                //This while loop determines which minute we are on in regard to taking photos of the alien
                while(minutes < 4)
                {
                    minutes = minutes + 1;
                    //This is the first minute
                    if(minutes == 1 && count_of_photos <=10)
                    {
                        eigthOrbit = (int)(Math.random() *100) * .50;
                        photo_alien++;
                        count_of_photos++;
                        System.out.println("Simulation 8: Photos of alien: " + eigthOrbit + " ,");
                    }
                    //This is the second minute
                    else if(minutes == 2 && count_of_photos <=10)
                    {
                        eigthOrbit = (int)(Math.random() *100) * .75;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 8: Photos of alien: " + eigthOrbit + " ,");
                    }
                    //This is the third minute
                    else if(minutes == 3 && count_of_photos <=10)
                    {
                        eigthOrbit = (int)(Math.random() *100) * .25;
                        photo_alien++;
                        count_of_photos++;
                        System.out.print("Simulation 8: Photos of alien: " + eigthOrbit);
                    }
                    //break;
                    continue;
                }
            }
        }
    }
}

}

2 个答案:

答案 0 :(得分:0)

是否需要以分钟/小时等方式工作?

如果不是,可以将其抽象为更简单的格式。

取而代之的是

while(orbitCount < 8) {
    calcPictures(50)
    calcPictures(75)
    calcPictures(25)
    orbitCount++
}

这应该会显着减少您拥有的嵌套循环的数量。逻辑将需要为calcPictures方法实现,您可以在其中传递百分比并计算成功图片的数量。

答案 1 :(得分:0)

  1. 您使用.50代表第1名,第2名和第2名第3分钟。将其改为第2分钟至.75&amp;为第3 .25。
  2. 使用while循环代替if else ladder几个小时
  3. 使用double数组作为轨道。

    double hour = 3;
    while(hour<=24)
    {
     hour = hour + 3;
     if(minutes == 1)
                {
                    firstOrbit = (int)(Math.random() *100) * .50;
                    System.out.print("Simulation "+hour/3+": Photos of alien: " + firstOrbit);
                }
                //This is the second minute
                else if(minutes == 2)
                {<br>
                    firstOrbit = (int)(Math.random() *100) * .75;
                    System.out.print("Simulation "+hour/3+": Photos of alien: " + firstOrbit);
                }
                //This is the third minute
                else if(minutes == 3)
                {
                    firstOrbit = (int)(Math.random() *100) * .25;
                    System.out.print("Simulation "+hour/3+": Photos of alien: " + firstOrbit);
                }
      minutes=0;
    }