获取用户输入并在java中更改它

时间:2016-04-02 17:54:06

标签: java

我被要求获得会员类型的用户输入。

会员资格为金/银/铜奖。

Gold membership = 750
Silver membership = 450
Bronze membership = 250

下一步是询问用户是否需要雪轮胎租用,如果需要额外10%,但是我被困在那一点,是否有办法获得会员输入的结果由用户使用if语句来确定是否添加10%。

这是我到目前为止的代码。

import java.io.*;
public class SkiLodge {

    public static void main(String[] args) throws Exception
    {

        InputStreamReader ui = new InputStreamReader (System.in);
        BufferedReader gd = new BufferedReader(ui);
        BufferedReader sr = new BufferedReader(ui);
        BufferedReader bz = new BufferedReader(ui);

        System.out.println("Select Membership Type: (Gold/Silver/Bronze");
        String membership = gd.readLine();

        double x;

        if (membership.equals("Gold"))
            System.out.println("Gold Membership Selected: £" + (x = 750));
        else if (membership.equals("Silver"))
            System.out.println("Silver Membership Selected: £" + (x = 450));
        else if (membership.equals("Bronze"))
            System.out.println("Bronze Membership Selected: £" + (x = 250));
        else System.out.println("Invalid Membership");



        System.out.println("Do you require snow tyre rental? (yes/no)");
        String tyreRental = sr.readLine();


        if (tyreRental.equals("yes"))   
            System.out.println(x = x * 1.1);

    }

}

2 个答案:

答案 0 :(得分:1)

您应该只创建一个Reader并使用它来读取所有输入。由于它们都来自System.in,您的输入将出现在所有读者中。例如,输入"金/银/铜"将会出现在BufferedReader的所有人中,所以当您阅读String tyreRental = sr.readLine();时,您会真正阅读"金/银/铜奖"再次输入。

这对你来说应该更好:

import java.io.*;
public class SkiLodge {

    public static void main(String[] args) throws Exception
    {

        InputStreamReader ui = new InputStreamReader (System.in);
        BufferedReader inputReader = new BufferedReader(ui);

        System.out.println("Select Membership Type: (Gold/Silver/Bronze");
        String membership = inputReader.readLine();

        double x = 0.0d;

        if (membership.equals("Gold"))
            System.out.println("Gold Membership Selected: £" + (x = 750));
        else if (membership.equals("Silver"))
            System.out.println("Silver Membership Selected: £" + (x = 450));
        else if (membership.equals("Bronze"))
            System.out.println("Bronze Membership Selected: £" + (x = 250));
        else System.out.println("Invalid Membership");



        System.out.println("Do you require snow tyre rental? (yes/no)");
        String tyreRental = inputReader.readLine();


        if (tyreRental.equals("yes"))
            System.out.println(x = x * 1.1);

    }

}

答案 1 :(得分:0)

希望这会有所帮助。 :)

import java.io.*;
public class SkiLodge {

public static void main(String[] args) throws Exception
{

    InputStreamReader ui = new InputStreamReader (System.in);
    BufferedReader gd = new BufferedReader(ui);
    BufferedReader sr = new BufferedReader(ui);
    BufferedReader bz = new BufferedReader(ui);

    System.out.println("Select Membership Type: (Gold/Silver/Bronze");
    String membership = gd.readLine();

    double x = 0.0;
    double a = 0.0;
    if (membership.equals("Gold"))
        System.out.println("Gold Membership Selected: £" + (x = 750));
    else if (membership.equals("Silver"))
        System.out.println("Silver Membership Selected: £" + (x = 450));
    else if (membership.equals("Bronze"))
        System.out.println("Bronze Membership Selected: £" + (x = 250));
    else System.out.println("Invalid Membership");


    System.out.println("Do you require snow tyre rental? (yes/no)");
    String tyreRental = sr.readLine();

    x = x * 1.1;
    a = x;
    if (tyreRental.equals("yes"))   
        System.out.println(a);

}
}