Results 1 to 7 of 7
Thread: how should i begin..
- 10-21-2010, 12:22 AM #1
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
how should i begin..
Trying to design an application that determines and prints the number of odd,even,and zero digits in an interger value read from keyboard.Guys i've done problems similar to this one and always get stuck in the same spot..
Java Code:import java.util.Scanner; public class smoked { public static void main(String[]args) { Scanner scan = new Scanner(System.in); System.out.print("Enter any Interger:"); scan.nextInt(); int j = scan.nextInt(); } }:)
- 10-21-2010, 12:58 AM #2
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
You have your integer entered from the key board stored in variable j.
All you need to do is code the "math" on that integer and do a System.out.print() on your calculated results.
example: if you wanted to add 5 to your integer and print the result:
int result = j + 5;
System.out.print(result);
If it's the "math" part that you are having trouble with, think about how you would solve the problem (or prove it mathematically) by hand and then try to code it from there.
example: how to determine if a number is even or odd:
divide by two and see if the result is a whole number, if the result is a whole number then the remainder would be 0, thus your number is even -- you could then look at the Math class Math (Java Platform SE 6) and Arithmetic Operators Summary of Operators (The Java™ Tutorials > Learning the Java Language > Language Basics) to see what will help you solve the problem.
I hope this helps. :)Last edited by tashimoto; 10-21-2010 at 01:10 AM.
- 10-21-2010, 01:38 AM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Hey thanks for your response .. so i've got it down with the checking if the interger is odd/even. Now to check and analyze every individual number in the interger , could i do something like a while loop, that keeps on taking the remainder of the interger to seperate each number then analyze each number ??
Java Code:import java.util.Scanner; public class smoked { public static void main(String[]args) { Scanner scan = new Scanner(System.in); System.out.print("Enter any Interger:"); int holder = scan.nextInt(); if (holder % 2 == 0 ) { System.out.println("I am an even number"); } else { System.out.println("I am an odd number"); } } }
- 10-21-2010, 05:06 PM #4
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Do you mean, if the integer entered by the user is 345, then the program checks the 3, the 4, and the 5 (as in 300, 40, 5) or is it supposed to check every number below 345 : 344, 343, 342, 341... back down to 0 ??
The first way is breaking down the number into it's 100s, 10s, 1s place to find each individual number. The other way is decrementing the last number by 1.
Both would need a loop of some sort. Try breaking it down by hand solving smaller integers to find the mathematical "patterns" ... any repeating patterns usually require a loop.
Hope this helps! :)
- 10-21-2010, 05:10 PM #5
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
HEY IT TOOK ME A WHILE BUT I GOT IT THANKS TOO ALL YOUR HELP, HERE IT IS
Java Code:import java.util.Scanner; public class smoked { public static void main(String[]args) { int x = 0; int y,holder,holder2; Scanner scan = new Scanner(System.in); System.out.print("Enter any Interger:"); holder = scan.nextInt(); while (holder > 0) { holder2 = holder % 10; holder = holder /10; System.out.println(holder2); if ( holder2 % 2 != 0 ) { System.out.println("Im an odd Interger"); } else { System.out.println("im an even Interger"); } x++; } } }// GETTING STARTED IS MY BIGGEST HURDLE
- 10-21-2010, 05:38 PM #6
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Cool!
For other problems similar to this one, it also might help to list out your variables and what value they contain as you design your loops, especially when using arrays. It might help visualize what is happening in the loops. example: var[i] in a loop, write the value of var[0] and i, then var[1] and i, then var[2] and i ... and so on. It really helps to do this with nested loops! :)
Also, some other things to think about is code format... Class names should begin with a capital letter while variables begin with lower case letters: "smoked" should be "Smoked". In larger projects, it makes the code easier for others to read.
Also, when I was starting out, a handy piece of code to add to all your projects is a while loop that allows the user to continue or quit. Then you can run your code with multiple entries.
Good luck with your other projects! :)
- 10-21-2010, 09:48 PM #7
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Similar Threads
-
New to Java...where do I begin?
By Darkrainer in forum New To JavaReplies: 3Last Post: 10-04-2010, 04:55 PM -
Graphics/animations etc. Where to begin?
By Mattedatten in forum New To JavaReplies: 2Last Post: 03-16-2010, 05:51 PM -
Where do I begin
By shaani in forum New To JavaReplies: 1Last Post: 12-24-2009, 05:32 AM -
new to java: where to begin?
By shintashi in forum New To JavaReplies: 3Last Post: 07-23-2009, 08:33 AM -
how begin with writing a new program ?
By dimitrist in forum New To JavaReplies: 11Last Post: 05-13-2008, 03:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks