我是Java编程的新手,我试图学习Java编程中类和对象的用法,同时编写以下代码,但出现了异常
java.util.NoSuchElementException
用于示例输入
5
1 2 3 4 5
第一行包含元素数(在本例中为5),下一行包含元素。
在Election类的for循环内输入时,我遇到异常。
我尝试在堆栈溢出和其他资源上进行搜索,但是仍然不知道如何删除此异常。
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;
}
}
}
}
答案 0 :(得分:1)
我认为您发布的代码丢失。您发布的代码正常运行,并且只有在obj.getVotes()之前编写了input.close()时,我才能获得异常。当您要关闭扫描仪时,应在代码完成后执行此操作。因此,如果在obj.getVotes()之后关闭输入,则不会出现任何错误。
答案 1 :(得分:1)
看起来我来晚了,但是由于您接受的答案更多是评论而不是解决方案,因此无论如何我都会发布。
这只是您提供的代码的一个简单偏差,但达到了预期的结果!
我将指导您完成此操作
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
及其工作方式。
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;
}
}
}
答案 2 :(得分:0)
我尝试在简短的主类中运行您的代码,但没有任何异常。这是我运行您的方法的方式:
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 2,控制台输出是:
2 // v1
3 // v2
1 // v3
1 // v4
1 // v5
2 // d
我确实对您的程序做了一些小的更改。在getVotes()
方法内的for循环中,我将条件更改为i<n
(而不是您发布的代码中的i<1
)