//Test01.java
//로또1~45까지6개
//난수를발생시켜6개의수를배열에넣어출력하시오
//@@단, 중복된숫자가배열에들어가면안됨
//배열출력(로또번호출력)
//int r = (int)(Math.random()*45)+1;
int[] lotto = new int[6];
int n=0;
while(n<lotto.length) {
int r = (int)(Math.random()*45)+1;
lotto[n]=r
//배열에중복된값이있는지조사, 있으면넣은값빼기
for(int i=0;i<ni++) {
if(r==lotto[i]) {
n--;//중복제거
break
}
}
n++;
}//while
for(int num:lotto) {
System.out.println(num);
}
//Student.java
package com.day03;
public class Student {
int studentID, grade;
String name;
public Student() {}
public Student(String name, int StudentID, int grade) {
this.studentID=StudentID;
this.grade=grade;
this.name=name;
}
public Student(int StudentID, String name, int grade) {
//이렇게도 쓸 수 있다
this(name,studentID,grade);
}
public void study() {
System.out.println(name+" 공부하다");
}
public void play() {
System.out.println(name+" 운동하다");
}
public static void main(String[] args) {
Student s1 = new Student();
s1.studentID=100;
s1.grade=1;
s1.name="홍길동";
s1.study(); //홍길동 공부하다
Student s2 = new Student("이순신",200,3);
s2.study(); //이순신 공부하다
Student s3 = new Student(300,"강감찬",4);
s3.study();//강감찬 공부하다
s3.play();//강감찬 운동하다
}
}
//StudentScore.java
package com.day03;
public class StudentScore {
String name;
int kor,math;
public StudentScore() {}
public StudentScore(String name, int kor, int math) {
this.name=name;
this.kor=kor;
this.math=math;
}
public int getTotal() {
return kor+math;
}
public float getAvg() {
//return (kor+math)/2.0f;
return getTotal()/2.0f;
}
public static void main(String[] args) {
StudentScore s1 = new StudentScore("홍길동",100,85);
StudentScore s2 = new StudentScore("이순신",50,70);
System.out.println("이름:"+s1.name);
System.out.println("국어:"+s1.kor);
System.out.println("수학:"+s1.math);
System.out.println("총점:"+s1.getTotal());
System.out.println("평균:"+s1.getAvg());
}
}