简体   繁体   English

UVA Java运行时错误

[英]UVA Java runtime error

I am a new learner. 我是一个新学习者。 Why uva show runtime error? 为什么UVA显示运行时错误?

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int a, b, c, i, n;

        Scanner d = new Scanner(System.in);
        System.out.println("how many test");
        n = d.nextInt();

        for(i = 0; i <= n; i++)
        {
            Scanner s = new Scanner(System.in);
            System.out.println("enter the value of a\n");
            a = s.nextInt();

            Scanner x = new Scanner(System.in);
            System.out.println("enter the value of b\n");
            b = x.nextInt();

            Scanner z = new Scanner(System.in);
            System.out.println("enter the value of c\n");
            c = z.nextInt();

            if(a>b && a<c || b>a && a>c)
               System.out.println(a);

            if(b>a && b<c || b<a && b>c)
               System.out.println(b);

            if(c>b && c<a || c>a && b>c)
               System.out.println(c);
        }
    } 
}

Three small suggestions 三个小建议

  • It's actually reading n+1 cases, change "<=" to "<" on the for loop. 它实际上正在读取n + 1种情况,在for循环上将“ <=”更改为“ <”。

  • No need to instantiate a new Scanner every time you read, remove all "new Scanner..." except the first one, and from then on, use that one, egdnextInt() inside the loop. 无需在每次阅读时实例化一个新的Scanner,只需删除第一个扫描仪之外的所有“ new Scanner ...”,然后从此开始,在循环中使用该实例,例如egdnextInt()。

  • Close the scanner after the loop: d.close(); 循环后关闭扫描仪:d.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM