-2

项目描述:项目包含奥运年列表及其举办地。假设要求用户输入年份,然后给他们位置。

我的任务:我被要求制作 while 循环和 if 内部构造,如果用户输入了错误的奥林匹克年份,它会告诉用户对不起,在给定的年份没有举行奥运会。

我的问题:无论给定的年份如何,程序都会打印第一届奥运会的年份。我的 while 循环有什么问题?谢谢

这是代码:

import java.util.* ;

class  Olympics
{
   int     olympic_year  ;
   String  olympic_city ;
   String  olympic_country ;

   public Olympics( int given_olympic_year,  String given_olympic_city,  String given_olympic_country )
   {
      olympic_year    =  given_olympic_year ;
      olympic_city    =  given_olympic_city ;
      olympic_country =  given_olympic_country ;
   }

   public int get_year()
   {
      return  olympic_year ;
   }

   public void print_olympics_data()
   {
      System.out.print( "\n    In "  +  olympic_year  +
              ", Olympic Games were held in " +  olympic_city  +
              ", "  +  olympic_country  +  ".\n" ) ;
   }
}

class  OlympicsDataFinder
{
   public static void main( String[] not_in_use )
   {
      Olympics[]  olympics_table  =
      {
         new Olympics( 1896, "Athens",   "Greece" ),
         new Olympics( 1900, "Paris",    "France" ),
         new Olympics( 1904, "St. Louis", "U.S.A." ),
         new Olympics( 1906, "Athens",   "Greece"  ), 
         new Olympics( 1908, "London",   "Great Britain" ),
         new Olympics( 1912, "Stockholm","Sweden" ),
         new Olympics( 1920, "Antwerp",  "Belgium"   ),
         new Olympics( 1924, "Paris",    "France"    ),
         new Olympics( 1928, "Amsterdam","Netherlands"),
         new Olympics( 1932, "Los Angeles", "U.S.A."),
         new Olympics( 1936, "Berlin",  "Germany"   ),
         new Olympics( 1948, "London",  "Great Britain" ),
         new Olympics( 1952, "Helsinki","Finland"  ),
         new Olympics( 1956, "Melbourne","Australia" ),
         new Olympics( 1960, "Rome",     "Italy"   ),
         new Olympics( 1964, "Tokyo",    "Japan"   ),
         new Olympics( 1968, "Mexico City","Mexico" ),
         new Olympics( 1972, "Munich",   "West Germany" ),
         new Olympics( 1976, "Montreal", "Canada"  ),
         new Olympics( 1980, "Moscow",   "Soviet Union" ),
         new Olympics( 1984, "Los Angeles","U.S.A."),
         new Olympics( 1988, "Seoul",    "South Korea"),
         new Olympics( 1992, "Barcelona","Spain"   ),
         new Olympics( 1996, "Atlanta",  "U.S.A." ),
         new Olympics( 2000, "Sydney",   "Australia" ),
         new Olympics( 2004, "Athens",   "Greece"  ),
         new Olympics( 2008, "Beijing",  "China"   ),
         new Olympics( 2012, "London",   "Great Britain" ),

      } ;


      System.out.print("\n This program can tell where the Olympic "
                     + "\n Games were held in a given year. Give "
                     + "\n a year by using four digits: "  ) ;

      Scanner  keyboard  =  new Scanner( System.in ) ;
      int  given_year  =  keyboard.nextInt() ;

      int  olympics_index  =  0 ;

      boolean table_search_ready  =  false ;

      while ( table_search_ready  ==  false )
      {
         if ( olympics_index < olympics_table.length )
         {
            olympics_table[ olympics_index ].print_olympics_data() ;

            table_search_ready  =  true ;
         }
         else if ( olympics_index >= olympics_table.length )
         {
            System.out.print( "\n    Sorry, no Olympic Games were held in "
                           +  given_year  + ".\n" ) ;

            table_search_ready  =  false ;
         }
         else
         {
            olympics_index  ++  ;
         }
      }
   }
}
4

3 回答 3

2

if (olympics_index < olympics_table.length)将始终是true因为olympics_index在您的 while 循环开始时等于 0。

您可以使用 for 循环简化程序:

boolean found = false;
for(int i = 0; i < olympics_table.length; i++){
    if(olympics_table[i].get_year() == given_year){
        olympics_table[i].print_olympics_data();
        found = true;
        break; //break the for loop because it has been founded so no need to iterate through the array
    }
}      
if(!found)
    System.out.print( "\n    Sorry, no Olympic Games were held in "+given_year+ ".\n" ) ;
于 2013-11-13T18:24:47.660 回答
0

我相信您正在使用 olympic_index 进行检查,而不是使用您扫描的给定年份。尝试使用 given_year 检查您的 while 循环

于 2013-11-13T18:26:25.953 回答
0

请使用此代码...

public class OlympicsDataFinder {
   public static void main(String[] not_in_use) {
    Olympics[] olympics_table = {
        new Olympics(1896, "Athens", "Greece"),
        new Olympics(1900, "Paris", "France"),
        new Olympics(1904, "St. Louis", "U.S.A."),
        new Olympics(1906, "Athens", "Greece"),
        new Olympics(1908, "London", "Great Britain"),
        new Olympics(1912, "Stockholm", "Sweden"),
        new Olympics(1920, "Antwerp", "Belgium"),
        new Olympics(1924, "Paris", "France"),
        new Olympics(1928, "Amsterdam", "Netherlands"),
        new Olympics(1932, "Los Angeles", "U.S.A."),
        new Olympics(1936, "Berlin", "Germany"),
        new Olympics(1948, "London", "Great Britain"),
        new Olympics(1952, "Helsinki", "Finland"),
        new Olympics(1956, "Melbourne", "Australia"),
        new Olympics(1960, "Rome", "Italy"),
        new Olympics(1964, "Tokyo", "Japan"),
        new Olympics(1968, "Mexico City", "Mexico"),
        new Olympics(1972, "Munich", "West Germany"),
        new Olympics(1976, "Montreal", "Canada"),
        new Olympics(1980, "Moscow", "Soviet Union"),
        new Olympics(1984, "Los Angeles", "U.S.A."),
        new Olympics(1988, "Seoul", "South Korea"),
        new Olympics(1992, "Barcelona", "Spain"),
        new Olympics(1996, "Atlanta", "U.S.A."),
        new Olympics(2000, "Sydney", "Australia"),
        new Olympics(2004, "Athens", "Greece"),
        new Olympics(2008, "Beijing", "China"),
        new Olympics(2012, "London", "Great Britain"),};


    System.out.print("\n This program can tell where the Olympic "
            + "\n Games were held in a given year. Give "
            + "\n a year by using four digits: ");

    Scanner keyboard = new Scanner(System.in);
    int given_year = keyboard.nextInt();

    int olympics_index = 0;

    boolean foundrecord = false;

    while (true) {
        if (olympics_index >= olympics_table.length) {
            break;
        }
        if (olympics_table[olympics_index].get_year() == given_year) {
            olympics_table[ olympics_index].print_olympics_data();
            foundrecord=true;
            break;
        }
        olympics_index++;
    }

    if (!foundrecord) {
        System.out.print("\n Sorry, no Olympic Game was held in "
                + given_year + ".\n");
    }
}
}
于 2013-11-13T18:37:02.687 回答