Page 1 of 2

Simplifiing Java

PostPosted: Tue Jun 21, 2011 4:45 pm
by T3rminus
Hey everyone I am trying out some java programming and getting further each day but I made this today with the skills that I have so far. I wanted to know I would be able to simplify this code or if I am making it to complicated. Please let me know. Its just a simple number guessing game:

Code: Select all
import java.util.Scanner;
public class imp {
   public static void main (String [] args){
      Scanner input = new Scanner(System.in);
      int usrIn = 0;
      int answer = 66;
      int cdown = 3;
      
      while (usrIn != answer){
         if (cdown == 0){
            System.out.println("You are out of tries!");
            break;
         }else{
            
      System.out.printf("Enter a number between 1-100. You have %s tries:", cdown);
      usrIn = input.nextInt();
            if (usrIn == answer){
         System.out.println("You guessed correctly! Congrats!");
         }else{
            System.out.println("You guessed incorrectly!");
            cdown--;
            }
         }
      }
      
      
   }
}

Re: Simplifiing Java

PostPosted: Tue Jun 21, 2011 5:44 pm
by conscience
I'd suggest

Code: Select all
while ((usrIn != answer) && (cdown>0))


for the first sight.

after the loop you just

Code: Select all
if (usrIn==answer){
  String outln="You guessed correctly! Congrats!";
} else {
  String outln("You ran out of tries!");
}
System.out.println(outln);


and alike. I'm not a frequent Java user so chances are that my solution is by no means optimal. I was just checking out and posting a first guess...

Re: Simplifiing Java

PostPosted: Wed Jun 22, 2011 12:19 am
by LoGiCaL__
Sorry man I don't know java yet. Sticking with c++ for now. So if you don't want to see it skip to the next post. But just some simple C++, this is what i would do:

Code: Select all
#include <iostream>
#include <string>
using namespace std;

int main()
{
  int guess = 0, magicNum = 79, cdown = 6;
  string name;

  while((guess != 79) || (cdown > 0))
  {

    cout << "You have " << cdown << " tries." << "Pick a number (1 through 200):  ";
    cin >> guess;

    if(guess > magicNum)
    {
      cout << "Too high.\n";
      --cdown;
    }
    else if(guess < magicNum)
    {
      cout << "Too low.\n";
      --cdown;
    }
    else
    {
      cout << "CONGRATULATION, YOU WON A PRIZE!!!!\n";;
    }

  }

  if(!cdown)
  {
    cout << "Sorry, you're out of tries.\n";
  }
 

  return 0;

}


Giving longer time, I would have the magicNum variable random instead of static, and also verify that it's a legit number. Open for correction. Shit, we need more stuff like this to keep skill up and keep shit competitive.

Edit in response to:

Post a little brain teaser (not unlike Project Euler in scope) and just reply with possible solutions in any language


Project Euler. Lol, Most of my down time at work is spent on there so it looks like I'm working on something.

Re: Simplifiing Java

PostPosted: Wed Jun 22, 2011 12:36 am
by Vulpine
LoGiCaL__ wrote:Shit, we need more stuff like this to keep skill up and keep shit competitive.


Ya know... I think that's an idea worth exploring. The first thing that popped into my head after reading that was some kind of weekly programming challenge on the forums: Post a little brain teaser (not unlike Project Euler in scope) and just reply with possible solutions in any language. No points, prizes, or other bullshit, just communal brain-storming.

Sorry for hijacking, btw...

Re: Simplifiing Java

PostPosted: Wed Jun 22, 2011 6:06 pm
by conscience
LoGiCaL__ wrote:
Code: Select all
#include <iostream>
#include <string>
using namespace std;

int main()
{
  int guess = 0, magicNum = 79, cdown = 6;
  string name;

  while((guess != 79) || (cdown > 0))
  {

    cout << "You have " << cdown << " tries." << "Pick a number (1 through 200):  ";
    cin >> guess;

    if(guess > magicNum)
    {
      cout << "Too high.\n";
      --cdown;
    }
    else if(guess < magicNum)
    {
      cout << "Too low.\n";
      --cdown;
    }
    else
    {
      cout << "CONGRATULATION, YOU WON A PRIZE!!!!\n";;
    }

  }

  if(!cdown)
  {
    cout << "Sorry, you're out of tries.\n";
  }
 

  return 0;

}


PORT:

Code: Select all
public class ILoveBeer {

  private int guess = 0;  // as with the visibility part, I don't really know how they'd take a single line
  private int magicNum = 79;
  private int cdown = 6;

