Java問題1(国別コース)

7085 ワード

package TEST;

public class ex01 {
	public static void main(String[] args) {
		int[] randNum = new int[10];
		int sum = 0;
		double avg;
		
		for (int i = 0; i < randNum.length; i++) {
			randNum[i] = (int)Math.floor(Math.random()*10+1);
			sum += randNum[i];
		}
		avg = (double)sum/randNum.length;
		
		System.out.print("랜덤한 정수들 : ");
		for (int i : randNum) {
			System.out.print(i+" ");
		}
		System.out.printf("\n평균은 %.1f",avg);
		
	}
}
package TEST;

import java.util.Scanner;

public class ex02 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int getArrayRange, count = 0;
		int[] randNum;
		
		
		while(true){
			System.out.print("정수 몇개?(100보다 작은 개수) ");
			getArrayRange = scanner.nextInt();
			if(getArrayRange > 99) continue;
			randNum = new int[getArrayRange];
			break;
		}
		
		for (int i = 0; i < randNum.length; i++) {
			randNum[i] = (int)Math.floor(Math.random()*100+1);
			for (int j = 0; j < i; j++) {
				if(randNum[j] == randNum[i]){
					i--;
					break;
				}
			}
		}
		
		for (int i = 0; i < randNum.length; i++) {
			System.out.printf("%3d ",randNum[i]);
			count++;
			if(count == 10){
				System.out.println();
				count = 0;
			}
		}
		
		scanner.close();
		
	}
}
package TEST;

public class ex03 {
	public static void main(String[] args) {
		int[][] randNum = new int[4][4];
		
		for (int i = 0; i < randNum.length; i++) {
			for (int j = 0; j < randNum[0].length; j++) {
				randNum[i][j] = (int)Math.floor(Math.random()*10+1);
				System.out.printf("%2d ",randNum[i][j]);
			}
			System.out.println();
		}
		
	}
}
package TEST;

public class ex04 {
	public static void main(String[] args) {
		int[][] randNum = new int[4][4];
		
		for (int i = 0; i < 10; i++) {
			int num1 = (int)Math.floor(Math.random()*randNum.length);
			int num2 = (int)Math.floor(Math.random()*randNum[0].length);
			if(randNum[num1][num2] != 0){
				i--;
				continue;
			}
			randNum[num1][num2] = (int)Math.floor(Math.random()*10+1);
		}
		
		for (int i = 0; i < randNum.length; i++) {
			for (int j = 0; j < randNum[0].length; j++) {
				System.out.printf("%2d ",randNum[i][j]);
			}
			System.out.println();
		}
		
	}
}
package TEST;

import java.util.Scanner;

public class ex05 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String course[] = {"Java", "C++","HTML5", "컴퓨터구조","안드로이드"};
		boolean check = false;
		int score[] = {95, 88, 76, 62, 55};
		
		while(true){
			System.out.print("과목 이름>> ");
			String subject = scanner.next();
			if(subject.equals("그만")) break;
			for (int i = 0; i < course.length; i++) {
				if(course[i].equals(subject)){
					System.out.println(course[i]+"의 점수는 "+ score[i]);
					check = true;
					break;
				}
			}
			if(!check){
				System.out.println("없는 과목 입니다");
			}
			
		}
		scanner.close();
		
	}
}
package TEST;

public class TV {
	private String company;
	private int year, inch;
	
	public TV(String company, int year, int inch){
		this.company = company;
		this.year = year;
		this.inch = inch;
	}
	public void show() {
		System.out.println(company +"에서 만든 "+year+"년형 "+inch+"인치 TV");
	}
	
}


package TEST;


public class ex06 {
	public static void main(String[] args) {
		TV myTv = new TV("LG", 2017, 32);
		myTv.show();
	}
}
package TEST;

public class Grade {
	private int math, science, english;
	
	public Grade(int math, int science, int english){
		this.math = math;
		this.science = science;
		this.english = english;
	}
	
	public int average(){
		return (math+science+english)/3;
	}
}


package TEST;

import java.util.InputMismatchException;
import java.util.Scanner;

public class ex07 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		while(true) {
			try {
				System.out.print("수학, 과학, 영어 순으로 3개의 점수 입력 >> ");
				int math = scanner.nextInt();
				int sience = scanner.nextInt();
				int english = scanner.nextInt();
				Grade me = new Grade(math, sience, english);
				System.out.println("평균은 "+me.average());	
				break;
			} catch (InputMismatchException e) {
				System.out.println("정수를 입력해주세요!");
				scanner = new Scanner(System.in);
			}	
		}
		
		scanner.close();
	}
}
package TEST;

public class Song {
	private String title, artist, country;
	private int year;
	
	public Song(){};
	//오버로딩
	public Song(String title, String artist, int year, String country){
		this.title = title;
		this.artist = artist;
		this.year = year;
		this.country = country;
	};
	
	public void show(){
		System.out.println(year+"년 "+country+"국적의 "+artist+"가 부른 "+title);
	}
}


package TEST;


public class ex08 {
	public static void main(String[] args) {
		Song song = new Song("Dancing Queen", "ABBA", 1978, "스웨덴");
		song.show();
	}
}
package TEST;

public class Reactangle {
	private int x, y, width, height;
	
	public Reactangle(int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	public int square() {
		return width*height;
	}
	
	public void show() {
		System.out.printf("(%d,%d)에서 크기가 %dx%d인 사각형\n",x,y,width,height);
	}
	
	public boolean contains(Reactangle r) {
		if( (r.x > x && (r.x+r.width) < (x+width)) && (r.y > y && (r.y + r.height) < (y + height)))
			return true;
		else return false;
	}
}


package TEST;

public class ex09 {
	public static void main(String[] args) {
		Reactangle r = new Reactangle(2, 2, 8, 7);
		Reactangle s = new Reactangle(5, 5, 6, 6);
		Reactangle t = new Reactangle(1, 1, 10, 10);
		
		r.show();
		System.out.println("s의 면적은 "+s.square());
		if(t.contains(r)) System.out.println("t는 r을 포함합니다.");
		if(t.contains(s)) System.out.println("t는 s를 포함합니다.");
	}
}