Java 我的while和if构造有什么问题

Java 我的while和if构造有什么问题,java,Java,计划说明: 该计划包含一份奥运年及其发生地点的清单。假设要求用户输入一年,然后给出位置 我的任务: 我被要求做while循环,如果在里面构造,那么如果用户输入了错误的奥运会年份,它会告诉用户对不起,给定的年份没有举办奥运会 我的问题是: 无论给定的年份如何,该程序都会打印第一届奥运会的年份。我的while循环有什么问题?谢谢 代码如下: import java.util.* ; class Olympics { int olympic_year ; String ol

计划说明: 该计划包含一份奥运年及其发生地点的清单。假设要求用户输入一年,然后给出位置

我的任务: 我被要求做while循环,如果在里面构造,那么如果用户输入了错误的奥运会年份,它会告诉用户对不起,给定的年份没有举办奥运会

我的问题是: 无论给定的年份如何,该程序都会打印第一届奥运会的年份。我的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  ++  ;
         }
      }
   }
}
import java.util.*;
级别奥运会
{
国际奥林匹克年;
北京市;
美国;
公共奥运会(国际奥林匹克年、国际奥林匹克城市、国际奥林匹克国家)
{
奥运年=给定的奥运年;
奥运城市=给定的奥运城市;
奥林匹克国家=给定的奥林匹克国家;
}
公共int get_年()
{
回归奥运年;
}
公共作废打印数据()
{
系统输出打印(“\n In”+奥林匹克年+
,奥运会在“+奥林匹克城”举行+
“,”+奥运会国家+“\n”);
}
}
类数据查找器
{
公共静态void main(字符串[]未使用)
{
奥运会=
{
新奥运会(1896年,“雅典”、“希腊”),
新奥运会(1900年,“巴黎”、“法国”),
新奥运会(1904年,“圣路易斯”,“美国”),
新奥运会(1906年,“雅典”、“希腊”),
新奥运会(1908年,“伦敦”、“大不列颠”),
新奥运会(1912年,“斯德哥尔摩”、“瑞典”),
新奥运会(1920年,“安特卫普”、“比利时”),
新奥运会(1924年,“巴黎”、“法国”),
新奥运会(1928年,“阿姆斯特丹”、“荷兰”),
新奥运会(1932年,“洛杉矶”,“美国”),
新奥运会(1936年,“柏林”、“德国”),
新奥运会(1948年,“伦敦”、“英国”),
新奥运会(1952年,“赫尔辛基”、“芬兰”),
新奥运会(1956年,“墨尔本”、“澳大利亚”),
新奥运会(1960年,“罗马”、“意大利”),
新奥运会(1964年,“东京”、“日本”),
新奥运会(1968年,“墨西哥城”,“墨西哥”),
新奥运会(1972年,“慕尼黑”、“西德”),
新奥运会(1976年,“蒙特利尔”、“加拿大”),
新奥运会(1980年,“莫斯科”、“苏联”),
新奥运会(1984年,“洛杉矶”,“美国”),
新奥运会(1988年,“首尔”、“韩国”),
新奥运会(1992年,“巴塞罗那”、“西班牙”),
新奥运会(1996年,“亚特兰大”,“美国”),
新奥运会(2000年,“悉尼”、“澳大利亚”),
新奥运会(2004年,“雅典”、“希腊”),
新奥运会(2008,“北京”、“中国”),
新奥运会(2012年,“伦敦”、“英国”),
} ;
System.out.print(“\n此程序可以告诉您奥运会在哪里举行”
+“\n游戏是在给定的年份举行的。请给出”
+“\n一年,使用四位数字:”;
扫描仪键盘=新扫描仪(System.in);
给定的int_year=keyboard.nextInt();
国际奥林匹克联合会指数=0;
布尔表_search_ready=false;
while(表搜索准备就绪==false)
{
if(奥运会指数<奥运会表格长度)
{
奥运会表格[奥运会索引]。打印奥运会数据();
表_search_ready=true;
}
else if(奥运会索引>=奥运会表格长度)
{
System.out.print(“\n抱歉,没有在中举办奥运会”
+给定年份+“\n”);
表_search_ready=false;
}
其他的
{
奥运会指数++;
}
}
}
}
如果(奥运会索引
将始终为
true
,因为
奥运会索引在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" ) ;
boolean-found=false;
for(int i=0;i
我相信您在使用奥林匹克索引时检查了,而没有使用您扫描的给定年份。尝试检查给定年份的while循环

请使用此代码

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");
    }
}
}

不确定你的问题的具体原因,但有两件事很突出:1。您没有在任何地方使用给定年份的
,这是故意的吗?2.您可以将整个index+bool+loop mess提取到一个带有签名的函数中。
Olympic findOlympicByYear(int givenYear)
-您可以使用扩展的
for
和早期的
返回值
,而不必手动维护索引和标记。@millimoose这很可能是一个学习控制结构的学校作业,我猜他是被要求专门做这件事的。@millimoose是对的,我必须用while循环和if构造来做