レニングラード 2022-08-10 22:25 采纳率: 100%
浏览 76
已结题

为什么IDEA运行时会有错误

请教一下,刚入门Java遇到的问题:一样的程序,记事本通过cmd指令编译可以运行但是IDEA显示编译错误

但是IDEA在编译前显示没有错误,在编译后才显示错误。

问题相关代码

import java.util.Scanner;
public class text{
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("please enter a number of :");
int number = input.nextInt();
int positive = 0, negetive = 0, count = 1, total = 0;
double average;
while (number != 0) {
if (number != 0) {
positive = (number > 0) ? (positive += 1) : (positive -= 1);
negetive = (number < 0) ? (negetive += 1) : (negetive -= 1);
} else if (number == 0)
break;
count++;
total += number;
number = input.nextInt();
}
average = total / count;
System.out.print("the amount of number is " + total + "and the average is " + average
+ "and count is " + count);
}}

运行结果及报错内容

please enter a number of :
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at text.main(text.java:10)

Process finished with exit code 1

我的解答思路和尝试过的方法

我看网上是把int number = input.nextInt();放到循环里,能解决是可以解决

我想要达到的结果

但是为啥会这样。就是记事本可以运行但是IDEA显示编译错误。请问是为什么?

  • 写回答

3条回答 默认 最新

  • Roc-xb 后端领域优质创作者 2022-08-11 00:24
    关注

    img


    运行之后,除了平均数计算错误外,其它都是没问题。解决平均数计算错误,只需要将count初始化值改为0即可。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月19日
  • 已采纳回答 8月11日
  • 创建了问题 8月10日

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?