簡體   English   中英

當我輸入整數時,為什么我的Java雙變量類型變為0.0?

[英]Why is my Java double variable type coming out to 0.0 when I put in an integer?

我正在使用驅動程序類來創建另一個類的對象。 當我輸入寵物重量或整數時,數字為0.0。 所有權重變量都聲明為double,因此我不知道為什么要這樣做。

  import java.util.Scanner;

    public class PetAssignment {

        public static void main(String[] args) 
        {
            String nameAndType;
            int yrs;
            double lbs;

            //Scanner object for the keyboard input
            Scanner answers = new Scanner(System.in);

            //Pet objects used for calling accessor methods
            Pet petName = new Pet();
            Pet petType = new Pet();
            Pet petAge = new Pet();
            Pet petWeight = new Pet();

            //A bunch of other code and pet attributes

            //Input for the weight of pet
            System.out.print("How many pounds does your pet weight? ");
            lbs = answers.nextDouble();
            petName.setWeight(lbs);

            //Print out of the user's answers
            System.out.println(""); 
            System.out.println("You have a "+ petType.getType() + ". That is named " 
                + petName.getName()+ " and is " 
                + petAge.getAge() + " years old and weighs " 
                + petWeight.getWeight() + " lbs.");         
        }
    }

這是我的寵物課

 public class Pet 
    {
      private String name;
      private String type;
      private int age;
      private double weight;    
      /*
       * a bunch of other code
       */
      public void setWeight(double petWeight)
      {
          weight = petWeight;
      }
      /*
       * a bunch of other code
       */
      public double getWeight()
      {
          return weight;
      }
    }

此問題的第一要點是,您只需要一個類“ Pet”的實例。 一只寵物可以容納您需要的所有變量。 例如:

Pet pet = new Pet();
System.out.print("How many pounds does your pet weight? ");
lbs = answers.nextDouble();
petName.setWeight(lbs);

 System.out.println(""); 
        System.out.println("You have a "+ pet.getType() + ". That   is named " 
            + pet.getName()+ " and is " 
            + pet.getAge() + " years old and weighs " 
            + pet.getWeight() + " lbs.");

當您只需要一個容器時,您編寫它的方式實際上會創建4個不同的容器。 您將權重分配給了petWeight容器,但隨后嘗試從petName容器中獲取權重,這導致您檢索了錯誤的變量。 在這種情況下,只有一個容器或實例稱為“ pet”,就不會發生此問題。

錯誤的是您使用此代碼設置值

 petName.setWeight(lbs);

並用它來獲取價值

 Pet petWeight = new Pet();

它們是2個不同的對象,您必須在set中使用2個語句使用相同的對象,

petWeight.setWeight(lbs);

代替

petName.setWeight(lbs);

會解決的

如果ageweight沒有預期的格式,建議您檢查getAge()getWeight的返回類型。 如果它與字段不匹配,則返回時至少在int->double情況下會轉換(我相信double->int將需要強制轉換)。

但正如已經提到的,將.0添加為double的預期行為。 如果您不想要它,可以將其顯式轉換為int

我看到的問題是您正在創建多個Pet對象,並將pet重量分配給“ petName”對象,但是隨后您嘗試將“ petWeight”對象的Weight添加到輸出中。 請嘗試以下操作:

        //Input for the weight of pet
        System.out.print("How many pounds does your pet weight? ");
        lbs = answers.nextDouble();
        petWeight.setWeight(lbs);

        //Print out of the user's answers
        System.out.println(""); 
        System.out.println("You have a "+ petType.getType() + ". That is named " 
            + petName.getName()+ " and is " 
            + petAge.getAge() + " years old and weighs " 
            + petWeight.getWeight() + " lbs.");         
    }
}

另外,我建議僅使用一個對象“ pet”並將每個值分配給該對象,以及在system.out上僅使用該對象。

問題在第34行,請檢查您在這里做什么

petName.setWeight(lbs);

但是當您顯示輸出時

System.out.println("You have a "+ petType.getType() + ". That is named " 
            + petName.getName()+ " and is " 
            + petAge.getAge() + " years old and weighs " 
            + petWeight.getWeight() + " lbs.");

你看到了嗎? 您正在顯示“ petWeight”的權重,但掃描儀正在設置petName對象,請進行調試並簽出。

System.out.print("How many pounds does your pet weight? ");
lbs = answers.nextDouble();
petWeight.setWeight(lbs); 

結果是

 You have a null. That is named null and is 0 years old and weighs 1.5 lbs. 

當然,其他屬性為null。

我希望這有用。 問候!

謝謝大家的幫助。 我犯了這么簡單的錯誤,真是個白痴。 我還僅用一個pet類實例簡化了我的代碼。

import java.util.Scanner;

    public class PetAssignment {

        public static void main(String[] args) 
        {
            String nameAndType;
            int yrs;
            double lbs;

            //Scanner object for the keyboard input
            Scanner answers = new Scanner(System.in);

            //Pet objects used for calling accessor methods
            Pet pet = new Pet();

            //A bunch of other code and pet attributes

            //Input for the weight of pet
            System.out.print("How many pounds does your pet weight? ");
            lbs = answers.nextDouble();
            pet.setWeight(lbs);

            //Print out of the user's answers
            System.out.println(""); 
            System.out.println("You have a "+ pet.getType() + ". That is named " 
                + pet.getName()+ " and is " 
                + pet.getAge() + " years old and weighs " 
                + pet.getWeight() + " lbs.");         
        }
    }

您在設置和增加寵物重量方面做錯了。 您需要使用同一對象來設置和獲取價值。 您正在petName對象中設置Pet重量,並從petWeight對象獲取。 這就是為什么要得到0.0的原因,這是double的默認值。 要解決您的問題,請使用同一對象設置並獲得寵物的體重。

例:

petWeight.setWeight(lbs); //setting the value in petWight object
petWeight.getWeight(); // it will returns the same value that you set.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM