java - 答案返回0

标签 java double

Java 初学者。我的结果一直显示为 0,而我希望它显示为 membercount * Members(即,如果有 100 名成员,并且天气 = 1,则总数应为 25)。我似乎无法弄清楚我哪里出了问题。我认为我没有正确让我的程序存储用户输入的信息,因此 double 继续读取为 0。这是我的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package playgolf;
import java.util.Scanner;
import javax.swing.JOptionPane;

/**
 * @author Alex
 */
public class PlayGolf {
    public static void main(String[] args) {
        golf stats = new golf();
        stats.getData();
        golf total = new golf();
        total.display_data();
    }
}

class golf {
    private double members;
    private double weather;
    private double temp;
    private double membercount;
    public double total;

    public void getData() {
        Scanner input = new Scanner(System.in);
        System.out.print("How many members are there?: ");
        members = input.nextInt();
        System.out.print("What is the weather like? (Enter 1 for sunny, 2 for overcast, 3 for rain): ");
        weather = input.nextInt();
        System.out.print("What is the temperature? (in Farenheight): ");
        temp = input.nextInt();

        if (weather == 1) {
            membercount = .25;
            if (weather == 2) {
                membercount = .12;
                if (weather == 3) {
                    membercount = .03;
                }
            }
        }
        if (temp < 32) {
            membercount = 0;
            System.out.println("No one will play today, it's too darn cold!");
        }
        total = (membercount * members);
    }

    public void display_data() {
        System.out.println(" ");
        System.out.println("This many members will play today: ");
        System.out.println(total);
    }
}

最佳答案

你再次创建新对象,它应该是:

public static void main(String[] args) {
       golf stats = new golf();
       stats.getData();
       stats.display_data();
}

关于java - 答案返回0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19624246/

相关文章:

python - Python 上的 Int 与 Double

java - 部署到应用程序服务器后出现 404,但不在本地

c++ - 整数除法始终为零

java - 使用方法参数选择 JOptionPane 中的图标类型

java - JAXB 和集合中的继承

c - 为什么该函数可以正常使用 float 而不能使用 double?

c# - 将 double 日期和时间值转换为另一个时区?

c - 在 C 程序中为 double 加上 f 后缀

java - 如何找到响应中的最大值?

java - JsonNode 实例何时为 null?