1

我正在用 Java 制作一个计算机定价程序。我试图找出一种方法来生成由用户输出确定的子类。到目前为止,这是我的代码。我意识到我多次运行相同的方法,但我不知道任何其他方法来获取变量以测试输出。For some reason when the b.getScreenPrice(screenPrice)isn't executed when 2 is picked. 如果有帮助,任何答案。谢谢

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
4

2 回答 2

0

尝试更改您的代码以使用

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

这将修剪任何尾随和前导空格。如果您打印出您的尺寸,我敢打赌它的末尾会有一个换行符,因为按下回车键。

于 2013-10-03T18:33:47.760 回答
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());
        }

我想你要找的东西会出现

于 2013-10-03T18:55:13.917 回答