存储在for循环中使用的访问器方法中的值?

时间:2017-07-28 22:25:01

标签: java object for-loop accessor

基本上,我需要在模块中使用getOriginalPrice方法中的值。

当我简单地放入item.getOriginalPrice * .1时,我的代码仍会编译。

不确定这个是怎么回事。

这是我的代码(除非需要,否则不粘贴原始类代码)。

import java.util.Scanner;

class InventoryApp {

   public static void main(String[] args) {

   String invNum = " ";
   double ogPrice = 1;
   int finishItems;

   Inventory saleItem;
   saleItem = new Inventory();

   Scanner keyb = new Scanner(System.in);

   System.out.println("Input the item numbers and original price for each item");

   System.out.println("and I will show you the discounted price of each item");

   System.out.println("for each day of the sale this week.");

   System.out.println("Enter stop at the first prompt to end your list.");


   System.out.println("Enter the item number of the item (Ex. 2b)");
   invNum = keyb.next();
   saleItem.setItemNumber(invNum);
   while (!invNum.equals("stop")) {

   System.out.println("Enter the item's original price (Ex 23.50");
   ogPrice = keyb.nextDouble();
   saleItem.setOriginalPrice(ogPrice);
   printSaleData(saleItem);

   System.out.println("Enter the item's item number");
   invNum = keyb.next();
   saleItem.setItemNumber(invNum);



   }//end While




   }//end main
   public static void printSaleData(Inventory item) {

   System.out.println("Item Number: " + item.getItemNumber());

      for(int x = 1; x < 7; x = x + 1) {
         item.getOriginalPrice() = item.getOriginalPrice - 
(item.getOriginalPrice * .1);
         System.out.println("Day: " + x + "\t" + "Price: $" + item.getOriginalPrice());

      }//end for
   }
}//end Class

1 个答案:

答案 0 :(得分:0)

您的printSaleDate方法无效java。根据您发布的信息,您不清楚预期的输出是什么。

但严格来说,一旦修好,这是你发布的方法:

for (int x=1; x<7; x++) {
    double discountedPrice = item.getOriginalPrice() * 0.1;
    System.out.println("Day: " + x + "\tPrice: $" + discountedPrice);
}