반응형

문제

Scanner 클래스를 사용하여 5개 학점('A', 'B', 'C', 'D', 'F')을 문자로 입력받아 ArrayList에 저장하고, ArrayList를 검색하여 학점을 점수(A=4.0, B=3.0, C=2.0, D=1.0, F=0)로 변환하여 출력하는 프로그램을 작성하라.

 

소스코드

import java.util.*;
// ArrayList연습
public class Main2 {

	public static void main(String[] args) {
		ArrayList<String> array = new ArrayList<String>();
		
		Scanner scan = new Scanner(System.in);
		System.out.print("5개의 학점을 입력하세요 : ");
		for(int i =0 ;i<5;i++) {
			String temp = scan.next();
			array.add(temp);
		}
		
		for(int i = 0;i<array.size();i++) {
			String temp = array.get(i); // i번째 문자열 얻어오기
			if(temp.equals("A")) {
				System.out.println("학점 ["+temp + "] = 4.0");
			}
			else if(temp.equals("B")){
				System.out.println("학점 [" + temp + "] = 3.0");
			}
			else if(temp.equals("C")){
				System.out.println("학점 [" + temp + "] = 2.0");
			}
			else if(temp.equals("D")){
				System.out.println("학점 [" + temp + "] = 1.0");
			}
			else
				System.out.println("학점 [" + temp + "] = 0");
		}
        scan.close();
	}

}

결과

5개의 학점을 입력하세요 : A B C D F
학점 [A] = 4.0
학점 [B] = 3.0
학점 [C] = 2.0
학점 [D] = 1.0
학점 [F] = 0
반응형
반응형

문제

Scanner 클래스를 사용하여 10개의 실수 값을 키보드로부터 읽어 벡터에 저장한 후, 벡터를 검색하여 가장 큰 수를 출력하는 프로그램을 작성하라.

 

소스코드

import java.util.*;
// Vector연습
public class Main1 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		Vector<Double> num = new Vector<Double>();
		System.out.println("10개의 실수를 입력하시오");
		for (int i = 0; i < 10; i++) {
			num.add(scan.nextDouble());
		}
		
		double max = 0.0;
		double temp = 0.0;
		Iterator<Double>it = num.iterator();
		while (it.hasNext()) {
			temp = it.next();
			max = (max > temp) ? max : temp;
		}
		System.out.println("10개의 실수 중 최대값은 : "+ max);
        scan.close();
	}

}

결과

반응형
반응형

문제

다음 프로그램은 커파일 오류가 발생한다. 소스의 어디에서 왜 컴파일 오류가 발생하는가?

 

#include<iostream>
using namespace std;

class Circle {
	int radius;
public:
	Circle(int radius = 1) { this->radius = radius; }
	int getRadius() { return radius; }
};

template<class T>
T bigger(T a, T b){ // 두 개의 매개 변수를 비교하여 큰 값을 리턴
	if (a > b)return a;
	else return b;
}

int main()
{
	int a = 20, b = 50, c;
	c = bigger(a, b);
	cout << "20과 50중 큰 값은 " << c << endl;
	Circle waffle(10), pizza(50), y;
	y = bigger(waffle, pizza);
	cout << "waffle과 pizza 중 큰 것은 반지름은 " << y.getRadius() << endl;
}

 

아래 결과와 같이 출력되도록 프로그램을 수정하라

20과 50중 큰 값은 50
waffle과 pizza 중 큰 것의 반지름은 20

 

y = bigger(waffle, pizza); 이 부분에서 클래스에 대한 비교가 없기 때문에 오류가 납니다.

 

소스코드

#include<iostream>
using namespace std;

class Circle {
	int radius;
public:
	Circle(int radius = 1) { this->radius = radius; }
	int getRadius() { return radius; }
};

template<class T>
T bigger(T a, T b){ // 두 개의 매개 변수를 비교하여 큰 값을 리턴
	if (a > b)return a;
	else return b;
}

Circle bigger(Circle a, Circle b) {
	if (a.getRadius() > b.getRadius())return a;
	else return b;
}

int main()
{
	int a = 20, b = 50, c;
	c = bigger(a, b);
	cout << "20과 50중 큰 값은 " << c << endl;
	Circle waffle(10), pizza(20), y;
	y = bigger(waffle, pizza);
	cout << "waffle과 pizza 중 큰 것은 반지름은 " << y.getRadius() << endl;
}
반응형

+ Recent posts