Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

Em Viết Code Quản Lý Sv Mà Không Biết Cái Đoạn Cuối Tại Sao Lại Sai, Ai Giúp Em Sửa Lỗi Đc Không Ạ ?

Discussion in 'Java Project' started by Xuân Quyền, 29/9/19.

  1. Xuân Quyền

    Xuân Quyền New Member

    import java.util.Scanner;
    public class DemoQuanLySV {
    public class Student {
    //Khai báo thuộc tính
    String name;
    int age;
    float cMark, javaMark;
    float TBMark;
    float avg()
    {
    return (this.cMark+this.javaMark)/2;
    }
    void input(){
    //Nhập các thuộc tính từ bàn phím
    Scanner scanner = new Scanner(System.in);
    System.out.print("Nhap ten sinh vien:");
    this.name = scanner.nextLine();
    System.out.print("Nhap tuoi: ");
    this.age = scanner.nextInt();
    System.out.print("Nhap diem C/C++ : ");
    this.cMark = scanner.nextFloat();
    System.out.print("Nhap diem Java: ");
    this.javaMark = scanner.nextFloat();
    this.TBMark = this.avg();
    }
    void output() {
    //Xuất,in các thuộc tính ra màn hình.
    System.out.print("Ten sinh vien: "+this.name);
    System.out.print("Tuoi: "+this.age);
    System.out.print("Diem C/C++:"+this.cMark);
    System.out.print("Diem Java: "+this.javaMark);
    System.out.print("Diem TB: "+this.TBMark);
    }
    }
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n,size = 100;
    Student[] SV = new Student[size];
    do {
    System.out.print("Nhap so sinh vien can quan ly: ");
    n = scanner.nextInt();
    } while (n<1 || n>size-1);
    for (int i=0;i<=n;i++) {
    Student sinhvien = new Student();
    sinhvien.input();
    SV.output();
    }
    }
    }
     
  2. Phạm Huy Thiên

    Phạm Huy Thiên New Member

    Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author SystemError
     */
    import java.util.Scanner;
     
    public class test {
     
    	private static Scanner scanner = new Scanner(System.in);
     
    	public static void main(String[] args) {
     
    		int n, size = 100;
    		Student[] SV = new Student[size];
    		do {
    			System.out.print("Nhap so sinh vien can quan ly: ");
    			n = scanner.nextInt();
    		} while (n < 1 || n > size - 1);
     
    		for (int i = 0; i <= n; i++) {
    			SV[i] = new Student();
    			SV[i].input();
    			SV[i].output();
    		}
    	   
    		scanner.close();
    	}
     
    }
     
    class Student {
    //Khai báo thuộc tính
     
    	private Scanner scanner = new Scanner(System.in);
     
    	String name;
    	int age;
    	float cMark, javaMark;
    	float TBMark;
     
    	float avg() {
    		return (this.cMark + this.javaMark) / 2;
    	}
     
    	void input() {
    //Nhập các thuộc tính từ bàn phím
    		System.out.print("Nhap ten sinh vien:");
    		this.name = scanner.nextLine();
    		System.out.print("Nhap tuoi: ");
    		this.age = scanner.nextInt();
    		System.out.print("Nhap diem C/C++ : ");
    		this.cMark = scanner.nextFloat();
    		System.out.print("Nhap diem Java: ");
    		this.javaMark = scanner.nextFloat();
    		this.TBMark = this.avg();
    	}
     
    	void output() {
    //Xuất,in các thuộc tính ra màn hình.
    		System.out.print("Ten sinh vien: " + this.name);
    		System.out.print("Tuoi: " + this.age);
    		System.out.print("Diem C/C++:" + this.cMark);
    		System.out.print("Diem Java: " + this.javaMark);
    		System.out.print("Diem TB: " + this.TBMark);
    	}
    }
    
    code trên lỗi là vì khai báo 1 đằng dùng 1 nẻo thôi bạn copy code trên so sánh rồi tự đối chiếu là biết ^^
    mình cũng dang học jAVA rất mong được làm quen học hỏi ^^
     
    Last edited: 30/9/19
  3. nguyenha7

    nguyenha7 New Member

    Sai thì phải báo sai thế nào? lỗi khúc nào thì mọi người mới giúp nhanh được bạn.
     
  4. Joe

    Joe Thành viên VIP

    You made 3 cardinal coding errors which tell me that you are a newbie.
    1. you run an IDE which is usually leading to confusion. Here: Student is an internal class of class DemoQuanlySV and when the main() invokes new Student() it expects a static class, not a non-static class.
    2. SV is an array and its elements are null (not initialized).
    3. The invocation SV.output() is wrong because SV is an (uninitialized) array.
    If you compile your codes on a CMD window you may get this:
    Code:
    C:\links\java\test>javac -g:none -d ./classes DemoQuanlySV.java
    DemoQuanlySV.java:44: error: non-static variable this cannot be referenced from a static context
    Student sinhvien = new Student();
    				   ^
    DemoQuanlySV.java:46: error: cannot find symbol
    SV.output();
      ^
      symbol:   method output()
      location: variable SV of type DemoQuanlySV.Student[]
    2 errors
     
    C:\links\java\test>
    
    the line DemoQuanlySV.java:44:... says the line 44 contains the error. The same with line DemoQuanlySV.java:46:
    By the way: be careful with Scanner in mixed modes between numbers and lines.
     
    Last edited: 15/11/19
  5. Joe

    Joe Thành viên VIP

    @Phạm Huy Thiên
    Well done by moving out the Student class. But there's a bug: the line for "Nhap so sinh vien can quan ly:" takes a number, for example: 1. The line for (int i = 0; i <= n; i++) starts at 0 and ends at 2. Meaning: not 1, but 2 students because 0, 1 are taken before i = 2. Remember: Java and all other computer languages always start at 0 (zero).
     

Chia sẻ trang này

Loading...