变量average定义了两次
书写错误,scan.nextInt(); 是 next int 的意思 下一个整型。是字母i的大写,而不是L的小写。
另外要考虑数据类型转换的问题,int / int 的结果也是int。比如如果定义 double a = 3 / 2; 结果是a=1.0。double a = 3 / 2.0; 则a=1.5;
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
int val1, val2, val3;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter three integers and " + "I will compute their average");
val1 = scan.nextInt();
val2 = scan.nextInt();
val3 = scan.nextInt();
// 除以3.0才能让结果是较准确的double类型
double average = (val1 + val2 + val3) / 3.0;
System.out.println("The average is:" + average);
}
}
已改,看注释
#include
int main()
{
int a,b,c;
float average;
printf("input:");
scanf("%d%d%d",&a,&b,&c); // 里面的\n去掉
printf("a=%d,b=%d,c=%d\n",a,b,c);
average=(a+b+c)/(float)3; // 将3强制转换为float
printf("average=%f\n",average);
return 0;
}
我给你改了,如下:
#include
main()
{
int a,b,c;
float average;
printf("input:");
scanf("%d%d%d",&a,&b,&c);
printf("a=%d,b=%d,c=%d\n",a,b,c);
average=(a+b+c)/3;
printf("average=%f\n",average);
}
把scanf里面那个“/n”去掉就OK了!!
average=(a+b+c)/3;
改为average=(float)(a+b+c)/3;
试试