我的课程互相冲突,我不知道为什么。

huangapple go评论55阅读模式
英文:

My classes are conflicting with each other and I dont know why

问题

所以我现在正在学习Java,并且我正在使用类似于C中的“子程序”,但出现了一个问题,当我同时使用这两个程序时,它会给我一个错误:“主线程中的异常java.util.NoSuchElementException”

主程序:

    public static void main(String[] args) {
        System.out.println("请输入一个字符串:");
        String s = SubProgram.string();
        System.out.println("字符串:" + s);
        System.out.println("请输入一个整数:");
        int i = SubProgram.umInt();
        System.out.println("整数:" + i);    
    }
}

类:


public class SubProgram {
    public static String string() {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        scan.close();
        return s;
    }

    public static int umInt(){
        Scanner scan2 = new Scanner(System.in);
        int num = scan2.nextInt();
        scan2.close();
        return num;
    }
}

如果我注释掉所有的“String”类,那么整数部分可以正常工作。如果我不注释掉“String”部分,那么字符串可以正常工作,但整数部分无法正常工作。

英文:

So I'm learning java right now and I'm using "sub-programs" just like in C but for some reason when I have both programs it gives me an error "Exception in thread "main" java.util.NoSuchElementException"

Main:

    public static void main(String[] args) {
        System.out.println("Escreva uma string:");
        String s = SubProgram.string();
        System.out.println("The string " +s);
        System.out.println("Escreva um Int:");
        int i = SubProgram.umInt();
        System.out.println("The int: " +i);    
    }
}

Classes:


public class SubProgram {
    public static String string() {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        scan.close();
        return s;
    }

    public static int umInt(){
        Scanner scan2 = new Scanner(System.in);
        int num = scan2.nextInt();
        scan2.close();
        return num;
    }
}

If I comment all the "String" class the int one works fine. If I don't comment the String, the String works fine but the int does not

答案1

得分: 1

首先,让我们澄清一些术语:实际上,无论是C还是Java,都没有使用"子程序"这个术语,但如果您不是英语母语人士,在翻译时犯这个错误是可以理解的。

您的程序中有一个类 - SubProgram,它有两个"方法" - stringumInt

这些名称不太好,因为它们与数据类型太相似。
最好使用诸如getStringgetInt之类的方法名称。

但真正的问题是您关闭了System.in
就像C中的stdin一样,Java中的System.in是表示程序标准输入的文件。

Java有几个用于更方便地处理文件的"包装器"类,其中之一就是Scanner

但是,如果关闭了包装文件的Scanner,也将关闭文件。
请注意,Scanner不仅会在显式调用close时关闭,而且如果超出范围,它也会关闭 - 这意味着如果您在方法中创建了Scanner对象,就像您现在所做的那样,一旦退出方法,Scanner将会自动关闭。

这是Java与C不同的地方:对许多事物都有自动的"清理"!

解决问题的一种方法是将Scanner转换为类的静态成员,如下所示:

public class SubProgram {
    private static final Scanner INPUT = new Scanner(System.in);

    public static String string() {
        String s = INPUT.nextLine();
        return s;
    }

    public static int umInt(){
        int num = INPUT.nextInt();

        return num;
    }
}

我添加了final修饰符,类似于C中的const
由于您希望在程序运行时不更改此Scanner,最好将其保存为常量。

此外,在Java中,使用大写字母表示常量名称是习惯做法(虽然语言不要求)。

  • 一个小注:出于简单起见,我使用了"文件"这个术语,但实际上,System.in实际上是一个"流"(类型为InputStream的对象)。流是Java处理诸如文件和套接字等可以读写的东西的方式,但目前您可以将其视为类似于C中捆绑有相关函数(如fopen)的FILE *
英文:

First, lets clarify some terms: neither C nor Java actually use the term "sub program", but if you are not a native English speaker this is an understandable error in translation.

What you have in your program is one class - SubProgram that has 2 "methods" - string and umInt.

These names are not very good, since they are too similar to data types.
It would be preferable to use method names like getString and getInt.

But the real problem is that you are closing System.in.
Just like stdin in C, System.in in Java is a file representing the standard input of your program.

Java has several "wrapper" classes to make working with files more convenient, and Scanner is one of them.

However, if you close the Scanner wrapping the file, you will also close the file.
Note, that Scanner will close not only if you explicitly call close, but also if it goes out of scope - this means if you created the Scanner object in a method, like you do now, once you exit the method the Scanner will close automatically.

This is the part where Java is different from C: there is automatic "cleanup" for a lot of things!

One way to solve your problem is to turn Scanner in to a static memeber of the class, like this:

public class SubProgram {
    private static final Scanner INPUT = new Scanner(System.in);

    public static String string() {
        String s = INPUT.nextLine();
        return s;
    }

    public static int umInt(){
        int num = INPUT.nextInt();

        return num;
    }
}

I added the final modifier which is similar to const in C.
Since you don't want to change this Scanner as long as your program is running, it is better to hold it in a constant.

Also, it is customary to use UPPERCASE for constant names in Java (though it is not required by the language).

  • One small note: I used the term "file" for simplicity, but System.in is actually a "stream" (object of type InputStream). Streams is how Java deals with things you can read and write like files and sockets, but for now you can think of it as being similar to FILE * in C bundled with relevant functions like fopen.

huangapple
  • 本文由 发表于 2020年10月5日 05:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64199888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定