Simplifiing Java

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Simplifiing Java

Post by T3rminus on Tue Jun 21, 2011 4:45 pm
([msg=58813]see Simplifiing Java[/msg])

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--;
            }
         }
      }
      
      
   }
}
Philosopher - T3rminus
Image
http://lunchboxproductions.webs.com
Basic: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
Realistic: (1) (2) (3)
Application: (1) (2)
Javascript: (1) (2)
User avatar
T3rminus
New User
New User
 
Posts: 13
Joined: Fri Aug 15, 2008 4:16 pm
Location: ∙∙∙∙∙∙...∙∙∙∙∙∙
Blog: View Blog (0)


Re: Simplifiing Java

Post by conscience on Tue Jun 21, 2011 5:44 pm
([msg=58815]see Re: Simplifiing Java[/msg])

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...
Let him who has understanding recount the number of the beast, for it is a human number: His number is 0x029A.
conscience
Poster
Poster
 
Posts: 250
Joined: Thu Jan 08, 2009 9:05 pm
Location: 127.0.0.1
Blog: View Blog (0)


Re: Simplifiing Java

Post by LoGiCaL__ on Wed Jun 22, 2011 12:19 am
([msg=58818]see Re: Simplifiing Java[/msg])

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.
Last edited by LoGiCaL__ on Wed Jun 22, 2011 9:09 am, edited 2 times in total.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1063
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: Simplifiing Java

Post by Vulpine on Wed Jun 22, 2011 12:36 am
([msg=58819]see Re: Simplifiing Java[/msg])

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...
User avatar
Vulpine
Poster
Poster
 
Posts: 379
Joined: Fri Mar 26, 2010 11:14 pm
Blog: View Blog (0)


Re: Simplifiing Java

Post by conscience on Wed Jun 22, 2011 6:06 pm
([msg=58867]see Re: Simplifiing Java[/msg])

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
Let him who has understanding recount the number of the beast, for it is a human number: His number is 0x029A.
conscience
Poster
Poster
 
Posts: 250
Joined: Thu Jan 08, 2009 9:05 pm
Location: 127.0.0.1
Blog: View Blog (0)


Re: Simplifiing Java

Post by ryl on Tue Jul 05, 2011 5:09 pm
([msg=59463]see Re: Simplifiing Java[/msg])

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!!
ryl
New User
New User
 
Posts: 2
Joined: Tue Jul 05, 2011 4:27 pm
Blog: View Blog (0)


Re: Simplifiing Java

Post by conscience on Wed Jul 06, 2011 12:10 pm
([msg=59505]see Re: Simplifiing Java[/msg])

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...
Let him who has understanding recount the number of the beast, for it is a human number: His number is 0x029A.
conscience
Poster
Poster
 
Posts: 250
Joined: Thu Jan 08, 2009 9:05 pm
Location: 127.0.0.1
Blog: View Blog (0)


Re: Simplifiing Java

Post by mShred on Wed Jul 06, 2011 12:25 pm
([msg=59506]see Re: Simplifiing Java[/msg])

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
Image

For those about to rock.
User avatar
mShred
Administrator
Administrator
 
Posts: 1763
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)


Re: Simplifiing Java

Post by conscience on Wed Jul 06, 2011 2:24 pm
([msg=59509]see Re: Simplifiing Java[/msg])

What's wrong, dude?
Let him who has understanding recount the number of the beast, for it is a human number: His number is 0x029A.
conscience
Poster
Poster
 
Posts: 250
Joined: Thu Jan 08, 2009 9:05 pm
Location: 127.0.0.1
Blog: View Blog (0)


Re: Simplifiing Java

Post by mShred on Wed Jul 06, 2011 8:56 pm
([msg=59521]see Re: Simplifiing Java[/msg])

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.
Image

For those about to rock.
User avatar
mShred
Administrator
Administrator
 
Posts: 1763
Joined: Tue Jun 22, 2010 4:22 pm
Blog: View Blog (2)


Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests