실행코드 ¶
~cpp code
import javax.swing.*;
//import java.io.*;
public class NumberBaseballGame {
public static void main(String[] args){
int[] comNum = new int[3];
comNum[0] = (int)(Math.random() * 10 +1);
comNum[1] = (int)(Math.random() * 10 +1);
comNum[2] = (int)(Math.random() * 10 +1);
int[] userNum = new int[3];
int strikeCounter = 0, ballCounter=0, outCounter=0;
int i, j;
for(i=0;i<3;i++){
if (comNum[i] == 10){
comNum[i] --;
}
}
for(;;){
userNum[0] = Integer.parseInt(JOptionPane.showInputDialog(null,"첫번째 숫ìž�를 ìž…ë ¥í•˜ì‹œì˜¤"));
userNum[1] = Integer.parseInt(JOptionPane.showInputDialog(null,"ë‘�번째 숫ìž�를 ìž…ë ¥í•˜ì‹œì˜¤"));
userNum[2] = Integer.parseInt(JOptionPane.showInputDialog(null,"세번째 숫ìž�를 ìž…ë ¥í•˜ì‹œì˜¤"));
//String temp = JOptionPane.showInputDialog(null,"숫ìž�를 ìž…ë ¥í•˜ì‹œì˜¤ (한칸씩 ë�„어서)");
strikeCounter = 0;
ballCounter = 0;
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
if (comNum[i] == userNum[j] && i == j)
strikeCounter++;
else if(comNum[i] == userNum[j] && j != i && (comNum[j] != userNum[j]))
ballCounter++;
}
}
if(strikeCounter == 3){
JOptionPane.showMessageDialog(null, "3strike!! You win!");
break;
}
outCounter++;
JOptionPane.showMessageDialog(null, strikeCounter +"strike " + ballCounter + "ball " + outCounter + "out!");
if(outCounter == 3 && strikeCounter == 0){
JOptionPane.showMessageDialog(null,"삼진?ㅋ Game Over");
break;
}
}
}
}
처ì�Œì—� 하다가 ì˜�준ì�´í•œê±¸ 보니까 마지막ì�€ ì™„ì „ 똑같아진;;;ã… ã…
- 방법ì�€ 여러 방법ì�´ 있지. 만약 100ìž�리ë�¼ë©´, int 형ì�´ ì •ìˆ˜ê°’ë§Œ ê°€ì§€ê³ ë‚˜ë¨¸ì§€ëŠ” 버리는 특성ì�„ ì�´ìš©í•´ì„œ 123%10 하면 3ì�´ ë‚˜ì˜¤ê³ , 12%10 하면 2 ë‚˜ì˜¤ê³ ë‚˜ë¨¸ì§€ëŠ” 1ì�´ê³ ... ì�´ëŸ°ì‹�으로 숫ìž�른 나누어 줄 수ë�„ ìžˆê³ , ìž…ë ¥ì‹œì—� 어짜피 String형으로 받아지기 때문ì—� 문ìž� 하나씩 ë�Šì–´ ì�½ê²Œë�” í•´ë�„ ë�˜ì§€^^ 조금만 ìƒ�ê°�í•´ë³´ë©´ 방법ì�´ 나올 수ë�„ 있어 - ìƒ�ìš±










