Reputation: 13
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.");
}
}
}
Upvotes: 0
Views: 51
Reputation: 334
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
Upvotes: 1