如何在java中解决这个扫描方法错误?

时间:2014-07-26 08:06:58

标签: java

不工作...... 如何删除错误.. 请帮我 怎么做

它给我扫描仪扫描方法的错误 我无法解决这个问题......

如何使用此扫描仪类

import java.util.*;

public class A
{

  class Student
  {

    int i,id,age;
    int m[]=new int[3];
    int tot=0,per;
    String name;


    Scanner scan=new Scanner(System.in);

    Student(int id,String name,int age)
    {
      this.id=id;
      this.name=name;
      this.age=age;
    }

    public void getMarks()
    {
      System.out.println("Enter UR marks-- ");

      for(i=0;i<3;i++)
      {
        System.out.print("\n Enter Marks For Sub "+i);
        m[i]=scan.nextInt();
        tot=tot+m[i];
      }
    }

    void display()
    {
      System.out.println("ID-- "+id);
      System.out.println("Name-- "+name);
      System.out.println("Age-- "+age);
      System.out.println("Total-- "+tot);
      System.out.println("Percentage-- "+(per/i));

    }

  }

  public static void main(String[] args)
  {
    Student s1=new Student(101,"jay",20);
    s1.getMarks();
    s1.display();
  }

}

1 个答案:

答案 0 :(得分:0)

你走了:

import java.util.*;

class Student {

int i, id, age;
int m[] = new int[3];
int tot = 0, per;
String name;



Student(int id, String name, int age) {
    this.id = id;
    this.name = name;
    this.age = age;
    }

public void getMarks() {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter UR marks-- ");

    for (i = 0; i < 3; i++) {
        System.out.print("\n Enter Marks For Sub " + i);
        m[i] = scan.nextInt();
        tot = tot + m[i];
      }
   }

void display() {
    System.out.println("ID-- " + id);
    System.out.println("Name-- " + name);
    System.out.println("Age-- " + age);
    System.out.println("Total-- " + tot);
    System.out.println("Percentage-- " + (per / i));

    }

}

public class A {

public static void main(String[] args) {

    Student s1 = new Student(101, "jay", 20);
    s1.getMarks();
    s1.display();
    }

}

一些提示: 1)除非需要,否则不要将类声明为其他类的成员。 (正如你所做的那样,class Student class A内。 2)声明实例化方法中要使用它的Scanner对象。将其声明为类成员并不能用于任何目的。 3)使用后始终关闭扫描仪。即scan.close()

访问以下链接:

  1. When to use nested classes
  2. Using Scanner in java