为什么我的阵列返回比特率 0,而其他地方都正常?

Why is my array returning 0 for BitRate, and ok everywhere else?

我试图在我的 musicLibrary 数组中显示 10 objects。每个 object 都有自己的艺术家姓名、歌曲名称、比特率和歌曲的总时间。

我的 运行 除了 "BitRate = 0k" 部分以外其他地方都很好。我的 运行 为阵列显示下的 10 个 object 中的每一个提供 BitRate = 0k。我希望每个 object 的比特率是 {250, 150, 350, 450, 550, 200, 100, 400, 300, 500}。 这些值都在我在 class iTunes 中设置的范围内。无论如何,如果这些值不在设定范围内,我应该会收到 "Bad _ _ input" 的输出。但是,我得到的是“0”。

我的代码中有两个独立的部分。 第一部分由四个不在数组中的 object 组成。对于这四个,我显示它们,改变它们,显示它们,设置为默认值,然后显示。我的 运行 在这里很好。 (我不想删除这部分以防混淆)。

第二部分是我的十个 object 数组。这部分我只需要显示数组中十个object的数据即可。我的问题就在这里,当我显示我的数组并且我的比特率数据显示为 0 时。 对于这两个部分,我正在检查值是否在我设置的范围内。

次要注意,为了清洁起见,我可以删除我的其他人(在我的代码中标记)吗?

我很困惑,因为我为比特率编写的代码与我为其他实例成员(艺术家姓名、歌曲标题、总时间)编写的代码基本相同,只是变量名称不同。我似乎无法弄清楚是什么导致 Eclipse 显示“0”而不是我的值。 我是 Java 的初学者,所以希望能提供一个简单的解释。 谢谢

代码:

public class Foothill {

public static void main(String [] args) {

   //default constructor 
   iTunes Coldplay = new iTunes();
   iTunes NorahJones = new iTunes();
   iTunes Banks = new iTunes();

   //constructor with parameters
   iTunes JamesBay = new iTunes("Hold Back The River", "James Bay", 300, 240006);

   //display objects immediately
   System.out.print("Initial Display: " + '\n' + '\n' );
   Coldplay.display();
   NorahJones.display();
   Banks.display();
   JamesBay.display();
   System.out.print("\n");

   //mutate many of the objects' fields
   Coldplay.setArtistName("Coldplay");
   Coldplay.setKiloBits(275);
   Coldplay.setSongTitle("Yellow");
   Coldplay.setMilliSeconds(260000);

   NorahJones.setArtistName("Norah Jones");
   NorahJones.setKiloBits(75);
   NorahJones.setSongTitle("Come Away With Me");
   NorahJones.setMilliSeconds(190000);

   Banks.setArtistName("Banks");
   Banks.setKiloBits(175);
   Banks.setSongTitle("Beggin for Thread");
   Banks.setMilliSeconds(250000);

   JamesBay.setArtistName("James Bay");
   JamesBay.setKiloBits(375);
   JamesBay.setSongTitle("Let it Go");
   JamesBay.setMilliSeconds(251000);

   //access object fields
   Coldplay.getArtistName();
   Coldplay.getSongTitle();

   NorahJones.getArtistName();
   NorahJones.getSongTitle();

   Banks.getArtistName();
   Banks.getSongTitle();

   JamesBay.getArtistName();
   JamesBay.getSongTitle();

   //check to make sure mutator call is valid
   System.out.print("Mutated Display: " + '\n' + '\n');
   if (Coldplay.setSongTitle("Viva La Vida")){ 
      Coldplay.display(); 
   }
   else { //can I delete this else?
      System.out.println("Bad Artist Name input");
   }
   if (NorahJones.setSongTitle("Come Away With Me")){
      NorahJones.display(); 
   }
   else { //can I delete this else?
      System.out.println("Bad Artist Name input");
   }
   if (Banks.setSongTitle("Beggin For Thread")){
      Banks.display();  
   } 
   else { //can I delete this else?
      System.out.println("Bad Artist Name input");
   }
   if (JamesBay.setSongTitle("Let it Go")){
      JamesBay.display(); 
      System.out.print("\n");
   }
   else { //can I delete this else?
      System.out.println("Bad Artist Name input");
   }

   //reset objects to default values
   Coldplay.setDefaults();
   NorahJones.setDefaults();
   Banks.setDefaults();
   JamesBay.setDefaults();

   //display objects for the last time
   System.out.print("Default Display: " + '\n' + '\n');
   Coldplay.display();
   NorahJones.display();
   Banks.display();
   JamesBay.display();
   System.out.println("\n");

   iTunes[] musicLibrary = new iTunes[10];
   String[] originalArtist = new String [] {"The Script", "Tegan and Sara", "Bastille", "The Beatles", "Hozier", "Sam Smith", "Birdy", "George Ezra", "Haim", "Vance Joy"};
   String[] nameArtist = new String[] {"The Script", "Tegan and Sara", "Bastille", "The Beatles", "Hozier", "Sam Smith", "Birdy", "George Ezra", "Haim", "Vance Joy"};
   String[] originalSong = new String [] {"Superheroes", "Closer", "Flaws", "Let it Be", "Take Me to Church", "Lay Me Down", "Tee Shirt", "Budapest", "Falling", "Mess Is Mine"};
   String[] titleSong = new String[] {"Superheroes", "Closer", "Flaws", "Let it Be", "Take Me to Church", "Lay Me Down", "Tee Shirt", "Budapest", "Falling", "Mess Is Mine"};
   int[] originalBit = new int [] {250, 150, 350, 450, 550, 200, 100, 400, 300, 500}; //this is should be displayed as my BitRate
   int[] rateBit = new int[] {250, 150, 350, 450, 550, 200, 100, 400, 300, 500}; //this is should be displayed as my BitRate
   int[] originalTime = new int[] {270000, 200000, 210000, 240000, 242000, 246000, 145000, 202000, 250000, 208000};
   int[] timeTotal = new int[] {270000, 200000, 210000, 240000, 242000, 246000, 145000, 202000, 250000, 208000};

   System.out.print("Array Display: " + '\n' + '\n');
   for (int i = 0; i < 10; i++) {
      musicLibrary[i] = new iTunes();

      if (musicLibrary[i].setArtistName(originalArtist[i])){
         musicLibrary[i].setArtistName(nameArtist[i]);
      }
      else {
         System.out.println("Bad Artist Name input");
      }
      if (musicLibrary[i].setSongTitle(originalSong[i])){
         musicLibrary[i].setSongTitle(titleSong[i]);
      }
      else {
         System.out.println("Bad Song Title input");
      }
      if (musicLibrary[i].setKiloBits(originalBit[i])){
         musicLibrary[i].setKiloBits(rateBit[i]);
      }
      else {
         System.out.println("Bad Bits Rate input");
      }
      if (musicLibrary[i].setMilliSeconds(originalTime[i])){
         musicLibrary[i].setMilliSeconds(timeTotal[i]);
      }
      else {
         System.out.println("Bad Song Length input");
      }

      musicLibrary[i].display();
   }
 }
}

