我的System.out.println语句有什么问题

时间:2014-12-09 18:36:20

标签: java eclipse arraylist

我的菜单系统有错误,这是我的两个班级; 音乐课(没有错误):

public class music {

  private String title;
  private String artist;
  private int Length;

  public music(String title, String artist, int Length){
   this.artist = artist;
   this.title = title;
   this.Length = Length;
  }

  public String gettitle(){
   return title;
  }

  public String getartist(){
   return artist;
  }

  public int getLength(){
   return Length;
  }
}

音乐管理课程:

 import java.util.Scanner;
 import java.util.ArrayList;

 public class musicmanager {

   Scanner in = new Scanner(System.in);
   Scanner text = new Scanner(System.in).useDelimiter(" ");
   int option =0;

   ArrayList<music>mymusic = new ArrayList<music>();

   System.out.println("Welcome to your music libary");
   System.out.println("");
   System.out.println("1. Add a new song");
   System.out.println("2. Edit a song");
   System.out.println("3.Remove a song");
   System.out.println("4. shuffle your songs")
   System.out.println("5. Exit");
   do{
     System.out.println("please make a choice");
     option = in.nextInt();

     switch(option){
      }

    case 1:


   }
}

我的菜单系统出错了。 out和println之间,括号中的文字和;在每个系统的最后一行代码。 eclipse告诉我这是一个语法错误,但在我做过的其他项目上没有错误。

2 个答案:

答案 0 :(得分:2)

您的代码中出现了很多错误。如果您正在执行除赋值之外的任何操作(包括变量声明或方法),则代码必须包含在可用性方法中。

您的所有System.out.println()和循环都必须位于方法中,例如main()方法。一个例子:

public static void main(String[] args) {
    System.out.println("Welcome to your music libary");
    System.out.println("");
    System.out.println("1. Add a new song");
    System.out.println("2. Edit a song");
    System.out.println("3.Remove a song");
    System.out.println("4. shuffle your songs")
    System.out.println("5. Exit");
}

do-while loop始终包含while()条件。

do {
    //do something that's looped
} while(option != 5);

switch-statement总是将其案例放在其正文中:

switch (option) {
    case 1:
       //do something
}

在这里你缺少终止分号:

System.out.println("4. shuffle your songs")

这应该可以解决你的大部分问题。我想建议遵循Java编程教程。

答案 1 :(得分:0)

格式化代码后,do / switch语句中的语法根本没有意义。不用时,外面开关的情况......

您的代码如下:

 do{
 System.out.println("please make a choice");
 option = in.nextInt();

 switch(option){
  }

case 1:

}

语法应该是:

 do {
     //do something...
    switch(option) {
        case 1: 
            //do something
        break;
    }
 } 
 while (expression);
相关问题