从Subclasses Java获取值

时间:2013-10-03 18:24:26

标签: java subclass

我正在用Java制作计算机定价程序。我试图找出一种方法来生成由用户输出确定的子类。到目前为止,我的代码。我意识到我多次运行相同的方法,但我不知道任何其他方式来获取变量以测试输出。出于某种原因,当选择2时未执行b.getScreenPrice(screenPrice)。如果有帮助,任何答案。感谢

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Computer {
    InputStreamReader in = new InputStreamReader(System.in);

    BufferedReader keyboard = new BufferedReader(in);

    public Computer() {

    }

    public String pickComputer() {
        String comp = null;
        System.out.println("Select a number for desired computer:");
        System.out.println("1. Desktop");
        System.out.println("2. Laptop");
        try {
            comp = keyboard.readLine();
            if (comp.equals("1")) {
                comp = "Desktop";
            }
            if (comp.equals("2")) {
                comp = "Laptop";
            }
        } catch (IOException e) {
            pickComputer();
        }
        return comp;
    }

    public class Notebook extends Computer {
        public String getScreenSize() {
            String size = null;
            System.out.println("Select a number for the desired screen size:");
            System.out.println("1. 19 inch ($200)");
            System.out.println("2. 17 inch ($150)");
            System.out.println("3. 15 inch ($100)");
            try {
                size = keyboard.readLine();

                if (size.equals("1")) {
                    size = "19";
                }
                if (size.equals("2")) {
                    size = "17";
                }
                if (size.equals("3")) {
                    size = "15";
                }

            } catch (IOException e) {
                getScreenSize();
            }
            return size;

        }

        public int getScreenPrice(String screen) {
            int sPrice = 0;
            if (screen.equals("19")) {
                sPrice = 200;
            }
            if (screen.equals("17")) {
                sPrice = 150;
            }
            if (screen.equals("15")) {
                sPrice = 100;
            }
            return sPrice;

        }

        public void calculatePrice(int screeen) {
            System.out.println(screeen);
        }

    }

    public class ComputerTester {
        public static void main(String[] args) {

            Computer a = new Computer();
            Notebook b = new Notebook();
            String screenSize = b.getScreenSize();
            String newComputer = a.pickComputer();

            if (newComputer.equals("Desktop")) {
                System.out.println("Test Worked");
            }
            if (newComputer.equals("Laptop")) {
                b.getScreenPrice(screenSize);
            }
        }
    }
}

输出:

Select a number for the desired screen size:
1. 19 inch ($200)
2. 17 inch ($150)
3. 15 inch ($100)
1
Select a number for desired computer:
1. Desktop
2. Laptop
2

2 个答案:

答案 0 :(得分:0)

尝试更改代码以使用

size = keyboard.readLine().trim();

这将修剪任何尾随和前导空格。如果你打印出你的尺寸,我打赌它会在它的末尾有一个新的线条字符,因为按下回车。

答案 1 :(得分:0)

在getScreenPrice(String)方法中,您返回一个int值,但在main方法中,您只是调用该方法。您没有设置int值或任何东西。所以正在发生的事情是你的代码在你选择2时没有任何反应就能正常工作,因为在你的程序结束时“没有”被认为会发生。

尝试更改此内容:

        if (newComputer.equals("Laptop")) {
            b.getScreenPrice(screenSize);
        }

在此:

        if (newComputer.equals("Laptop")) {
           int price = b.getScreenPrice(screenSize);
           System.out.println(price.toString());
        }

我认为你要找的是