首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扫描多个输入时没有此类元素异常

扫描多个输入时没有此类元素异常
EN

Stack Overflow用户
提问于 2019-01-17 13:44:31
回答 3查看 339关注 0票数 2

我是java编程新手,我试图学习java编程中类和对象的用法,同时编写下面的代码,我得到了一个例外。

java.util.NoSuchElementException

用于样本输入

5

1 2 3 4 5

这里,第一行包含元素数(在本例中为5),下一行包含元素。

当在类选举中接受for循环中的输入时,我得到了异常。

我试着搜索堆栈溢出和其他资源,但仍然找不出如何删除这个异常。

代码语言:javascript
复制
import java.io.*;
import java.util.Scanner;
public class TestClass {
     public static void main(String[] args) {
        int n;
        Scanner input = new Scanner(System.in);
        n = input.nextInt();
        input.nextLine();
        Election obj = new Election(n);
        obj.getVotes();
    }
}
class Election {
     int n,v1,v2,v3,v4,v5,d;
     public Election(int n) {
          this.n = n;
          v1=v2=v3=v4=v5=d=0;
     }
     public void getVotes() {
       Scanner sc = new Scanner(System.in);
       for(int i = 0 ; i < 1 ; i++) {
         int var = sc.nextInt();
         switch(var) {
             case 1: ++v1; break;
             case 2: ++v2; break;
             case 3: ++v3; break;
             case 4: ++v4; break;
             case 5: ++v5; break;
           default: ++d; break;
         }
       }     
     }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-01-17 15:06:40

看上去我有点晚了,但既然你接受的回答更多的是评论而不是解决方案,我还是会发这篇文章。

下面是您提供的代码的简单偏差,但是达到了预期的结果!

我会带你经历这一切:

代码语言:javascript
复制
public class MyTest {
    public static void main(String[] args) {

        //First of all, we need an instance of an Election-type object, so
        //that we can call its methods and get votes from users.
        Election e = new Election();
        //Now we can easily call the method getVotes(), as defined in Election class.
        //What happens here, is that the program will 'jump' to the getVotes() method
        //and it will execute every line of code in that method. Then it will
        //'return' to where it 'left off' in the main() method. Since getVotes()
        //is of type 'void', it will not return anything. It will just 'jump' back.
        e.getVotes();

        //Now, you can use testResult() method, to see the values of the variables.
        e.testResult();
    }
}

现在,让我们看一下类Election以及它是如何工作的。

代码语言:javascript
复制
public class Election {
    private final int VOTES_NUM = 1;
    private int v1,v2,v3,v4,v5,d;

    public Election() {
        v1=v2=v3=v4=v5=d=0;
        //print now, just to show that all variables = 0
        testResult();
    }

    //Simple method that prints value of each variable. We use this for testing
    public void testResult(){
        System.out.println("v1 = "+v1);
        System.out.println("v2 = "+v2);
        System.out.println("v3 = "+v3);
        System.out.println("v4 = "+v4);
        System.out.println("v5 = "+v5);
        System.out.println("d = "+d);
    }

    private int getInput(){
       //First of all, we need a Scanner to take user input. 
       //You do that in your own code too. We simply move it in this method instead.
        Scanner input = new Scanner(System.in);

        //You also need variable to hold the user input. 
        //(Always give meaningful names to all entities)
        int userInput;

        System.out.print("Please enter vote number here: ");

        //the next part has to be in a try-catch block, 
        //to avoid exceptions like InputMismatchException, etc..
        try{
            //Get user input
            userInput = input.nextInt();
        }
        //If user enters letter, or symbol, or something else that isn't an integer,
        //then inform them of the mistake they made and recursively call this method,
        //until they get it right!
        catch (InputMismatchException ime){
            System.out.println("Please enter only a single number");
            return getInput();
        }

        //If all goes well, return the user input
        return userInput;
    }

    public void getVotes() {
        //'VOTES_NUM' is a constant that defines the times the 
        //loop will iterate (like Macros in 'C')
        for(int x=0; x<VOTES_NUM; x++)
            int n = getInput();
        //then let the switch statement increment one of the variables
        switch(userInput) {
            case 1: ++v1; break;
            case 2: ++v2; break;
            case 3: ++v3; break;
            case 4: ++v4; break;
            case 5: ++v5; break;
            default: ++d; break;
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2019-01-17 14:21:35

我想你发布的密码不见了。您发布的代码工作正常,只有在我在input.close()之前编写obj.getVotes()时,我才实现了异常。当您想关闭扫描器时,您应该在代码完成后进行此操作。因此,如果在obj.getVotes()之后关闭输入,则不应该出现任何错误。

票数 1
EN

Stack Overflow用户

发布于 2019-01-17 13:59:17

我试着在一个简短的主类中运行您的代码,没有任何异常。我就是这样运行您的方法的:

代码语言:javascript
复制
public static  void main(String...args){
    Election election = new Election(10);
    election.getVotes();
    System.out.println(election.v1);
    System.out.println(election.v2);
    System.out.println(election.v3);
    System.out.println(election.v4);
    System.out.println(election.v5);
    System.out.println(election.d);
}

我的输入是1 2 3 4 5 6 7 1 2,控制台输出是:

代码语言:javascript
复制
2 // v1
3 // v2
1 // v3
1 // v4
1 // v5
2 // d

我对你的节目做了个小小的改动。在getVotes()方法中的for循环中,我将条件更改为i<n (而不是发布的代码中的i<1 )。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54237280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档