2013-03-27 7 views
-1

내 CS 수업에 숙제 문제가 있습니다. 나는 슬롯 머신을 만들어야하고 계속 작동하지 못한다. 스캐너 nextLine 메서드가 라인을 건너 뛰고 거기에있는 것을 유지하지만, 아무 것도 입력하지 않으면 그냥 끝난다. 프로그램. 또한 교수님이 요구하는 서식 때문에이 방법을 30 줄 이하로 나누어야합니다. 나는 또한 winnings를위한 달리는 총계를 유지해야한다 그러나 나는 대성공을 위해 그것을하는 방법을 알아낼 수 없다.자바 슬롯 머신

/** 
* ProgrammingAssignment2.java 
* 
* @author Corey Goff 
* @version March 2013 
*/ 
import java.util.Random; 
import java.util.Scanner; 
/** 
* This class simulates a slot machine. 
*/ 
public class SlotMachine 
{ 
    /** 
    * This is the main method. 
    * 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 
     Random random = new Random(); 
     String cont = "n"; 
     char answer; 
     int coin = 0; 
     int totalEntered = 0; 
     int a; 
     int b; 
     int c; 
     int n; 
     int amountWon = 0; 
     int dubs = coin * 2; 
     int trips = coin * 4; 

     while (cont.equals("n")) 
     { 
      a = random.nextInt(6); 
      b = random.nextInt(6); 
      c = random.nextInt(6); 
      n = random.nextInt(991) +10; 
      totalEntered += coin; 
      System.out.println("How much would you like to bet? "); 
      coin = keyboard.nextInt(); 

      switch (a) 
      { 
       case 0: 
        System.out.println("Cherry"); 
        break; 
       case 1: 
        System.out.println("Orange"); 
        break; 
       case 2: 
        System.out.println("Plum"); 
        break; 
       case 3: 
        System.out.println("Bell"); 
        break; 
       case 4: 
        System.out.println("Melon"); 
        break; 
       default: 
        System.out.println("Bar"); 
      } 

      switch (b) 
      { 
       case 0: 
        System.out.println("Cherry"); 
        break; 
       case 1: 
        System.out.println("Orange"); 
        break; 
       case 2: 
        System.out.println("Plum"); 
        break; 
       case 3: 
        System.out.println("Bell"); 
        break; 
       case 4: 
        System.out.println("Melon"); 
        break; 
       default: 
        System.out.println("Bar"); 
      } 

      switch (c) 
      { 
       case 0: 
        System.out.println("Cherry"); 
        break; 
       case 1: 
        System.out.println("Orange"); 
        break; 
       case 2: 
        System.out.println("Plum"); 
        break; 
       case 3: 
        System.out.println("Bell"); 
        break; 
       case 4: 
        System.out.println("Melon"); 
        break; 
       default: 
        System.out.println("Bar"); 
      } 

      if (a != b && a != c && b != c) 
      { 
       System.out.println("You have won $0"); 
      } 
      else if (a == b || a == c || b == c) 
      { 
       System.out.println("Congratulations, you have won $" + dubs); 
        amountWon += dubs; 
      } 
      else if (a == b && a == c && a != 0) 
      { 
       System.out.println("Congratulations, you have won $" + trips); 
        amountWon += trips; 
      } 
      else if (a == 0 && b == 0 && c == 0) 
      { 
       System.out.println("Congratulations! You have won the jackpot of $" 
        + (coin * n)); 

      } 

      System.out.println("Continue? y/n "); 
      cont = keyboard.nextLine(); 
     } 
    } 
} 
+3

왜 모두 투표를 거절 하시겠습니까? Atlest는 OP가 어떻게 질문을 개선 할 수 있는지 알려줍니다. – Smit

답변

2

당신이 keyboard.nextInt()를 호출하면 버퍼에서 개행 문자를 덤프 keyboard.nextLine()를 호출해야합니다. nextInt()는 int의 일부가 될 수없는 것을 발견 할 때까지 읽고 버퍼에있는 int 뒤에 모든 것을 남겨 둡니다. Reading Strings next() and nextLine() Java

+0

고맙습니다. 그게 그 문제를 해결했습니다. – Corey211

0

가 (작은 변화) 코드가 작동하도록하려면 :

자세한 내용은이 게시물을 참조 사용 keyboard.next(); 대신 keyboard.nextLine();

변화를 계속하는 최초 계속 = "n"을 = " y "로 설정하고 while 루프 검사에서 if (cont.equals ("y "))를 확인하십시오.

이것은 빠른 수정으로 효율성을 높이고 표준을 따라 잡을 수있는 많은 변경 사항이 있지만 자세히 설명하지는 않습니다.

30 라인 이하 요구 사항에 대해서는 하나의 구조에서 3 가지 과일을 모두 생성하기 위해 하나의 임의 생성기 만 사용하는 것으로 시작합니다. 자신에게 물어보십시오. 왜 각 과일에 대해 임의의 발전기를 분리해야합니까? 과일 당 새로운 스위치가 필요한 이유는 무엇입니까? 하나의 랜덤 vs 3 랜덤 제너레이터는 과일에 대해 동일한 임의성을 생성합니다.

관련 문제