1

So in this section of my program I'm trying to make the program re-ask for input from the user.

The problem is that is says the int have already been declared. But how do I get the input for the question again?

Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter possible and actual points for participation: ");
int pparticipation = keyboard.nextInt();
int aparticipation = keyboard.nextInt();

while (aparticipation > pparticipation || pparticipation < 0){
   System.out.println("Please enter possible and actual points for participation: ");
   int pparticipation = keyboard.nextInt();
   int aparticipation = keyboard.nextInt();
}
4

3 回答 3

2

您两次声明了变量。从循环中的变量中删除“int”应该让它工作。

int aparticipation; 那就是声明一个变量。之后为变量赋值,你只需 aparticipation = keyboard.nextInt();

您已经声明了它,因此您不必再次告诉编译器它是一个 int。

于 2013-03-02T02:10:20.017 回答
1

发生错误是因为您试图在循环中再次声明 pparticipation 和 participation。从这两个变量前面删除类型 (int)。

于 2013-03-02T02:11:30.137 回答
0

您所要做的就是更改以下内容:-

while (aparticipation > pparticipation || pparticipation < 0){
   System.out.println("Please enter possible and actual points for participation: ");
   int pparticipation = keyboard.nextInt();
   int aparticipation = keyboard.nextInt();
}

while (aparticipation > pparticipation || pparticipation < 0){
   System.out.println("Please enter possible and actual points for participation: ");
   pparticipation = keyboard.nextInt();
   aparticipation = keyboard.nextInt();
}

变量已经被声明了,所以不要再这样做了。

于 2013-03-02T02:18:39.283 回答