Java:在变量中计算和存储总计
我被要求计算今天预计参加比赛的高尔夫俱乐部会员总数(总数乘以小数点后的百分比),并将总数存储在players变量中。我不知道如何储存总数。任何帮助都会很好。我尝试使用if语句,然后键入,Java:在变量中计算和存储总计,java,Java,我被要求计算今天预计参加比赛的高尔夫俱乐部会员总数(总数乘以小数点后的百分比),并将总数存储在players变量中。我不知道如何储存总数。任何帮助都会很好。我尝试使用if语句,然后键入,players=input.nextDouble()但这不起作用。我对java很陌生 class golf { private int weather; private double val1; private double players; public void
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!");
}
total=val1*0.25代码>?它必须在计算方法中,顺便说一句……您是否声明了变量players
?我想您可能需要一个方法,该方法采用天气类型
并返回玩家数量。请参阅,您的意思是您将多次调用calculations()
和output()
,并使用变量players
存储总数?将其标记为已接受,以便其他人可以找到它是否有用Java是否有switch
/case
语句?我忘了。至少,如果
/else
,为什么不使用else呢?是的。我们可以使用switch/case
或if/else/if
。他们两个都在那里