【问题标题】:Can't compile java code无法编译java代码
【发布时间】:2015-09-01 21:11:43
【问题描述】:

我在 Eclipse 中为一个简单的二十一点游戏编写了一个程序,它运行起来没有任何问题。但是,当我尝试在我的 pico 编辑器中使用 javac 编译时,它不会编译。为什么 Eclipse 不说编译错误在哪里?代码如下。

import java.util.Scanner;

import java.util.Random;

public class Blackjack {

     public static void main(String args[])
     {
         Scanner keyboard = new Scanner(System.in);
         Random randomGenerator = new Random();
         int nextCard;
         boolean keepPlaying = true;
         boolean playAgain = true;
         char hit;
         int DECK_MAX = 10;

         //This is the main loop to play the entire game
         while (playAgain == true)
         {
             int userCard1 = randomGenerator.nextInt(DECK_MAX) + 1;
             int userCard2 = randomGenerator.nextInt(DECK_MAX) + 1;
             int userTotal = userCard1 + userCard2;
             int dealerCard1 = randomGenerator.nextInt(DECK_MAX) + 1;
             int dealerCard2 = randomGenerator.nextInt(DECK_MAX) + 1;
             int dealerTotal = dealerCard1 + dealerCard2;

             //This deals the first two cards to the user, and prints the total
             System.out.println("Welcome to the CCSF Casino! Lets play...");
             System.out.println("Your first cards are " + userCard1 + " and " + userCard2);
             System.out.println("You have a total of " + userTotal);

             //The following checks three scenarios that can end the game on the first deal
             if (userTotal == 21 && dealerTotal == 21) 
             {
                 System.out.println("Both you and the dealer have 21. It's a push.");
                 keepPlaying = false;
             }
             if (userTotal != 21 && dealerTotal == 21)
             {
                 System.out.println("The dealer was dealt 21. You lose.");
                 keepPlaying = false;
             }
             if (userTotal == 21 && dealerTotal != 21)
             {
                 System.out.println("You have 21. You win!");
                 keepPlaying = false;
             }

             //The following checks if the game should continue to user interaction phase, and takes user input if so
             if (keepPlaying == true)
             {
                 System.out.println("The dealer is showing " + dealerCard1 + " with one card face down.");

                 //The following asks the user if they want to keep hitting as long as their total is less than 21
                 while (userTotal < 21)
                 {
                     System.out.println("Would you like another card(y/n)?");
                     hit = keyboard.next().charAt(0);
                     if (hit == 'y')
                     {
                         nextCard = randomGenerator.nextInt(DECK_MAX) + 1; 
                         userTotal += nextCard;
                         System.out.println("You were dealt a " + nextCard + " for a total of " + userTotal);

                         if(userTotal > 21)
                         {
                             System.out.println("You bust and lose. Sorry...");
                             keepPlaying = false;
                             break;
                         }
                     }
                     else
                         break;
                 } 
             }


             //The following checks if the game should continue to the dealer interaction phase. If so, it hits until dealer has > 17
             if (keepPlaying == true)
             {
                 System.out.println("The dealer's current total is " + dealerTotal);

                 while (dealerTotal < 17)
                 {
                     nextCard = randomGenerator.nextInt(DECK_MAX) + 1;
                     dealerTotal += nextCard;
                     System.out.println("Dealer has been dealt a " + nextCard + " for a total of " + dealerTotal);

                     if (dealerTotal > 21)
                     {
                         System.out.println("The dealer busts. You win!");
                         keepPlaying = false;
                         break;
                     }
                 }
             }

             //The following checks if the game should continue into the comparison phase. If so, it determines whether the dealer of user win
             if (keepPlaying == true)
             {
                 if (userTotal > dealerTotal)

                     System.out.println("You win! You had " + userTotal + " and dealer only had " + dealerTotal);

                 if (userTotal  < dealerTotal)
                     System.out.println("You lose...You only had " + userTotal + " and dealer had " + dealerTotal);

                 if (userTotal == dealerTotal)
                     System.out.println("It's a push!");
             }

             System.out.println("Would you like to play again(y/n)?");
             if (keyboard.next().charAt(0) == 'y')
             {   playAgain = true;
                 keepPlaying = true;
             }
             else
             {
                 System.out.println("Don't let the door hit you on the way out!");
                 break;
             }
         }

     }
}

