mercredi 22 avril 2015

Trouble with switch statements?

I am just trying to write a program that generates a random year between 2000 and 2010, then reads off a space exploration fact that occurred in that year. This is the code that I have written, however when I run it, no matter what year is generated, it just prints the last case (2010). How do I fix this?

import java.util.Random;

public class SpaceExploration {
public static void main(String[] args) {

int year =(int)(Math.random()*11) + 2000;
String eventString = "";

switch (year) {
    case 2000:  eventString = "2000: First spacecraft orbits an asteroid";
    case 2001:  eventString = "2001: First spacecraft lands on asteroid";
    case 2002:  eventString = "2002: N/A";
    case 2003:  eventString = "2003: Largest infrared telescope released";
    case 2004:  eventString = "2004: N/A";
    case 2005:  eventString = "2005: Spacecraft collies with comet";
    case 2006:  eventString = "2006: Spacecraft returns with collections from a comet";
    case 2007:  eventString = "2007: N/A";
    case 2008:  eventString = "2008: Kepler launched to study deep space";
    case 2009:  eventString = "2009: N/A";
    case 2010:  eventString = "2010: SpaceX sucessfully sends spacecraft to orbit and back";
    }
    System.out.println(eventString);
}
}




Aucun commentaire:

Enregistrer un commentaire