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/