class iTunes{
   //private class instance members
   private String name;
   private String artist;
   private int bitRate;
   private double totalTime;

   //public class static final constants
   public static final int MIN_BITRATE = 64;
   public static final int MAX_BITRATE = 705;
   public static final int MIN_STR_LENGTH = 1;
   public static final int MAX_STR_LENGTH = 128;
   public static final int MIN_PLAY_TIME = 5000;
   public static final int MAX_PLAY_TIME = 1000*60*60;
   public static final int DEFAULT_BITRATE = 64;
   public static final int DEFAULT_PLAY_TIME = 5000;
   public static final String DEFAULT_STRING = "(undefined)"; 

   //default constructor
   public iTunes (){
      name = "";
      artist = "";
      bitRate = 0;
      totalTime = 0;
   }
   //constructor with parameters
   iTunes (String songTitle, String artistName, int kiloBits, int milliSeconds) {
      name = songTitle;
      artist = artistName;
      bitRate = kiloBits;
      totalTime = milliSeconds;
   }
   //get/accessor and set/mutators for instance members
   public String getSongTitle() {
      return name;
   }
   public boolean setSongTitle(String songTitle) {
      if (songTitle.length() > MIN_STR_LENGTH && songTitle.length() < MAX_STR_LENGTH){
         name = songTitle;
         return true;
      }
      return false;
   }
   public String getArtistName() {
      return artist;
   }
   public boolean setArtistName(String artistName) {
      if (artistName.length() > MIN_STR_LENGTH && artistName.length() < MAX_STR_LENGTH){
         artist = artistName;
         return true;
      }
      return false;
   }
   public int getKiloBits() {
      return bitRate;
   }
   public boolean setKiloBits(int kiloBits) {
      if (kiloBits > MIN_BITRATE && kiloBits < MAX_BITRATE){
         bitRate = (kiloBits/1000);
         return true;
      }
      return false;
   }
   public double getMilliSeconds() {
      return totalTime;
   }
   public boolean setMilliSeconds(int milliSeconds) {
      if (milliSeconds > MIN_PLAY_TIME && milliSeconds < MAX_PLAY_TIME){
         totalTime = milliSeconds;
         return true;
      }
      return false;
   }
   public String timeInMinutesAndSeconds(){
      if (totalTime == 0.0){
         return (totalTime + " minutes and " + totalTime + " seconds");
      }
      else {
         int timeMinutes = (int) totalTime /60000;
         int timeRemainder = (int) totalTime - timeMinutes * 60000;
         int timeSeconds = timeRemainder/600;
         return (timeMinutes + " minutes and " + timeSeconds + " seconds");
      }
   }
   public String toString(){
      return ("Title: " + name + " / Artist: " + artist + " / Playing Time: " + timeInMinutesAndSeconds() + " / BitRate: " + bitRate + "k");  
   }
   public void setDefaults(){
      name = DEFAULT_STRING;
      artist = DEFAULT_STRING;
      bitRate = DEFAULT_BITRATE;
      totalTime = DEFAULT_PLAY_TIME;
   }
   public void display(){
      System.out.println(toString());
   }
}

