gpt4 book ai didi

Java:计算总数并将其存储在变量中

转载 作者:行者123 更新时间:2023-11-30 10:53:14 25 4
gpt4 key购买 nike

我被要求计算今天预计参加比赛的高尔夫俱乐部成员(member)总数(总数乘以小数百分比)并将该总数存储在球员变量中。我不知道如何存储总数。任何帮助都会很棒。我尝试使用 if 语句然后输入 players = input.nextDouble(); 但这不起作用。我对 Java 很陌生......

 class golf
{
private int weather;
private double val1;
private double players;

public void calculations()
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of club members: ");
val1 = input.nextDouble();

System.out.println("Now select a number for the weather"); //weather atm
System.out.println("1: Sunny");
System.out.println("2: Overcast");
System.out.println("3: Rain");
System.out.print("Selection: ");
weather = input.nextInt();

System.out.println(" ");
}

public void output()
{
if (weather == 1) //calculates expected players when Sunny
System.out.println(val1*.25 + " " + "will play golf today!");

if (weather == 2) //calculates expected players when Overcast
System.out.println(val1*.12 + " " + "will play golf today!");

if (weather == 3) //calculates expected players when Rainy
System.out.println(val1*.03 + " " + "will play golf today!");
}
}

最佳答案

这是你需要的吗?

public void output() {
if (weather == 1) {
players = 0.25 * val1;
}

if (weather == 2) {
players = 0.12 * val1;
}

if (weather == 3) {
players = 0.03 * val1;
}

System.out.println(players + " will play golf today!");
}

关于Java:计算总数并将其存储在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34080995/

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