简体   繁体   English

从子类Java获取值

[英]Get values from Subclasses Java

I am making a computer pricing program in Java. 我正在用Java开发计算机定价程序。 I am trying to figure out a way to generate subclasses determined by the user output. 我试图找出一种生成由用户输出确定的子类的方法。 Heres my code so far. 到目前为止,这是我的代码。 I realize I am running the same methods multiple times but I dont know any other way to get the variable in order to test the output. 我意识到我多次运行相同的方法,但是我不知道其他任何方法来获取变量以测试输出。 For some reason when the b.getScreenPrice(screenPrice) isn't executed when 2 is picked. 由于某些原因,当选择2时未执行b.getScreenPrice(screenPrice) Any answers if of help. 任何帮助的答案。 thanks 谢谢

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);
            }
        }
    }
}

Output: 输出:

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

Try changing your code to use 尝试更改您的代码以使用

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

This will trim any trailing and leading whitespace. 这将修剪所有尾随和前导的空格。 If you were print out your size, I bet it would have a new line character on the end of it cause by pressing enter. 如果您打印出了自己的尺寸,我敢打赌,按回车键会导致它的末尾有换行符。

In your getScreenPrice(String) method, you are returning an int value but in your main method you are just calling the method. 在您的getScreenPrice(String)方法中,您将返回一个int值,但是在您的主方法中,您只是在调用该方法。 You are not setting an int value or anything. 您未设置int值或任何其他值。 So what is happening is your code is working fine if nothing happens when you choose 2 because at the end of your program "nothing" is suppose to happen. 因此,如果您选择2时没有任何反应,那么正在发生的事情就是您的代码可以正常工作,因为在程序结束时“什么也没有发生”。

Try changing this: 尝试更改此:

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

in this: 在此:

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

I think what youre looking for will appear 我想你要找的东西会出现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM