Author Topic: Random simple java programs that actually do something useful.  (Read 33787 times)

0 Members and 3 Guests are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #75 on: February 20, 2011, 02:47:58 pm »
Code: [Select]
package randomnumbergame;

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

public class Main {
    public static void main(String[] args) {
        System.out.println("Enter difficulty (from 0 to 1000): ");
        Scanner difficulty = new Scanner(System.in);
        int difficultyLevel = difficulty.nextInt();
        if (difficultyLevel<0 || difficultyLevel>1000)
        {
            System.out.println("Invalid Difficulty");
        }
        System.out.println(difficulty.nextLine());
        Random generator = new Random();
        int randomNumber = generator.nextInt(difficultyLevel);
        boolean gotIt = false;
        while (gotIt == false)
        {
            Scanner attempt = new Scanner(System.in);
            int attemptNumber = attempt.nextInt();
            if (attemptNumber == randomNumber)
            {
                gotIt = true;
            }
            else
            {
                if (attemptNumber > randomNumber)
                {
                    System.out.println("The secret number is smaller... ");
                }
                else
                {
                    System.out.println("The secret number is highter... ");
                }
            }
        }
        System.out.println("You won!");
    }

}

Random Number Game :)

Offline broooom

  • LV2 Member (Next: 40)
  • **
  • Posts: 28
  • Rating: +2/-0
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #76 on: February 20, 2011, 02:49:31 pm »
I'd avoid creating a scanner each and every time you need to read something - once is enough. :)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #77 on: February 20, 2011, 02:50:18 pm »
I'd avoid creating a scanner each and every time you need to read something - once is enough. :)

Oh yeah, I just need to call it again ;D

Ashbad

  • Guest
Re: Random simple java programs that actually do something useful.
« Reply #78 on: February 20, 2011, 02:50:47 pm »
Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better :P

anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Random simple java programs that actually do something useful.
« Reply #79 on: February 20, 2011, 02:50:52 pm »
Yeah, just use the same Scanner each time. Otherwise it can quickly eat up the JVM.




Offline broooom

  • LV2 Member (Next: 40)
  • **
  • Posts: 28
  • Rating: +2/-0
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #80 on: February 20, 2011, 02:51:37 pm »
Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better :P

anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?
You shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #81 on: February 20, 2011, 02:52:00 pm »
Code: [Select]
package randomnumbergame;

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

public class Main {
    public static void main(String[] args) {
        System.out.println("Enter difficulty (from 0 to 1000): ");
        Scanner inputScanner = new Scanner(System.in);
        int difficultyLevel = inputScanner.nextInt();
        if (difficultyLevel<0 || difficultyLevel>1000)
        {
            System.out.println("Invalid Difficulty");
        }
        System.out.println(inputScanner.nextLine());
        Random generator = new Random();
        int randomNumber = generator.nextInt(difficultyLevel);
        boolean gotIt = false;
        while (gotIt == false)
        {
            int attemptNumber = inputScanner.nextInt();
            if (attemptNumber == randomNumber)
            {
                gotIt = true;
            }
            else
            {
                if (attemptNumber > randomNumber)
                {
                    System.out.println("The secret number is smaller... ");
                }
                else
                {
                    System.out.println("The secret number is highter... ");
                }
            }
        }
        System.out.println("You won!");
    }

}

Would this work, then?

Offline broooom

  • LV2 Member (Next: 40)
  • **
  • Posts: 28
  • Rating: +2/-0
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #82 on: February 20, 2011, 02:52:59 pm »
One more thing: when the difficulty input is invalid, the program says so, but doesn't actually do anything - it just continues with the invalid input. :)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #83 on: February 20, 2011, 02:53:59 pm »
Code: [Select]
package randomnumbergame;

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

public class Main {
    public static void main(String[] args) {
        System.out.println("Enter difficulty (from 0 to 1000): ");
        Scanner inputScanner = new Scanner(System.in);
        int difficultyLevel = inputScanner.nextInt();
        if (difficultyLevel<0 || difficultyLevel>1000)
        {
            System.out.println("Invalid Difficulty");
            sys.exit(0);
        }
        System.out.println(inputScanner.nextLine());
        Random generator = new Random();
        int randomNumber = generator.nextInt(difficultyLevel);
        boolean gotIt = false;
        while (gotIt == false)
        {
            int attemptNumber = inputScanner.nextInt();
            if (attemptNumber == randomNumber)
            {
                gotIt = true;
            }
            else
            {
                if (attemptNumber > randomNumber)
                {
                    System.out.println("The secret number is smaller... ");
                }
                else
                {
                    System.out.println("The secret number is highter... ");
                }
            }
        }
        System.out.println("You won!");
    }

}

Fixed.

Ashbad

  • Guest
Re: Random simple java programs that actually do something useful.
« Reply #84 on: February 20, 2011, 02:54:14 pm »
Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better :P

anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?
You shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.

well, I'm talking simple 3D, like minecraft 3D.

Offline broooom

  • LV2 Member (Next: 40)
  • **
  • Posts: 28
  • Rating: +2/-0
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #85 on: February 20, 2011, 02:55:52 pm »
I'd do:

Code: [Select]
        int difficultyLevel = inputScanner.nextInt();
        while (difficultyLevel<0 || difficultyLevel>1000)
        {
            System.out.println("Invalid Difficulty");
            difficultyLevel = inputScanner.nextInt();
        }
:)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #86 on: February 20, 2011, 02:55:55 pm »
Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better :P

anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?
You shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.

well, I'm talking simple 3D, like minecraft 3D.

Even though... It's just so complicated, you shouldn't start with it, never.

Offline broooom

  • LV2 Member (Next: 40)
  • **
  • Posts: 28
  • Rating: +2/-0
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #87 on: February 20, 2011, 02:56:25 pm »
Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better :P

anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?
You shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.

well, I'm talking simple 3D, like minecraft 3D.
Well, still - you'd better be off learning C++ IMO. ;)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #88 on: February 20, 2011, 02:56:45 pm »
I'd do:

Code: [Select]
        int difficultyLevel = inputScanner.nextInt();
        while (difficultyLevel<0 || difficultyLevel>1000)
        {
            System.out.println("Invalid Difficulty");
            difficultyLevel = inputScanner.nextInt();
        }
:)

The user can also be too dumb to fix the error and make a valid difficulty. But it doens't matter.

Ashbad

  • Guest
Re: Random simple java programs that actually do something useful.
« Reply #89 on: February 20, 2011, 02:57:56 pm »
Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better :P

anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?
You shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.

well, I'm talking simple 3D, like minecraft 3D.

Even though... It's just so complicated, you shouldn't start with it, never.

I'm not starting with it, I definately know how to do java, just test me if you need to ;)

I tried openGL with C++ before, but that's the hardest way to use 3D I found out x.x  I just need to know a good library that's easy to install.