如果语句打印出'else'部分,即使它是正确的

时间:2015-02-25 13:55:43

标签: java arrays if-statement

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

但是当我运行搜索和删除部分时,程序运行我的整个if语句。我有一个'否则'因此,如果他们搜索列表中没有的歌曲,它会说“抱歉”。并将它们归还给它们。但它继续运行,即使他们确实搜索正确的歌曲标题。有人可以帮忙吗?

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

3 个答案:

答案 0 :(得分:4)

对于case中的每个switch,您必须添加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标签的表达式,直到a遇到break语句。

答案 1 :(得分:1)

您需要在下一个breaks之前将case添加到您的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();

  }

答案 2 :(得分:0)

1)问题是找到匹配时你没有破坏。每次发现不匹配时,都会执行其他操作

像这样改变

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)您必须在break

中的每个case之后添加switch