【问题标题】:If statement prints out the 'else' part even if it's correctif 语句打印出 'else' 部分,即使它是正确的
【发布时间】:2015-02-25 13:55:43
【问题描述】:

我设计了一些代码,它会询问用户 10 首歌曲标题(将存储在一个数组中),然后是这些歌曲的持续时间(也在一个数组中),最后他们可以从他们的歌曲中搜索和删除一首歌曲列表。

但是,当我运行搜索和删除部分时,程序会运行我的整个 if 语句。我有一个“else”,所以如果他们搜索不在列表中的歌曲,它会说“sorry”并让他们回到开头。但即使他们确实搜索了正确的歌曲标题,它也会继续运行。有人可以帮忙吗?

import java.util.Scanner;
/**
 * Created by IntelliJ IDEA.
 * Date: 25/02/2015
 * Time: 13:37
 * UPDATE COMMENT ABOUT PROGRAM HERE
 */
public class Songs
{
   static final int SIZE=10;
   static String songTitle [] = new String[SIZE];
   static Scanner keyboard = new Scanner(System.in);
   static double duration[]=new double[SIZE];
   static int choice;
   static String searchSong;

   public static void menu()
   {
      System.out.println("Please chose from the options below");
      System.out.println("1) Enter Song Titles **DO FIRST**");
      System.out.println("2) Enter duration of songs **DO SECOND**");
      System.out.println("3) Search and remove **DO THIRD**");
      choice=keyboard.nextInt();

      switch(choice)
      {

         case 1:
            setSongTitle();
            menu();

         case 2:
            duration();
            menu();

         case 3:
            searchRemove();

         default:System.out.println("Sorry try again");
            menu();

      }//switch
   }//menu

   public static void setSongTitle()
   {
      for(int count=0;count<SIZE;count++)
      {

         System.out.println("Please enter your song title number " + (count + 1) + " below..");
         songTitle[count]=keyboard.next();

      }//setSongTitle();

      System.out.println("Here are your songs");

      for(int count=0;count<SIZE;count++)
      {
         System.out.println(songTitle[count]);
      }//for



   }//setSongTitle

   public static void duration()
   {
      for(int count=0;count<SIZE;count++)
      {

         System.out.println("Please enter the duration of the song " +songTitle[count] + " below...");
         duration[count]=keyboard.nextDouble();

      }//for

      for(int count=0;count<SIZE;count++)
      {
         System.out.println(duration[count]);
      }//for

   }//duration

   public static void searchRemove()
   {

      System.out.println("Please enter a song title you would like to remove");
      searchSong=keyboard.next();

      for(int count=0;count<SIZE;count++)
      {




         if(searchSong.equals(songTitle[count]))
         {

            System.out.println("Song " +songTitle[count] + " is now removed from the list");
            System.out.println("The duration of " + duration[count] + " for song " +songTitle[count] + " is now removed from the list");
            duration[count]=0;
            songTitle[count]=null;

         }//if

         else
         {
            System.out.println("Sorry your search has not been found");
            searchRemove();
         }//else

      }//for


   }

   public static void main(String[] args)
   {
      menu();
   }//main
}//class

【问题讨论】:

    标签: java arrays if-statement


    【解决方案1】:

    对于switch中的每个case,你必须添加一个break;语句,否则会出现一种情况,称为fall-through

    switch(choice) {
        case 1:
           setSongTitle();
           menu();
           break;
        case 2:
           duration();
           menu();
           break;
        case 3:
           searchRemove();
           break;
        default:System.out.println("Sorry try again");
           menu();
    }
    

    跌倒

    break 语句是必要的,因为没有它们,switch 块中的语句将失败:匹配 case 标签之后的所有语句都按顺序执行,无论后续 case 标签的表达式如何,直到遇到 break 语句.

    【讨论】:

      【解决方案2】:

      您需要在下一个case 之前将breaks 添加到您的switch 语句中。示例:

      switch(choice)
        {
      
           case 1:
              setSongTitle();
              menu();
              break;
           case 2:
              duration();
              menu();
              break;
           case 3:
              searchRemove();
              break;
           default:System.out.println("Sorry try again");
              menu();
      
        }
      

      【讨论】:

        【解决方案3】:

        1)问题是找到匹配项时您没有中断。并且每次发现不匹配时 else 都会被执行

        这样改

        int flag=0;
        for(int count=0;count<SIZE;count++)
        {
        
             if(searchSong.equals(songTitle[count]))
             {
        
              System.out.println("Song " +songTitle[count] + " is now removed from the list");
              System.out.println("The duration of " + duration[count] + " for song " +songTitle[count] + " is now removed from the list");
              duration[count]=0;
              songTitle[count]=null;
        
                flag=1; //set a flag to denote that a match is found  
                break;
             }//if
        
        
        }//for
        
        if(flag==0)
          System.out.println("Sorry your search has not been found");
        

        2) 并且您必须在switch 中的每个case 之后添加break

        【讨论】:

          猜你喜欢
          • 2020-06-07
          • 1970-01-01
          • 1970-01-01
          • 2022-08-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-17
          • 1970-01-01
          相关资源
          最近更新 更多