Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 3 của 3
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Thắc mắc về mảng một chiều

    Mình tạo mảng một chiều với kiểu dữ liệu là Candidate, nhưng mình không thể nhập giá trị vào mảng được. Mong các bạn giúp đỡ [IMG]images/smilies/smile.png[/IMG]


    Mã:
    import java.util.Scanner; class Candidate{    private String name;    private float Maths;    private float Physics;    private float Chemistry;            public void Candidate(){        Scanner input = new Scanner(System.in);                System.out.print("Name: ");        name = input.nextLine();        System.out.print("Maths: ");        Maths = input.nextFloat();        System.out.print("Physics: ");        Physics = input.nextFloat();        System.out.print("Chemistry: ");        Chemistry = input.nextFloat();    }            public void Info(){        System.out.println("Name: " + name);        System.out.println("Maths: " + Maths);        System.out.println("Physics" + Physics);        System.out.println("Chemistry: " + Chemistry);        System.out.println("Total: " + Total());    }        public int Total(){        return (int) (Maths + Physics + Chemistry);    }} public class ThiSinh{       public static void main(String[] args){        Scanner input = new Scanner(System.in);                int n = input.nextInt();        Candidate[] a;                a= new Candidate[n];                for (int i = 0; i < n; i++)            a[i].Candidate();                System.out.println();                for (int i = 0; i < n; i++){            a[i].Info();            System.out.println();        }                System.out.println("Successful candidates: ");        for (int i = 0; i < n; i++){            if (a[i].Total() >= 18){                a[i].Info();                System.out.println();            }        }    }}

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Lỗi phát sinh do phần tử thứ a[i] chưa đc khởi tạo
    bạn tự thêm vài lệnh để biết đang nhập cái n nha

    Mã:
    import java.util.Scanner; class Candidate{    private String name;    private float Maths;    private float Physics;    private float Chemistry;            public void Candidate(){        Scanner input = new Scanner(System.in);                System.out.print("Name: ");        name = input.nextLine();        System.out.print("Maths: ");        Maths = input.nextFloat();        System.out.print("Physics: ");        Physics = input.nextFloat();        System.out.print("Chemistry: ");        Chemistry = input.nextFloat();    }            public void Info(){        System.out.println("Name: " + name);        System.out.println("Maths: " + Maths);        System.out.println("Physics" + Physics);        System.out.println("Chemistry: " + Chemistry);        System.out.println("Total: " + Total());    }        public int Total(){        return (int) (Maths + Physics + Chemistry);    }} public class ThiSinh{       public static void main(String[] args){        Scanner input = new Scanner(System.in);                int n = input.nextInt();        Candidate[] a;                a= new Candidate[n];//Khai báo mảng a, và khởi tạo n phần tử có kiểu dữ liệu Candidate                for (int i = 0; i < n; i++){            a[i]=new Candidate();//Khởi tạo đối tượng a[i]            System.out.println("Number "+(i+1));            a[i].Candidate();        }                System.out.println();                for (int i = 0; i < n; i++){            a[i].Info();            System.out.println();        }                System.out.println("Successful candidates: ");        for (int i = 0; i < n; i++){            if (a[i].Total() >= 18){                a[i].Info();                System.out.println();            }        }    }}

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mình cảm ơn bạn [IMG]images/smilies/smile.png[/IMG]

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •