Java - 最高、最低和平均

发布于 2024-09-02 10:55:06 字数 1826 浏览 7 评论 0原文

是的,那么为什么Java会出现这个错误:

Exception in thread "main" java.lang.Error: Unresolved Compilation Problem: 类型不匹配:无法

在 rainfall.main(rainfall.java:38)

处从 double 转换为 int从此:

public class rainfall {

 /**
  * @param args
  */
 public static void main(String[] args) 
 {
 int[]  numgroup;
 numgroup = new int [12];
 ConsoleReader console = new ConsoleReader();
 int highest;
 int lowest;
 int index;
 int tempVal;
 int minMonth;
    int minIndex;
 int maxMonth;
 int maxIndex;


 System.out.println("Welcome to Rainfall");
 // Input (index now 0-based)
 for(index = 0; index < 12; index = index + 1)
 {       
     System.out.println("Please enter the rainfall for month " + index + 1);
     tempVal = console.readInt();
     while (tempVal>100 || tempVal<0)
     {
         System.out.println("The rating must be within 0...100. Try again");
         tempVal = console.readInt();
     }
     numgroup[index] = tempVal;
 }           

 lowest = numgroup[0];
 highest = numgroup[0];
 int total = 0.0;
 // Loop over data (using 1 loop)
 for(index = 0; index < 12; index = index + 1)
 {       
     int curr = numgroup[index];
     if (curr < lowest) {
         lowest = curr;
         minIndex = index;
     }
     if (curr > highest) {
         highest = curr;
         maxIndex = index;
     }
      total += curr;
 }
 float avg = (float)total / numgroup.length;

 System.out.println("The average monthly rainfall was " + avg);
 // +1 to go from 0-based index to 1-based month
 System.out.println("The lowest monthly rainfall was month " + minIndex + 1);
 System.out.println("The highest monthly rainfall was month " + maxIndex + 1);

 System.out.println("Thank you for using Rainfall");

 }


 private static ConsoleReader ConsoleReader() {

  return null;
 }

}

Right, so why does Java come up with this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from double to int

at rainfall.main(rainfall.java:38)

From this:

public class rainfall {

 /**
  * @param args
  */
 public static void main(String[] args) 
 {
 int[]  numgroup;
 numgroup = new int [12];
 ConsoleReader console = new ConsoleReader();
 int highest;
 int lowest;
 int index;
 int tempVal;
 int minMonth;
    int minIndex;
 int maxMonth;
 int maxIndex;


 System.out.println("Welcome to Rainfall");
 // Input (index now 0-based)
 for(index = 0; index < 12; index = index + 1)
 {       
     System.out.println("Please enter the rainfall for month " + index + 1);
     tempVal = console.readInt();
     while (tempVal>100 || tempVal<0)
     {
         System.out.println("The rating must be within 0...100. Try again");
         tempVal = console.readInt();
     }
     numgroup[index] = tempVal;
 }           

 lowest = numgroup[0];
 highest = numgroup[0];
 int total = 0.0;
 // Loop over data (using 1 loop)
 for(index = 0; index < 12; index = index + 1)
 {       
     int curr = numgroup[index];
     if (curr < lowest) {
         lowest = curr;
         minIndex = index;
     }
     if (curr > highest) {
         highest = curr;
         maxIndex = index;
     }
      total += curr;
 }
 float avg = (float)total / numgroup.length;

 System.out.println("The average monthly rainfall was " + avg);
 // +1 to go from 0-based index to 1-based month
 System.out.println("The lowest monthly rainfall was month " + minIndex + 1);
 System.out.println("The highest monthly rainfall was month " + maxIndex + 1);

 System.out.println("Thank you for using Rainfall");

 }


 private static ConsoleReader ConsoleReader() {

  return null;
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情未る 2024-09-09 10:55:06

我猜罪魁祸首是这一行:

int total = 0.0;

应该是

int total = 0;

I guess the culprit is this line:

int total = 0.0;

should be

int total = 0;

instead.

云裳 2024-09-09 10:55:06

问题出在这一行:

int total = 0.0; 

需要将总计更改为 float 类型

Problem is with this line here:

int total = 0.0; 

Need to change total to be of type float

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文