0

I have been staring at my computer for two hours and I can't figure out what I am doing wrong. Can anyone help me see the light?

package blackjack;

import java.util.Random;
import java.util.Scanner;

/**
 *
 * 
 */
public class whileloop
 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here


       }

        // declare a variable 
    int human;
    int computer;
    int computerTotal=0;
    int humanTotal=0;

    Random randomNumber = new Random();
    Scanner keyboatrd = new Scanner(System.in);

       while((computerTotal < 21) && (humanTotal < 21))
       {
    computer = randomNumber.nextInt(11) + 1;
    computerTotal   = computerTotal + computer;

    human= randomNumber.nextInt(11) + 1;
    humanTotal=humanTotal+human;
    }

        if(computerTotal == 21)
    {
        System.out.println("computer total=" + computerTotal);
        System.out.println("human total=" + humanTotal);
        System.out.println("AI wins.");

        else if (humanTotal == 21)
        {
            System.out.println("computer total =" + computerTotal);
            System.out.println("human total=" + humanTotal);
            System.out.println("Human wins.");

                }   else if ((computerTotal < 21) && (humanTotal > 21) )
        {
            System.out.println("computer total=" + computerTotal);
            System.out.println("human total=" + humanTotal);
            System.out.println("AI wins.");    
    }
         else if ((computerTotal > 21) && (humanTotal > 21) )
       {
            System.out.println("computer total=" + computerTotal);
            System.out.println("human total=" + humanTotal);
            System.out.println("human wins.");  
        }
        else
        {
            System.out.println("computer total=" + computerTotal);
            System.out.println("human total=" + humanTotal);
            System.out.println("No winner."); 
        }
        }
    }
3
  • 3
    hint main method { }. Commented Sep 29, 2015 at 18:36
  • 1
    Format your code nicely and youll see the problem, which lies in opening or closing braces. Of course, reading the error text might help too. Commented Sep 29, 2015 at 18:38
  • Thanks! Being a rookie sure sucks... Commented Sep 29, 2015 at 18:49

1 Answer 1

1

You have several errors in your braces. First one is this piece of code

  public static void main(String[] args) {
    // TODO code application logic here


   }

You basically have empty program, because the rest of your code lies outside of the main method and the compiler never reaches the rest of your code. So remove the }

Your second error is in the

 if(computerTotal == 21)
{
    System.out.println("computer total=" + computerTotal);
    System.out.println("human total=" + humanTotal);
    System.out.println("AI wins.");

    else if (humanTotal == 21)

You don't have closing brace after the first if, so the else if is not attached to any if statement, hence the compiler error.

Are you using any IDE? Many people say using IDEs when you are rookie is bad idea, but I think it is not that bad idea at all, because it helps you see trivial errors like this faster. And you also manage to develop your personal workflow in the IDE as you learn to code, which saves you some time too

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using Netbean and it does get frustrating at times. thank you for your input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.