我写了一个Java程序但是我收到了错误:
ArrayMain.java:13: error: cannot find symbol
ar[c]=input.nextInt;
^
symbol: variable nextInt
location: variable input of type Scanner
代码:
import java.util.Scanner;
class ArrayMain
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter the size:");
int c=input.nextInt();
int[] ar=new int[c];
System.out.println("Enter Element");
for(int i=0;i<c;i++)
{
ar[c]=input.nextInt;
}
array(ar,c);
}
public static void array(int[] ar,int c);
{
System.out.println("Elements in reverse order are");
for(int i=c;i>0;i--);
{
System.out.println(ar[c-1]);
}
}
}
我该如何解决?
答案 0 :(得分:0)
应该是
ar[c]=input.nextInt();
并确保方法定义正确
public static void array(int[] ar,int c) { //notice the removal of semicolon
答案 1 :(得分:0)
ar[c] = input.nextInt;
这是错误的,正确的代码是:
ar[c] = input.nextInt();
答案 2 :(得分:0)
你应该在i
中放置索引c
而不是ar[c]
来循环每个输入元素
System.out.println("Enter Element");
for(int i=0;i<c;i++)
{
ar[i]=input.nextInt(); //not input.nextInt
}
方法array(int[] ar,int c)
也有问题:
public static void array(int[] ar,int c) // method decleration shouldn't end with `;`
{
System.out.println("Elements in reverse order are");
// for loop mustn't end with ';' for(int i=c;i>0;i--);
for(int i=c-1;i>=0;i--) // start at last element index to the first element which index is i=0
{
System.out.println(ar[i]); // loop over each element in reverse order
}
}