java - if 语句打印出 'else' 部分,即使它是正确的

标签 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

最佳答案

对于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();
}
<小时/>

跌倒

The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

关于java - if 语句打印出 'else' 部分,即使它是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28720836/

相关文章:

java - 由于超时而安全取消线程

检查数组列表的两个列表是否相等的 Pythonic 方法

javascript - 数组到对象 : declaring object inside vs outside for loop. 不同的结果

java - 抓取嵌套在 JSoup 中多个元素中的图像源

java - 在 Java 中折叠顺序流

java - 如何从在不同类中运行的 TimerTask 更新 JavaFX 应用程序线程?

java - new int[]{} 与 {} 之间的区别

sql - 使用 IF/ELSE 语句向 SQL 中的现有表添加列

java - 我必须为一个程序编写两个方法,当用户提示宽度和高度时计算矩形的面积和周长?

java - IF 语句未执行