gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:06:56 25 4
gpt4 key购买 nike

系统要求我获取成员(member)类型的用户输入。

成员(member)资格分为金级/银级/铜级。

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

下一步是询问用户是否需要租用雪地轮胎,如果需要,则需要额外支付 10%,但是我被困在这一点上,有没有办法获取用户输入的成员(member)资格的结果以及使用 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);

}

}

最佳答案

您应该只创建一个 Reader 并用它来读取所有输入。由于它们都源自 System.in,因此您的输入将出现在所有阅读器中。例如,“Gold/Silver/Bronze”的输入将位于您的每个 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);

}

}

关于java - 获取用户输入并在java中更改它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36376843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com