gpt4 book ai didi

java - <为什么我的平均值没有增加到零以上?> 转换为 double

转载 作者:行者123 更新时间:2023-12-01 09:38:05 25 4
gpt4 key购买 nike

更新:我尝试将 getCalcMean() 和 calcMean() 方法的返回类型更改为 double 以使其更加准确,并且 Person 类中的所有内容都工作正常,但在程序类中现在显示“不兼容”类型:从 double 到 int 可能有损转换。”在 System.out.println(math1.calcMean(math1.getCalcMean()));我理解错误的含义,但我不知道如何修复它,因为最终结果需要是 double 的,我认为java可以计算 double 和整数并得到 double 。我究竟做错了什么?<<>>

<小时/>

我试图计算输入 999 后总数的平均值,但它一直显示 0,我不明白为什么。

有人可以告诉我如何让 getCalcMean() 方法将平均值显示为 numTotal/count 吗?

--- 类(class)计划 ---

public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int num = 0;
int count = 1;
Math math1 = new Math(num, count);
while (num != 999) {
num = kb.nextInt();
if (num != 999) {
math1.adder(num);
count ++;
System.out.println("Total till now:" + math1.getNumTotal());
}
}
math1.setCount(count);
System.out.println(math1.getNumTotal());
System.out.println(math1.calcMean(math1.getCalcMean()));
//System.out.println(math1.getNum());
kb.close();
/*Scanner kb = new Scanner(System.in);
String input = kb.nextLine();
Scanner scn = new Scanner(input);
int num = scn.nextInt();
Math math1 = new Math(num,0);
while(num != 999){
math1.adder(num);
input = kb.nextLine();
}

System.out.println(math1.getNumTotal());*/
} //main

}

---- 类数学 ----

public class Math {

private int num;
private int numTotal;
private double mean;
private int count;

/*public Math(int num, int numTotal){
this.num = num;
}*/

public Math(int num, int count) {
this.num = num;
this.count = count;
}

//get//
public int getNum(){
return this.num;
}

public int getNumTotal(){
return this.numTotal;
}

public double getCalcMean(){
return this.mean;
}

//set//

public void setNumTotal(int value){
this.numTotal = value;
}

public void setNum(int value){
this.num = value;
}

public void setCalcMean(double value){
this.mean = value;
}

public void setCount(int value){
this.count = value;
}
//other
/*public void adder(int num){
numTotal = numTotal + num;
}*/

public void adder(int num) {
this.num = num;
numTotal = numTotal + this.num;
}
//added after//
public double calcMean(int num){
this.numTotal = numTotal;
mean = numTotal / this.count;
return mean;
}

}

最佳答案

  1. counter++ 在代码中是没有意义的,因为您从未将其传递给 Math 对象。
  2. math1.getCalcMean() 将返回 Math 对象中的 mean,但 mean 尚未计算。

建议:

  1. 在数学类中为计数器添加 getter 和 setter。
  2. getCalcMean() 方法中计算平均值

请看下面的代码。我假设您使用 999 作为结束标志,因此没有考虑到它。计数器初始化为 0。

import java.util.Scanner;

public class Program {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int num = 0;
int counter = 0;
Math math1 = new Math(num, counter);
while (num != 999) {
num = kb.nextInt();
if (num != 999) {
math1.adder(num);
counter ++;
math1.setCounter(counter);
System.out.println("Total till now:" + math1.getNumTotal());
}
}
System.out.println(math1.getNumTotal());
System.out.println(math1.getCalcMean());
//System.out.println(math1.getNum());
kb.close();
/*Scanner kb = new Scanner(System.in);
String input = kb.nextLine();
Scanner scn = new Scanner(input);
int num = scn.nextInt();
Math math1 = new Math(num,0);
while(num != 999){
math1.adder(num);
input = kb.nextLine();
}

System.out.println(math1.getNumTotal());*/
} //main
}

还有数学课。

public class Math {

private int num;
private int numTotal;
private int mean;
private int counter;

/*public Math(int num, int numTotal){
this.num = num;
}*/

public Math(int num, int counter) {
this.num = num;
this.counter = counter;
}

//get//
public int getNum(){
return this.num;
}

public int getNumTotal(){
return this.numTotal;
}

public int getCalcMean(){
mean = numTotal / this.counter;
return mean;
}

public int getCounter(){
return this.counter;
}

//set//

public void setNumTotal(int value){
this.numTotal = value;
}

public void setNum(int value){
this.num = value;
}

public void setCalcMean(int value){
this.mean = value;
}

public void setCounter(int counter){
this.counter = counter;
}

//other
/*public void adder(int num){
numTotal = numTotal + num;
}*/

public void adder(int num) {
this.num = num;
numTotal = numTotal + this.num;
}
//added after//
public void calcMean(int num){
mean = numTotal / this.counter;
}
}

关于java - <为什么我的平均值没有增加到零以上?> 转换为 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38680270/

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