Results 1 to 6 of 6
- 09-16-2009, 10:10 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
I'm new to JAVA, stuck on "int"...Need some help getting this stuff straight, thanks!
Asks the user to enter a 5 digit positive integer
Calculate each digit using integer operations / and %.
Calculate and display the sum of the digits.
Heres what I got so far:
import java.util.Scanner;
/* Mike
* Testing Digits Program
* This is my work
*/
public class Digits
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter a 5 digit number:");
String digits = keyboard.nextLine();
int fi,se,th,fr,ft;
fi = (x/10000);
se = (x%1000)/1000;
th = (x%100)/100;
fr =
ft =
System.out.println()
}
}
I've got this far then i get errors left and right...and I'm looking for a similar question like it in the book and can't seem to find one. And this % is new to me I think I'm using it wrong so i left fr (fourth) and ft (fifth) . For example if i place 12345 i want the program to sum 1+2+3+4+5 which equals displaying 15...Thanks,, any contribution appreciated thanks.
- 09-16-2009, 10:21 AM #2
Hi,
1.Just take a paper and write what u want to do.
2.Implement the above one as a code by putting the comments properly
3.Try to compile.
4.Then ask us doubts where u got stuck up by bolding ur problematic line.
-Regards
RamyaRamya:cool:
- 09-16-2009, 10:42 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
I'm just getting started with an intro class to programing
1)I believe I am going to need to make a scanner so input can be inputted
2)Here is where I get stuck, in other scenerios like one found in the book...An age question can be asked
System. out. print ( "What is your age? ");
age = keyboard. nextInt( ) ;
But the question is asking me to difine different digits. I figured out that each of the digits needs to be looked at differently and uniquely, so I short named each of the digits as above, and stuck here again. How am I going to connect the int names to the display sum
I believe the last part should show System.out.println(fi+se+th+fr+ft)
I'm reading through the book now, the book isn't explaining as well. Thanks for your patience
- 09-16-2009, 10:56 AM #4
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
Heres what I wanted to do.... i figured it out
import java.util.Scanner;
public class Digits
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter a 5 digit number:");
int x = keyboard.nextInt();
int fi,se,th,fr,ft;
fi = (x - x%10000) / 10000;
x = x%10000;
se = (x - x%1000) / 1000;
x = x%1000;
th = (x - x%100) / 100;
x = x%100;
fr = (x - x%10) / 10;
x = x%10;
ft = x;
System.out.println(fi + se + th + fr + ft);
}
}
Thanks very much though...You have a good day.
- 09-16-2009, 01:16 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 13
- Rep Power
- 0
You may want to think about the names you give the variables.
fi,se,th,fr,ft are all very well and do work, but it is not that obvious what they are.
It would be much better to use:
firstDigit, secondDigit, thirdDigit, fourthDigit, fifthDigit.
It will make the code that much easier to read, and you will thank yourself for it one day when you have to go back and re read your own code.
- 09-16-2009, 01:36 PM #6
Member
- Join Date
- Sep 2009
- Location
- Chennai
- Posts
- 1
- Rep Power
- 0
% returns the remainder in a division problem. Getting user input in java through command line is not recommened. For now enter the input to the program directly rather than making the program dynamic. Java greatest strength is GUI . Dont worry you will learn about them later as you proceed through the course. If you want to know about GUI i will recommend Java how to program 6th edition Deitel &Deitel chapters 12 and 22 or 23
Similar Threads
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
final String currentWorld = "Java Forums"; String.format("Hello %s", currentWorld);
By mcfrog in forum IntroductionsReplies: 0Last Post: 04-02-2009, 07:02 PM -
[SOLVED] pls help :S . "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerE
By ara in forum New To JavaReplies: 10Last Post: 01-29-2009, 08:00 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks