【问题标题】:Why isnt the method calculating the doorspace?为什么不是计算门空间的方法?
【发布时间】:2016-11-16 16:35:49
【问题描述】:

我的方法有一些问题。它似乎没有计算门空间,因为它总是返回 0.0 的值。它应该根据输入的长度和高度来计算。谢谢

package Hw;

import java.util.Scanner;

public class HouseReno3   {

            public static void main(String[] args) {

            // Initialize and Declare Variables
            double ceramictile = 4.00;
            double hardwood = 3.00;
            double carpet = 2.00;
            double linoleumtile = 1.00;

            // Initialize Variables
            int floortype;
            int budget;
            double length; 
            double height;
            double width;
            double ld = 0;
            double hd = 0;
            double lw1;
            double hw1;
            double lw2;
            double hw2;
            double lb;
            double hb;
            double wb;
            double door = 0;
            double window = 0;
            double bookshelf = 0;
            double doorspace = 0;

            doorspace = itemd (door, ld, hd, doorspace);
        //  double costp;
        //  double costf;

            // Initialize Scanner
            Scanner keyboard = new Scanner (System.in);

            System.out.println("Enter Number Of Doors");
            door = keyboard.nextDouble();
            for(int doors = 0; doors < door; doors++)
            {   

                System.out.println("What is the length of the door (in feet)? ");
                ld = keyboard.nextDouble();
                System.out.println("What is the height of the door (in feet)? ");
                hd = keyboard.nextDouble();
                System.out.println("Doorspace is: " + doorspace);

            }



            System.out.println("Enter Number Of Windows");
            window = keyboard.nextDouble();
            for(int windows = 0; windows < window; windows++)
            {
                System.out.println("What is the length of the window (in feet)? ");
                lw1 = keyboard.nextDouble();
                System.out.println("What is the height of the window (in feet)? ");
                hw1 = keyboard.nextDouble();
            }

            System.out.println("Enter Number Of Bookshelves");
            bookshelf = keyboard.nextDouble();
            for(int bookshelves = 0; bookshelves < bookshelf; bookshelves++)
            {
                 System.out.println("What is the length of the bookcase (in feet)? ");
                 lb = keyboard.nextDouble();
                 System.out.println("What is the height of the bookcase (in feet)? ");
                 hb = keyboard.nextDouble();
                 System.out.println("What is the width of the bookcase (in feet)? ");
                 wb = keyboard.nextDouble();

            }

    }

            private static int itemd(double door, double ld, double hd, double doorspace) {

                doorspace = ld *hd;
                return (int) doorspace;
            }                   
}

【问题讨论】:

  • 也许您应该在处理完所有与门相关的输入值后才调用itemd
  • 我试过这样做,但输出仍然是 0.0
  • 尝试调用System.out.println("Doorspace is: " + itemd(door, ld, hd, doorspace));而不是System.out.println("Doorspace is: " + doorspace);
  • 非常感谢!

标签: java methods


【解决方案1】:

在原始代码中,门空间的计算是在读取输入值之前完成的。

doorspace = itemd (door, ld, hd, doorspace);
[...]

// Initialize Scanner
Scanner keyboard = new Scanner (System.in);

System.out.println("Enter Number Of Doors");

所以传递给itemdldhd 为0。您必须移动计算。如 cmets 中所述,更改线路就足够了

System.out.println("Doorspace is: " + doorspace);

System.out.println("Doorspace is: " + itemd (door, ld, hd, doorspace));

【讨论】:

  • 我仍然收到相同的答案。
猜你喜欢
  • 2010-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
  • 2014-02-20
  • 2010-09-13
相关资源
最近更新 更多