运行:

初始显示:

标题:/艺术家:/播放时间:0.0分0.0秒/比特率:0k

标题:/艺术家:/播放时间:0.0分0.0秒/比特率:0k

标题:/艺术家:/播放时间:0.0分0.0秒/比特率:0k

标题:Hold Back The River / 艺术家:James Bay / 播放时间:4 分 0 秒 / 比特率:300k

//赋值表示只改变一个或多个成员;我在这里留下了 BitRate = 0k,所以这里应该是 BitRate = 0k

变异显示:

标题:Viva La Vida / 艺术家:Coldplay / 播放时间:4 分 33 秒 / 比特率:0k

标题:跟我走/艺术家:诺拉·琼斯/播放时间:3分16秒/比特率:0k

标题:Beggin For Thread / 艺术家:Banks / 播放时间:4 分 16 秒 / 比特率:0k

标题:Let it Go / 艺术家:James Bay / 播放时间:4 分 18 秒 / 比特率:0k

//这里的默认比特率=64k,符合预期

默认显示:

标题:(未定义)/艺术家:(未定义)/播放时间:0分8秒/比特率:64k

标题:(未定义)/艺术家:(未定义)/播放时间:0分8秒/比特率:64k

标题:(未定义)/艺术家:(未定义)/播放时间:0分8秒/比特率:64k

标题:(未定义)/艺术家:(未定义)/播放时间:0分8秒/比特率:64k

//我的问题出现在Array Display: all my BitRates = 0k

数组显示:

标题:超级英雄/艺术家:剧本/播放时间:4分50秒/比特率:0k

标题:Closer / 艺术家:Tegan 和 Sara / 播放时间:3 分 33 秒 / 比特率:0k

标题:Flaws / 艺术家:Bastille / 播放时间:3 分 50 秒 / 码率:0k

标题:Let it Be / 艺术家:The Beatles / 播放时间:4 分 0 秒 / 比特率:0k

标题:带我去教堂/艺术家:Hozier/播放时间:4分3秒/比特率:0k

标题:Lay Me Down / 艺术家:Sam Smith / 播放时间:4 分 10 秒 / 比特率:0k

标题:Tee Shirt / 艺术家:Birdy / 播放时间:2 分 41 秒 / 比特率:0k

标题:布达佩斯/艺术家:George Ezra/播放时间:3分36秒/比特率:0k

标题:Falling / 艺术家:Haim / 播放时间:4 分 16 秒 / 码率:0k

标题:Mess Is Mine / 艺术家:Vance Joy / 播放时间:3 分 46 秒 / 比特率:0k

问题出在您的 setKiloBits 方法中:

public boolean setKiloBits(int kiloBits) {
      if (kiloBits > MIN_BITRATE && kiloBits < MAX_BITRATE){
         bitRate = (kiloBits/1000);
         return true;
      }
      return false;
}

您将一个整数除以一个整数,结果小于 1(320/1000 = .32 等),它会截断余数。试试这个:

public boolean setKiloBits(int kiloBits) {
      if (kiloBits > MIN_BITRATE && kiloBits < MAX_BITRATE){
         bitRate = (kiloBits/1000.0);
         return true;
      }
      return false;
}

但是,当您从千位转换为位时,我认为您实际上是想乘以 1000?但这取决于您的业务逻辑。

编辑:正如 Alpertmd 所指出的,如果您希望分割,还需要将比特率的定义更改为双倍。

问题是:

比特率 = (kiloBits/1000)

这是做整数除法,你的答案是 0。

您可能应该将 bitRate 声明为 double 然后执行:

比特率 = (kiloBits/1000.00) .