Guidance/Help with Code CleanUp
First of all, yes this is homework and I already solved the problem, which required the programmer to write a program to determine if the digits in a three-digit number supplied by a user are all odd, all even, or mixed. My question is the following: Is there a way to make this code look prettier? I'm a chick and I'm new to Java. I want to keep my program functional, but I would also like it to look nice as well. Any suggestions would be greatly appreciated.
Code java:
import java.util.Scanner;//program uses class Scanner
public class Exe_Two
{
//main method begins execution of Java application
public static void main( String[] args)
{
//create a Scanner to obtain input from the command window
Scanner in =new Scanner(System.in);
System.out.println( "Input a three-digit number:");//prompt
int num =in.nextInt(); //read number from user
Boolean number1=false;
Boolean number2=false;
Boolean number3=false;
number1 = num % 2 == 0;
number2 = num % 2 == 0;
num/=10;
number3 = num % 2 == 0;
if(number1&&number2&&number3==Boolean.TRUE)
System.out.println("This number contains all even digits.");
else
if(number1==Boolean.TRUE||number2==Boolean.TRUE||number3==Boolean.TRUE)
System.out.println("This number contains both odd and even digits.");
else
System.out.println("This number contains all odd digits.");
in.close();
} // end method main
} // end class Exe_Two
Re: Guidance/Help with Code CleanUp
First thing is to use [code] tags. This formats source code in a nice and readable format. I've modified your post for you.
A few comments about the actual code:
1. Parts of your code aren't tabbed properly. This could be a copy/paste error. In general you want to tab in the contents of a block (anything after an opening curly brace, the statements which belong to a control statement like if, while, else, etc.). This is a stylistic issue and your program will execute the same regardless, but it's good practice to make sure your code is tabbed correctly. It makes it a lot easier for others to read your code and follow the logic.
2. Personal preference, I don't like the shortcut control statements which don't use curly braces for single statements. It's perfectly valid and a lot of people do it, I just don't like it.
Code java:
if(some_condition == true)
{
System.out.println("this has curly braces! Yay!");
}
else
System.out.println("this doesn't have curly braces. Perfectly valid still, but I don't like it.");
3. I think you're missing a line of code in there (possibly copy/past error?). Try your program with the number 212 or 121 and see if you can't figure out what's wrong.
4. don't use Boolean.TRUE. Just use the true keyword.
5. Along with #4, I would use a primitive boolean instead of the object variant Boolean (notice the capitalized B). I generally avoid using the object variants unless I have to (usually only comes into play when I want to take advantage of polymorphism or generics).
Re: Guidance/Help with Code CleanUp
Also
Code :
if(someCondition == true)
{
// some code here
}
is more neatly (and readably) written as
Code :
if(someCondition)
{
// some code here
}
Sometimes you write conditions this way, sometimes not, so there might be confusion about this. If so, ask.
Re: Guidance/Help with Code CleanUp
Another suggestion would be to use variable names that describe what the variable holds.
number1 looks like it should hold a number.
digit1IsEven looks like it would holds a boolean that is true if a digit is even
Re: Guidance/Help with Code CleanUp
Thank you and everyone else for all of your helpful suggestions and comments.