  public static void main(String[] args) {

    while ((this.guess!=this.magicNum) && (the.cdown>0)) {  // You've made a mistake here, dude ;)

      System.out.print("You have "+cdown+" tries. Pick a number (1 through 200):  ");
      this.guess=System.in.read();

      if (this.guess>this.magicNum) {

        System.out.println("Too high!");
        this.cdown--; // or --cdown; I'm just more used to this one :)

      } else if (this.guess>this.magicNum) {

        Sytem.out.println("Too low!");
        this.cdown--; // or --cdown; I'm just more used to this one :)

      } else {

        Sytem.out.println("CONGRATULATION, YOU WON A PRIZE!!!!");

      }

    }

    if (this.cdown<=0) System.out.println("Sorry, you're out of tries.");

  } // main()

} // class ILoveBeer

// God, this lang is awful

Re: Simplifiing Java

PostPosted: Tue Jul 05, 2011 5:09 pm
by ryl
conscience wrote:
LoGiCaL__ wrote:
Code: Select all
#include <iostream>
#include <string>
using namespace std;

int main()
{
  int guess = 0, magicNum = 79, cdown = 6;
  string name;

  while((guess != 79) || (cdown > 0))
  {

    cout << "You have " << cdown << " tries." << "Pick a number (1 through 200):  ";
    cin >> guess;

    if(guess > magicNum)
    {
      cout << "Too high.\n";
      --cdown;
    }
    else if(guess < magicNum)
    {
      cout << "Too low.\n";
      --cdown;
    }
    else
    {
      cout << "CONGRATULATION, YOU WON A PRIZE!!!!\n";;
    }

  }

  if(!cdown)
  {
    cout << "Sorry, you're out of tries.\n";
  }
 

  return 0;

}


PORT:

Code: Select all
public class ILoveBeer {

  private int guess = 0;  // as with the visibility part, I don't really know how they'd take a single line
  private int magicNum = 79;
  private int cdown = 6;

  public static void main(String[] args) {

    while ((this.guess!=this.magicNum) && (the.cdown>0)) {  // You've made a mistake here, dude ;)

      System.out.print("You have "+cdown+" tries. Pick a number (1 through 200):  ");
      this.guess=System.in.read();

      if (this.guess>this.magicNum) {

        System.out.println("Too high!");
        this.cdown--; // or --cdown; I'm just more used to this one :)

      } else if (this.guess>this.magicNum) {

        Sytem.out.println("Too low!");
        this.cdown--; // or --cdown; I'm just more used to this one :)

      } else {

        Sytem.out.println("CONGRATULATION, YOU WON A PRIZE!!!!");

      }

    }

    if (this.cdown<=0) System.out.println("Sorry, you're out of tries.");

  } // main()

} // class ILoveBeer

// God, this lang is awful


Firstly java classes always start with a capital letter and you should avoid using I, i think c# and others use this as a convention for declaring interfaces. You shouldn't use the this statements as shown above, its not needed and will probably confuse you when you start working with objects later. On a side note, while its not really simplifying try expanding this to use a bufferedreader(new inputstreamreader(system.in)), you can then easily expand your app for socket programming or file handling. I think someone also mentioned above that you should probably try using Integer.parseInt when handling input from the user. And avoid using arrays they are the devil!!

Re: Simplifiing Java

PostPosted: Wed Jul 06, 2011 12:10 pm
by conscience
conscience wrote:I'm not a frequent Java user...


Thanks for ponting some out though.
I simply tried to make a port of the code above, not to re-implement it, so I didn't think it over. ;)
Actually I've implemented some basic stuff like a raw IRC terminal that helps me studying the traffic of the protocol. Java is not my cup of tea yet, however I'm currenty onto it...

Re: Simplifiing Java

PostPosted: Wed Jul 06, 2011 12:25 pm
by mShred
conscience wrote:
conscience wrote:I'm not a frequent Java user...


Thanks for ponting some out though.
I simply tried to make a port of the code above, not to re-implement it, so I didn't think it over. ;)
Actually I've implemented some basic stuff like a raw IRC terminal that helps me studying the traffic of the protocol. Java is not my cup of tea yet, however I'm currenty onto it...

O.o

Re: Simplifiing Java

PostPosted: Wed Jul 06, 2011 2:24 pm
by conscience
What's wrong, dude?

Re: Simplifiing Java

PostPosted: Wed Jul 06, 2011 8:56 pm
by mShred
conscience wrote:What's wrong, dude?

Oh nothing is wrong.. I just didn't get why you quoted yourself. Was that on accident? Or am i just missing something completely obvious. My bad man.