🖊️ 문제
class Exercise1 {
public static void main(String args[]) {
Student s = new Student(); s.name = "홍길동";
s.ban = 1;
s.no = 1;
s.kor = 100;
s.eng = 60;
s.math = 76;
System.out.println("이름:"+s.name);
System.out.println("총점:"+s.getTotal(s.kor, s.eng, s.math) );
System.out.println("평균:"+s.getAverage() );
}
}
class Student {
/*
(1) 알맞은 코드를 넣어 완성하시오.
*/
}
[실행결과]
이름:홍길동
총점:236
평균:78.7
🖊️ 풀이
public class Student {
String name;
int ban;
int no;
int kor;
int eng;
int math;
public void name(String name) {
System.out.print(name);
}
public int getTotal(int kor, int eng, int math) {
int sum = kor + eng + math;
return sum;
}
public double getAverage() {
int sum = kor + eng + math;
double avg = (double)sum / 3;
return avg;
}
}
🖊️ 문제
class Exercise2 {
public static void main(String args[]) {
MyTv2 t = new MyTv2();
t.setChannel(10);
System.out.println("CH:"+t.getChannel());
t.setVolume(20);
System.out.println("VOL:"+t.getVolume());
}
}
class MyTv2 {
boolean isPowerOn;
int channel; int volume;
/*
(1) 알맞은 코드를 넣어 완성하시오.
*/
}
[실행결과]
CH:10
VOL:20
🖊️ 풀이
public class MyTv2 {
boolean isPowerOn;
int channel;
int volume;
public void setVolume(int vol) {
volume = vol;
}
public int getVolume() {
return volume;
}
public void setChannel(int ch) { //setter
channel = ch;
}
public int getChannel() { //getter
return channel;
}
}
🖊️문제
두개의 주사위 합을 맞히는 게임 제작
코인: 20개
합을 맞추면 제시한 숫자에 따라서 배당금이 달라집니다.
2, 12 : 제시할 수없음 3, 11 : 18 배
4, 10 : 12 배
5,9 :9배
6,8 :7배 7 :6배
배팅할 코인 수는? -------> 입력
두 주사위의 합은? -------> 입력 2, 12를 입력하면 다시 입력
두 주사위의 합은? -------> 6
주사위 번호 출력
결과 출력
🖊️풀이
public class MainClass {
public static void main(String[] args) {
diceGame game = new diceGame();
game.loop();
}
}
public class Dice {
int number;
//주사위를 굴린다
public void setRandom() {
//1 ~ 6
number = (int)(Math.random()*6 +1);
}
//주사위 값을 취득
public int getNumber() {
return number;
}
}
public class diceGame {
Scanner sc = new Scanner(System.in);
int coin = 20;
int batting;
int userNumber;
//주사위 두개의 합이 필요하므로 Dice class를 배열로 설정해 두개로 만들어줌
Dice dice[] = new Dice[2];
public void init() {
for (int i = 0; i < dice.length; i++) {
dice[i] = new Dice();
dice[i].setRandom();
// System.out.println("주사위" + i + ":" +dice[i].getNumber());
}
}
public void userInput() {
//배팅금액
while(true) {
System.out.print("배팅할 코인 수는?");
batting = sc.nextInt();
if(batting <= coin) {
break;
}else {
System.out.println("다시 입력해주세요");
}
}
coin = coin - batting;
System.out.println("남은 코인의 수 = " + coin);
//유저의 주사위 숫자
while(true) {
System.out.print("두 주사위의 합은(2와 12제외)?");
userNumber = sc.nextInt();
if (userNumber != 2 || userNumber != 12) {
break;
}
System.out.print("2와 12를 제외한 다른 숫자를 입력해주세요");
}//end of while(유저의 주사위 숫자)
}
public void sumPrint() {
System.out.println("입력한 주사위의 합 = " + userNumber);
}
public void finding() {
int dice1 = dice[0].getNumber();
int dice2 = dice[1].getNumber();
int diceSum = dice1 +dice2;
if(userNumber == diceSum) {
if(diceSum == 3 || diceSum == 11) {
batting = batting * 18;
}else if (diceSum == 4 || diceSum == 10) {
batting = batting * 12;
}else if (diceSum == 5 || diceSum == 9) {
batting = batting * 9;
}else if (diceSum == 6 || diceSum == 8) {
batting = batting * 7;
}else if (diceSum == 7) {
batting = batting * 6;
}
System.out.println("축하합니다!!!" + batting + "이 배당되었습니다");
coin = coin + batting;
}else {
System.out.println("아쉽습니다. 다시 도전하세요");
}
}
public void result() {
System.out.println("주사위 1 : " + dice[0].getNumber());
System.out.println("주사위 2 : " + dice[1].getNumber());
System.out.println("합계 = " + (dice[0].getNumber() + dice[1].getNumber()));
System.out.println("당신이 입력한 수 = " + userNumber);
System.out.println("남은 코인의 수 = " + coin);
}
public void loop() {
while(true) {
init();
userInput();
finding();
result();
System.out.print("replay(y/n)?= ");
String replay = sc.next();
if(replay.equals("n")) {
System.out.println("안녕히 가십시오");
break;
}
System.out.println("화이팅입니다");
}
}
}