我正在尝试在我的musicLibrary数组中显示10个对象。每个对象都有其自己的艺术家名称,歌曲标题,比特率和歌曲总时间。

除“ BitRate = 0k”部分外,我的运行情况都很好。我的运行为我在阵列显示下的10个对象中的每个对象提供了BitRate = 0k。我希望每个对象的BitRate为{250、150、350、450、550、200、100、400、300、500}。
这些值都在我在iTunes类中设置的范围内。无论如何,如果这些值不在设定范围内,我应该收到“ Bad _ _ input”的输出。但是,我得到的是“ 0”。

我的代码中有两个单独的部分。
第一部分由不在数组中的四个对象组成。对于这四个,我显示它们,对其进行变异,显示它们,设置为默认值,然后显示。我的跑步很好。 (如果可能出现混淆,我不想删除此部分)。

第二部分是我的十个对象的数组。对于这一部分,我只需要显示数组中十个对象的数据。我的问题在这里,当我显示我的数组并且我的BitRate数据显示为0时。
对于这两个部分,我都在检查值是否在我设置的范围内。

值得一提的是,出于清洁起见,我可以删除其他代码(在代码中标记)吗?

我很困惑,因为我为BitRate编写的代码与我为其他实例成员(艺术家名称,歌曲名称,总时间)编写的代码基本相同,尽管变量名称不同。我似乎无法弄清楚是什么导致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

标题:忍住河/艺术家:詹姆斯湾/上场时间:4分钟0秒/比特率:300k

// Assignment表示仅变异一个或多个成员;我在这里离开了BitRate = 0k,所以这里应该是BitRate = 0k

突变显示:

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

标题:跟我走吧/歌手:诺拉·琼斯/上场时间:3分16秒/比特率:0k

标题:线程开始/艺术家:银行/播放时间:4分16秒/比特率:0k

标题:随它去吧/艺术家:詹姆斯贝(James Bay)/播放时间:4分18秒/比特率:0k

//这里的默认BitRate = 64k

默认显示:

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

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

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

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

//我的问题出现在数组显示中:我所有的BitRates = 0k

阵列显示:

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

标题:Closer /艺术家:Tegan and Sara /上场时间:3分33秒/比特率:0k

标题:缺陷/艺术家:巴士底狱/上场时间:3分50秒/比特率:0k

标题:顺其自然/艺术家:披头士乐队/上场时间:4分钟0秒/比特率:0k

标题:带我去教堂/艺术家:霍齐尔/上场时间:4分3秒/比特率:0k

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

标题:T恤衫/艺术家:Birdy /上场时间:2分41秒/ BitRate:0k

标题:布达佩斯/艺术家:乔治·埃兹拉/上场时间:3分36秒/比特率:0k

标题:坠落/艺术家:Haim /播放时间:4分16秒/比特率:0k

标题:乱七八糟/艺术家:万斯·乔伊/上场时间:3分46秒/比特率:0k

最佳答案

问题是 :

bitRate =(千位数/ 1000)

这是在做整数除法,您的答案为0。

您可能应该将bitRate声明为double,然后执行以下操作:

bitRate =(kiloBits / 1000.00)。

09-25 21:25