2016-10-05 44 views
0

嗨,那里我是超新的编码,当我尝试运行下面的代码时,我总是收到'.class'错误。我错过了什么?在java中的'.class错误'

import java.util.Scanner; 

import java.util.Scanner; 


public class PeopleWeights { 
    public static void main(String[] args) { 
     Scanner scnr = new Scanner (System.in); 
     userWeight = new int[5]; 
     int i = 0; 

     userWeight[0] = 0; 
     userWeight[1] = 5; 
     userWeight[2] = 6; 
     userWeight[3] = 7; 
     userWeight[4] = 9; 

     System.out.println("Enter weight 1: "); 
     userWeight = scnr.nextInt[]; 

     return; 
    } 
} 
+4

“'userWeight = scnr.nextInt [];'” - 这些是括号中的错误类型。使用'()'。这将解决你的一个问题。 – resueman

回答

0

首先不要多次导入包,现在让我们转到实际的“错误”。

这里:

import java.util.Scanner; 

public class PeopleWeights { 
    public static void main(String[] args) { 
     Scanner scnr = new Scanner (System.in); 
     int userWeight[] = new int[5];//You need to declare the type 
     //of a variable, in this case its int name[] 
     //because its an array of ints 
     int i = 0; 

     userWeight[0] = 0; 
     userWeight[1] = 5; 
     userWeight[2] = 6; 
     userWeight[3] = 7; 
     userWeight[4] = 9; 

     System.out.println("Enter weight 1: "); 
     userWeight[0] = scnr.nextInt();//I belive that you wanted to change 
     // the first element of the array here. 
     //Also nextInt() is a method you can't use nextInt[] 
     //since it doesn't exists 
     //return; You dont need it, because the method is void, thus it doesnt have to return anything. 

    } 

} 

代替,这也:

userWeight[0] = 0; 
userWeight[1] = 5; 
userWeight[2] = 6; 
userWeight[3] = 7; 
userWeight[4] = 9; 

可以数组的声明中这样做:

int userWeight[] = {0,5,6,7,9};//instantiate it with 5 integers 
1

这是问题

userWeight = scnr.nextInt[]; 

解决这个由:

userWeight[0] = scnr.nextInt();  //If you intended to change the first weight 

OR

userWeight[1] = scnr.nextInt();  //If you intended to change the value of userWeight at index 1 (ie. the second userWeight) 

应工作

PS:作为预防措施不导入Scanner类的两倍。做一次就足够了

+0

导入一次不是“预防措施”,只是一次清理。 –

0

我明白你的内涵及以下两种可能的方式来实现你的想法:

我看你是手动给值userWeight [0] = 0; 如果你想手动提供,我建议不要像下面那样使用扫描仪。

public static void main(String[] args) { 
    int[] userWeight={0, 5, 6,7,9}; 
     System.out.println("Weights are" +userWeight);//as you are giving values. 
} 

如果你的内涵是在运行时或从用户得到的值,请按以下方法

public static void main(String[] args) { 
     Scanner sc=new Scanner(System.in); 
     System.out.println("This is runtime and you need to enter input"); 

     int[] userWeight = new int[5]; 

      for (int i= 0; i < userWeight.length; i++) { 
       userWeight[i] = sc.nextInt(); 
       System.out.println(userWeight[i]); 
      } 
     } 

PS:

我使用的是util包导入两次看出,相反,您可以一次导入全部导入java.util。*;

此外,您正在尝试返回。请注意,无效方法不需要返回值。 VOID除了没有任何回报。