开发者

Can't Run My Program

Trying to make my first program "A Basic Mind Game".

Every time I try to run it nothing shows up and don't know what's wrong.

Hope that you can give me 开发者_如何学Pythonsome hand or advice.

BTW, I'm pretty much a NOOB at Java Programing so take it easy with the comments :)

Here is the code:

import java.util.*;
import java.util.Scanner.*;
import java.util.ArrayList.*;


 public class Main {
 public static void start() {

 int answer = (int) (Math.random() * 1000 + 1) ; 
 int tries = 0 ;
 int player ;
 String name = "name" ;
 String quit = "quit";
 String y = "yes";
 String n = "no";
 String guess = ("player") ;
 String another = ("Y") ;
 Scanner input = new Scanner (System.in);


    System.out.println( " Welcome to Guessing Game " ) ;
    System.out.print("Please enter a number between 1 and 1000 : ");
                    player = input.nextInt();
      long startTime = System.currentTimeMillis();
      int currentGuess = -1;



    while(another.equalsIgnoreCase("y")) {


      do
      {


               if (guess.equalsIgnoreCase(quit))
  {
   System.out.println("Leaving Us So Soon?");
   System.exit(0);
  }

               try    {
   currentGuess = Integer.parseInt(guess);
        } catch (NumberFormatException nfe) 
                        {
   System.out.println("Stupid Guess I Wont Count That.");
                        player = input.nextInt();
                        tries++;

   }


       if (currentGuess < answer )
          {
   System.out.println("too low");
   player = input.nextInt();
                        tries++;
  }


    else if(currentGuess  > answer )
  {
   System.out.println("too high");
   player = input.nextInt();
                        tries++;
  }
  //if the guess is invalid
  if (currentGuess < 0 || currentGuess > 1000)
  {
   System.out.println("Stupid Guess I Wont Count That.");
                        player = input.nextInt();
                        tries++;
  }
    else if (currentGuess == answer)
  {
   //stop stop watch
   long endTime = System.currentTimeMillis();
   //calculate game time
   long gameTime = endTime - startTime;
   System.out.println("You Rock Dude, Good Job!");

                        System.out.println("You guessed " + tries + " times in " + (int)(gameTime/1000) + " seconds.");
                        System.out.println("Please enter your name.");
            name = input.nextLine();
  }

        } while (player != answer);

      Scanner playGame = new Scanner(System.in);
   System.out.print("Want to go again?(y/n).....");
    another = playGame.nextLine();


    }
}

public static void main(String[] args) {

    }
}


Your main method is empty.

The main method contains the code that will run when the application starts. If the main method is empty, nothing will happen when you run your application.


If this isn't a troll I'll be stunned. The reason nothing shows up is because in your
static void main method you don't have any code for the program to execute.


Java's main method is main. Thus, you must run your code from main:

public static void main(String[] args) {
    start();
}


public static void main(String[] args) 
{
    start();
}

Make that change and see if it's better.


Sounds like homework, however this should do the trick.

public static void main(String[] args) {
    Main.start();
    }


After your "public class Main{ " line, put "start();" then proceed your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