1번
import java.util.Scanner;
import java.util.Vector;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Vector<Integer> ve = new Vector<Integer>();
System.out.print("정수(-1이 입력 될 때 까지)>> ");
int max = 0;
while(true){
int n = sc.nextInt();
if(n == -1) break;
ve.add(n);
if(max < n){
max = n;
}
}
System.out.print("가장 큰 수는 " + max);
}
}
2번
import java.util.Scanner;
import java.util.ArrayList;
public class main {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
System.out.print("6개의 학점을 빈 칸으로 분리 입력(A/B/C/D/F)>>");
int sum = 0;
for(int i = 0; i < 6; i++){
String b = sc.next();
arr.add(b);
char a = b.charAt(0);
if(a == 'A'){
sum += 4;
}
else if(a == 'B'){
sum += 3;
}
else if (a == 'C') {
sum += 2;
}
else if(a=='D'){
sum += 1;
}
}
System.out.print((double)sum / (double)arr.size());
}
}
3번
import java.util.Scanner;
import java.util.HashMap;
public class main {
public static void main(String[] args) {
HashMap<String, Integer> nations = new HashMap<>();
Scanner sc = new Scanner(System.in);
System.out.println("나라 이름과 인구를 입력하세요.(예 : Korea 5000)");
while(true){
System.out.print("나라 이름, 인구 >>");
String temp1 = sc.next();
if(temp1.equals("그만")) break;
int temp2 = sc.nextInt();
nations.put(temp1, temp2);
}
while(true){
System.out.print("인구 검색>>");
String temp = sc.next();
if(temp.equals("그만")) break;
if(nations.get(temp) == null){
System.out.println(temp + " 나라는 없습니다.");
}
else {
System.out.println(temp + "의 인구는 " + nations.get(temp));
}
}
}
}
4번
import java.util.Scanner;
import java.util.Vector;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Vector<Integer> ve = new Vector<>();
while(true){
System.out.print("강수량 입력 (0 입력시 종료) >> ");
int temp = sc.nextInt();
if(temp == 0) break;
ve.add(temp);
int sum = 0;
for(int i = 0; i < ve.size(); i++){
System.out.print(ve.get(i) + " ");
sum += ve.get(i);
}
System.out.println("\n현재 평균 " + sum / ve.size());
}
}
}
5번
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
class Student{
private String name, major, id, grade;
public Student() {}
public Student(String name, String major, String id, String grade) {this.name = name; this.major = major; this.id = id; this.grade = grade;}
public String getName() {return name;}
public String getMajor() {return major;}
public String getId() {return id;}
public String getGrade() {return grade;}
public void find_student(String name) {
if(name.equals(this.name)) {
System.out.println(this.name+", "+this.major+", "+this.grade+", "+this.grade);
}
}
}
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("학생 이름, 학과, 학점 평균을 입력하세요.");
ArrayList<Student> arr = new ArrayList<>();
for(int i = 0; i < 4; i++){
System.out.print(">> ");
String s = sc.nextLine();
StringTokenizer st = new StringTokenizer(s, ",");
String name = st.nextToken();
String major = st.nextToken();
String id = st.nextToken();
String grade = st.nextToken();
arr.add(new Student(name, major, id, grade));
}
System.out.println("----------------------");
for(int i = 0; i < 4; i++){
System.out.println("이름: " + arr.get(i).getName());
System.out.println("학과: " + arr.get(i).getMajor());
System.out.println("학번: " + arr.get(i).getId());
System.out.println("학점평균: " + arr.get(i).getGrade());
System.out.println("----------------------");
}
while (true){
System.out.println("학생 이름 >>");
String name = sc.nextLine();
if(name.equals("그만")) break;
for(int i = 0; i < arr.size(); i++){
Student s = arr.get(i);
s.find_student(name);
}
}
}
}
6번
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
class Location{
private int first, second;
public Location(int first, int second) {this.first = first; this.second = second;}
public void find_location(String city){System.out.println(city + " " + first + " " + second); }
}
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("학생 이름, 학과, 학점 평균을 입력하세요.");
HashMap<String, Location> hm = new HashMap<>();
for(int i = 0; i < 4; i++){
System.out.print(">>");
String str = sc.nextLine();
StringTokenizer st = new StringTokenizer(str, ",");
String name = st.nextToken();
int first = Integer.parseInt(st.nextToken().trim());
int second = Integer.parseInt(st.nextToken().trim());
hm.put(name, new Location(first, second));
}
System.out.println("----------------------------------");
Set<String> key = hm.keySet();
Iterator <String> it = key.iterator();
while(it.hasNext()){
String temp = it.next();
Location s = hm.get(temp);
s.find_location(temp);
}
System.out.println("----------------------------------");
while(true) {
System.out.print("도시 이름 >> ");
String city = sc.next();
if(city.equals("그만"))
break;
Location s = hm.get(city);
if( s == null)
System.out.println(city+"는 없습니다.");
else
s.find_location(city);
}
sc.close();
}
}
7번
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("미래장학금관리시스템입니다.");
HashMap<String, Double> hm = new HashMap<>();
for(int i = 0; i < 5; i++){
System.out.print("이름과 학점 >> ");
String name = sc.next();
double score = sc.nextDouble();
hm.put(name, score);
}
System.out.print("장학금 선발 학점 기준 입력>> ");
double fixScore = sc.nextDouble();
Set<String> key = hm.keySet();
Iterator <String> it = key.iterator();
while (it.hasNext()){
String name = it.next();
if(fixScore < hm.get(name)){
System.out.print(name + " ");
}
}
}
}
8번
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("** 포인트 관리 프로그램입니다. **");
HashMap<String, Integer> hm = new HashMap<>();
while (true) {
System.out.print("이름과 포인트 입력 >> ");
String name = sc.next();
if (name.equals("그만")) break;
int score = sc.nextInt();
if (hm.get(name) == null) {
hm.put(name, score);
} else {
hm.put(name, score + hm.get(name));
}
Set<String> key = hm.keySet();
Iterator <String> it = key.iterator();
while (it.hasNext()){
String str = it.next();
System.out.print("(" + str + "," + hm.get(str) + ")");
}
System.out.println();
}
}
}
9번
import java.util.*;
interface IStack<T> {
T pop();
boolean push(T ob);
}
class MyStack<T> implements IStack<T>{
private ArrayList<T> al;
public MyStack() {al = new ArrayList<>();}
public T pop(){
if(al.size() != 0){
return al.remove(0);
}
return null;
}
public boolean push(T ob){
al.add(0, ob);
return true;
}
}
public class main {
public static void main(String[] args) {
IStack<Integer> stack = new MyStack<Integer>();
for (int i=0; i<10; i++) stack.push(i);
while(true) {
Integer n = stack.pop();
if(n == null) break;
System.out.print(n+" ");
}
}
}
10번
import java.util.*;
abstract class Shape {
private Shape next;
public Shape() { next = null; }
public void setNext(Shape obj) { next = obj; } //링크 연결
public Shape getNext() { return next; }
public abstract void draw();
}
class Line extends Shape {
public void draw() {
System.out.println("Line");
}
}
class Rect extends Shape {
public void draw() {
System.out.println("Rect");
}
}
class Circle extends Shape {
public void draw() {
System.out.println("Circle");
}
}
public class main {
public static void main(String[] args) {
System.out.println("그래픽 에디터 beauty를 실행합니다.");
Vector<Shape> ve = new Vector<>();
Scanner sc = new Scanner(System.in);
while(true){
System.out.print("삽입(1), 삭제(2), 모두 보기(3), 종료(4)>>");
int input = sc.nextInt();
if(input == 1){
System.out.print("Line(1), Rect(2), Circle(3)>>");
int input1 = sc.nextInt();
if(input1 == 1){
ve.add(new Line());
}
else if(input1 == 2){
ve.add(new Rect());
}
else if(input1 == 3){
ve.add(new Circle());
}
else {
}
}
else if(input == 2){
System.out.print("삭제할 도형의 위치 >>");
int input1 = sc.nextInt();
if(ve.size() > input1){
if(ve.get(input1) == null){
System.out.println("삭제할 수 없습니다.");
}
else{
ve.remove(input1);
}
}
else{
System.out.println("삭제할 수 없습니다.");
}
}
else if(input == 3){
for(int i = 0; i < ve.size(); i++){
ve.get(i).draw();
}
}
else{
break;
}
}
}
}
11번
import java.util.*;
class Nation {
private String country;
private String capital;
public Nation(String country, String capital) {
this.country = country;
this.capital = capital;
}
public String getCountry() {
return country;
}
public String getCapital() {
return capital;
}
}
class CaptialQuiz{
private Vector<Nation> na;
private Scanner sc;
public CaptialQuiz() {
na = new Vector<>();
sc = new Scanner(System.in);
na.add(new Nation("멕시코", "멕시코시티"));
na.add(new Nation("스페인", "리스본"));
na.add(new Nation("프랑스", "파리"));
na.add(new Nation("영국", "런던"));
na.add(new Nation("그리스", "아테네"));
na.add(new Nation("독일", "베를린"));
na.add(new Nation("일본", "동경"));
na.add(new Nation("중국", "베이징"));
na.add(new Nation("러시아", "모스크바"));
}
public void run(){
while(true){
System.out.print("입력:1, 퀴즈:2, 종료:3>> ");
int input = sc.nextInt();
if(input == 1){
insert();
}
else if(input == 2){
play();
}
else if(input == 3){
break;
}
else {
System.out.println("잘못 된 입력입니다.");
}
}
}
public void insert(){
System.out.println("현재 " + na.size() + "개 나라와 수도가 입력되어 있습니다.");
while(true){
System.out.print("나라와 수도 입력" + (int)(na.size() + 1) + ">>");
String first = sc.next();
if(first.equals("그만")) break;
String second = sc.next();
boolean isIn = false;
for(int i = 0; i < na.size(); i++){
if(na.get(i).getCountry().equals(first)){
isIn = true;
break;
}
}
if(isIn){
System.out.println(first + "는 이미 있습니다!");
continue;
}
na.add(new Nation(first, second));
}
}
public void play(){
while (true){
int index = (int)(Math.random() * na.size());
System.out.print(na.get(index).getCountry() + "의 수도는?");
String inputs = sc.next();
if(inputs.equals("그만")) break;
if(inputs.equals(na.get(index).getCapital())){
System.out.println("정답!");
}
else{
System.out.println("아닙니다!!");
}
}
}
}
public class main {
public static void main(String[] args){
CaptialQuiz cq = new CaptialQuiz();
cq.run();
}
}
12번
import java.util.*;
class Word {
private String eng;
private String kor;
public Word(String eng, String kor) {
this.eng = eng;
this.kor = kor;
}
public String getEng() {
return eng;
}
public String getKor() {
return kor;
}
}
class EngQuiz{
private Vector<Word> ve;
private Scanner sc;
public EngQuiz() {
ve = new Vector<>();
sc = new Scanner(System.in);
ve.add(new Word("love", "사랑"));
ve.add(new Word("animal", "동물"));
ve.add(new Word("emotion", "감정"));
ve.add(new Word("human", "인간"));
ve.add(new Word("stock", "주식"));
ve.add(new Word("trade", "거래"));
ve.add(new Word("society", "사회"));
ve.add(new Word("baby", "아기"));
ve.add(new Word("honey", "꿀"));
ve.add(new Word("dall", "인형"));
ve.add(new Word("bear", "곰"));
ve.add(new Word("picture", "사진"));
ve.add(new Word("painting", "그림"));
ve.add(new Word("fault", "오류"));
ve.add(new Word("example", "보기"));
ve.add(new Word("eye", "눈"));
ve.add(new Word("statue", "조각상"));
}
void run(){
System.out.println("**** 영어 단어 테스트 프로그램 \"명품 영어\"입니다. ****");
while (true){
try {
System.out.print("단어 테스트:1, 단어 삽입:2, 종료:3>> ");
int input = sc.nextInt();
if(input == 1){
play();
}
else if(input == 2){
insert();
}
else if(input == 3){
break;
}
else{
System.out.println("잘못된 입력 다시 입력해주세요.");
}
}
catch (Exception ex) {
System.out.println("잘못된 입력");
String buffer = sc.nextLine();
continue;
}
}
}
void play(){
System.out.println("현재 " + ve.size() + "개의 단어가 들어 있습니다. -1을 입력하면 종료합니다.");
while(true){
int[] arr = {-1, -1, -1, -1};
int arrRandInt = (int)(Math.random() * 4);
int randInt = (int)(Math.random()*ve.size());
arr[arrRandInt] = randInt;
System.out.println(ve.get(randInt).getEng() + "?");
for(int i = 0; i < arr.length; i++){
if(arr[i] != -1 || arr[i] == arrRandInt){
continue;
}
else{
int tempRand = (int)(Math.random() * ve.size());
for(int j = 0; j < i; j++){
if(tempRand == arr[j]){
j = 0;
tempRand = (int)(Math.random() * ve.size());
}
}
arr[i] = tempRand;
}
}
for(int j = 0; j < arr.length; j++){
System.out.print("(" + (int)(j+1) + ")" + ve.get(arr[j]).getKor() + " ");
}
System.out.print(":>");
int n = sc.nextInt();
if(n == (int)(arrRandInt + 1)){
System.out.println("Exellent!");
}
else if(n == -1){
break;
}
else {
System.out.println("No!");
}
}
}
void insert(){
System.out.println("영어 단어에 그만을 입력하면 입력을 종료합니다.");
while (true){
System.out.print("영어 한글 입력 >>");
String eng = sc.next();
if(eng.equals("그만")) break;
String kor = sc.next();
ve.add(new Word(eng, kor));
}
}
}
public class main {
public static void main(String[] args){
EngQuiz eq = new EngQuiz();
eq.run();
}
}
'대학교 수업 > Java 프로그래밍' 카테고리의 다른 글
[JAVA] 명품 자바 프로그래밍 11장 실습문제 (0) | 2022.10.05 |
---|---|
[JAVA] 명품 자바 프로그래밍 9장 실습문제 (0) | 2022.10.03 |
[JAVA] 명품 자바 프로그래밍 6장 실습문제 (0) | 2022.06.12 |
[JAVA] 명품 자바 프로그래밍 5장 실습문제 (0) | 2022.06.11 |
[JAVA] 명품 자바 프로그래밍 4장 실습문제 (0) | 2022.04.17 |