这就是我从 javac 得到的结果

Blackjack.java:23: error: not a statement
total for user
^
Blackjack.java:23: error: ';' expected
total for user
     ^
Blackjack.java:23: error: '(' expected
total for user
         ^
Blackjack.java:23: error: not a statement
total for user
          ^
Blackjack.java:23: error: ';' expected
total for user
              ^
Blackjack.java:24: error: '.class' expected
                         int dealerCard1 =
                             ^
Blackjack.java:24: error: illegal start of expression
                         int dealerCard1 =
                                         ^
Blackjack.java:24: error: ')' expected
                         int dealerCard1 =
                                          ^
Blackjack.java:25: error: illegal start of expression
randomGenerator.nextInt(DECK_MAX) + 1; //first card for dealer
               ^
Blackjack.java:25: error: ';' expected
randomGenerator.nextInt(DECK_MAX) + 1; //first card for dealer
                       ^
Blackjack.java:25: error: variable declaration not allowed here
randomGenerator.nextInt(DECK_MAX) + 1; //first card for dealer
                ^
Blackjack.java:25: error: not a statement
randomGenerator.nextInt(DECK_MAX) + 1; //first card for dealer
                        ^
Blackjack.java:25: error: ';' expected
randomGenerator.nextInt(DECK_MAX) + 1; //first card for dealer
                                ^
Blackjack.java:32: error: ';' expected
prints the total
          ^
Blackjack.java:33: error: ';' expected
                         System.out.println("Welcome to the CCSF Casino!
                               ^
Blackjack.java:33: error: unclosed string literal
                         System.out.println("Welcome to the CCSF Casino!
                                            ^
Blackjack.java:33: error: ';' expected
                         System.out.println("Welcome to the CCSF Casino!
                                                                         ^
Blackjack.java:34: error: not a statement
Lets play...");
     ^
Blackjack.java:34: error: ';' expected
Lets play...");
         ^
Blackjack.java:34: error: unclosed string literal
Lets play...");
            ^
Blackjack.java:41: error: ';' expected
end the game on the first deal
       ^
Blackjack.java:41: error: ';' expected
end the game on the first deal
               ^
Blackjack.java:41: error: ';' expected
end the game on the first deal
                         ^
Blackjack.java:41: error: not a statement
end the game on the first deal
                          ^
Blackjack.java:41: error: ';' expected
end the game on the first deal
                              ^
Blackjack.java:43: error: ';' expected
the user and dealer were both dealt 21, it's a push and the game ends
        ^
Blackjack.java:43: error: variable declaration not allowed here
the user and dealer were both dealt 21, it's a push and the game ends
    ^
Blackjack.java:43: error: ';' expected
the user and dealer were both dealt 21, it's a push and the game ends
                   ^
Blackjack.java:43: error: ';' expected
the user and dealer were both dealt 21, it's a push and the game ends
                             ^
Blackjack.java:43: error: not a statement
the user and dealer were both dealt 21, it's a push and the game ends
                              ^
Blackjack.java:43: error: ';' expected
the user and dealer were both dealt 21, it's a push and the game ends
                                   ^
Blackjack.java:43: error: unclosed character literal
the user and dealer were both dealt 21, it's a push and the game ends
                                          ^
Blackjack.java:43: error: not a statement
the user and dealer were both dealt 21, it's a push and the game ends
                                        ^
Blackjack.java:43: error: ';' expected
the user and dealer were both dealt 21, it's a push and the game ends
                                                   ^
Blackjack.java:43: error: ';' expected
the user and dealer were both dealt 21, it's a push and the game ends
                                                           ^
Blackjack.java:43: error: ';' expected
the user and dealer were both dealt 21, it's a push and the game ends
                                                                     ^
Blackjack.java:45: error: unclosed string literal
                                 System.out.println("Both you and the
                                                    ^
Blackjack.java:45: error: ';' expected
                                 System.out.println("Both you and the
                                                                      ^
Blackjack.java:46: error: not a statement
dealer have 21. It's a push.");
       ^
Blackjack.java:46: error: ';' expected
dealer have 21. It's a push.");
           ^
Blackjack.java:46: error: unclosed character literal
dealer have 21. It's a push.");
                  ^
Blackjack.java:46: error: not a statement
dealer have 21. It's a push.");
                ^
Blackjack.java:46: error: ';' expected
dealer have 21. It's a push.");
                           ^
Blackjack.java:46: error: unclosed string literal
dealer have 21. It's a push.");
                            ^
Blackjack.java:50: error: ';' expected
the dealer was dealt 21 and the user wasn't, user loses and the game ends
          ^
Blackjack.java:50: error: variable declaration not allowed here
the dealer was dealt 21 and the user wasn't, user loses and the game ends
    ^
Blackjack.java:50: error: ';' expected
the dealer was dealt 21 and the user wasn't, user loses and the game ends
                    ^
Blackjack.java:50: error: ';' expected
the dealer was dealt 21 and the user wasn't, user loses and the game ends
                               ^
Blackjack.java:50: error: unclosed character literal
the dealer was dealt 21 and the user wasn't, user loses and the game ends
                                         ^
Blackjack.java:50: error: ';' expected
the dealer was dealt 21 and the user wasn't, user loses and the game ends
                                                       ^
Blackjack.java:50: error: ';' expected
the dealer was dealt 21 and the user wasn't, user loses and the game ends
                                                               ^
Blackjack.java:50: error: ';' expected
the dealer was dealt 21 and the user wasn't, user loses and the game ends
                                                                         ^
Blackjack.java:52: error: unclosed string literal
                                 System.out.println("The dealer was dealt
                                                    ^
Blackjack.java:52: error: ';' expected
                                 System.out.println("The dealer was dealt
                                                                          ^
Blackjack.java:53: error: ';' expected
21. You lose.");
            ^
Blackjack.java:53: error: unclosed string literal
21. You lose.");
             ^
Blackjack.java:57: error: ';' expected
the user got 21 and the dealer didn't, user wins and game ends
        ^
Blackjack.java:57: error: variable declaration not allowed here
the user got 21 and the dealer didn't, user wins and game ends
    ^
Blackjack.java:57: error: not a statement
the user got 21 and the dealer didn't, user wins and game ends
         ^
Blackjack.java:57: error: ';' expected
the user got 21 and the dealer didn't, user wins and game ends
            ^
Blackjack.java:57: error: ';' expected
the user got 21 and the dealer didn't, user wins and game ends
                       ^
Blackjack.java:57: error: unclosed character literal
the user got 21 and the dealer didn't, user wins and game ends
                                   ^
Blackjack.java:57: error: ';' expected
the user got 21 and the dealer didn't, user wins and game ends
                                                ^
Blackjack.java:57: error: ';' expected
the user got 21 and the dealer didn't, user wins and game ends
                                                         ^
Blackjack.java:57: error: not a statement
the user got 21 and the dealer didn't, user wins and game ends
                                                          ^
Blackjack.java:57: error: ';' expected
the user got 21 and the dealer didn't, user wins and game ends
                                                              ^
Blackjack.java:59: error: unclosed string literal
                                 System.out.println("You have 21. You
                                                    ^
Blackjack.java:59: error: ';' expected
                                 System.out.println("You have 21. You
                                                                      ^
Blackjack.java:60: error: unclosed string literal
win!");
    ^
Blackjack.java:60: error: not a statement
win!");
   ^
Blackjack.java:65: error: ';' expected
continue to user interaction phase, and takes user input if so
           ^
Blackjack.java:65: error: ';' expected
continue to user interaction phase, and takes user input if so
                            ^
Blackjack.java:65: error: not a statement
continue to user interaction phase, and takes user input if so
                             ^
Blackjack.java:65: error: ';' expected
continue to user interaction phase, and takes user input if so
                                  ^
Blackjack.java:65: error: ';' expected
continue to user interaction phase, and takes user input if so
                                             ^
Blackjack.java:65: error: ';' expected
continue to user interaction phase, and takes user input if so
                                                        ^
Blackjack.java:65: error: '(' expected
continue to user interaction phase, and takes user input if so
                                                           ^
Blackjack.java:65: error: ')' expected
continue to user interaction phase, and takes user input if so
                                                              ^
Blackjack.java:68: error: unclosed string literal
                                 System.out.println("The dealer is showing
                                                    ^
Blackjack.java:68: error: ';' expected
                                 System.out.println("The dealer is showing
                                                                           ^
Blackjack.java:69: error: ';' expected
" + dealerCard1 + " with one card face down.");
                            ^
Blackjack.java:69: error: ';' expected
" + dealerCard1 + " with one card face down.");
                                      ^
Blackjack.java:69: error: unclosed string literal
" + dealerCard1 + " with one card face down.");
                                            ^
Blackjack.java:69: error: not a statement
" + dealerCard1 + " with one card face down.");
                                           ^
Blackjack.java:72: error: ';' expected
want to keep hitting as long as their total is less than 21
       ^
Blackjack.java:72: error: ';' expected
want to keep hitting as long as their total is less than 21
                    ^
Blackjack.java:72: error: not a statement
want to keep hitting as long as their total is less than 21
                     ^
Blackjack.java:72: error: ';' expected
want to keep hitting as long as their total is less than 21
                       ^
Blackjack.java:72: error: ';' expected
want to keep hitting as long as their total is less than 21
                               ^
Blackjack.java:72: error: ';' expected
want to keep hitting as long as their total is less than 21
                                           ^
Blackjack.java:72: error: ';' expected
want to keep hitting as long as their total is less than 21
                                                   ^
Blackjack.java:72: error: not a statement
want to keep hitting as long as their total is less than 21
                                                    ^
Blackjack.java:72: error: ';' expected
want to keep hitting as long as their total is less than 21
                                                        ^
Blackjack.java:75: error: unclosed string literal
                                         System.out.println("Would you
                                                            ^
Blackjack.java:75: error: ';' expected
                                         System.out.println("Would you
                                                                       ^
Blackjack.java:76: error: ';' expected
like another card(y/n)?");
                 ^
Blackjack.java:76: error: not a statement
like another card(y/n)?");
                   ^
Blackjack.java:76: error: ';' expected
like another card(y/n)?");
                     ^
Blackjack.java:76: error: unclosed string literal
like another card(y/n)?");
                       ^
Blackjack.java:84: error: unclosed string literal
                                                 System.out.println("You
                                                                    ^
100 errors

【问题讨论】:

  • 那么 javac 是怎么说的?
  • 换句话说,请发布完整的编译器错误消息并指出是哪一行导致了问题。
  • userTotaldealerTotal永远不会为 21。它们最多为 20。
  • first 错误消息显示total for user。我在代码中没有看到该文本。再试一次。
  • 您正在编译的代码可能不是您在这里展示的代码。

标签: java compiler-errors


【解决方案1】:

对不起大家,但问题最终与我没有在我用于 javac 的 ssh 上正确保存文件名有关。并不是要浪费您的时间,我很感激您的意见。在带到这里之前,请务必在未来进行更仔细的审查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 2017-01-16
    相关资源
    最近更新 更多