Results 1 to 3 of 3

Thread: How this works

  1. #1
    snowed is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default How this works

    Guys hello i am total noob about java and learning it. How this code works.

    When i enter 3 numbers it ad spaces to those numbers... But what does do those "/" and "%" in code? Could find it in documentation there is too much information for me there.

    And if i divide e.g. 321 with 100 it is 3.21 not 3. and so on so really dont understand.


    Java Code:
    import java.util.Locale;
    import java.util.Scanner;
    public class HelloWorld {
    	
    	public static void main (String[] args){
    		Scanner sc = new Scanner(System.in);
    		
    		
    		  System.out.print("Enter 3 numbers: ");
    		    int k = sc.nextInt();
    		    int i1 = k / 100;
    		    int i2 = (k / 10) % 10;
    		    int i3 = k % 10;
    
    		    System.out.print("Entered numbers: " + i1 + "  " + i2 + "  " + i3 + "  ");
    		  }
    		}
    Last edited by snowed; 04-01-2012 at 11:16 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: How this works

    divide e.g. 321 with 100 it is 3.21 not 3
    The compiler is doing integer arithmetic. The are no decimal places in integers. If you want decimal places, use a double instead of an int.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. School works?
    By D-X69 in forum New To Java
    Replies: 5
    Last Post: 11-21-2011, 05:56 PM
  2. toCharArray() How it works ?
    By Austin in forum New To Java
    Replies: 10
    Last Post: 08-05-2011, 09:19 AM
  3. Tell me how this loop works.
    By hydride in forum New To Java
    Replies: 6
    Last Post: 05-13-2010, 04:50 AM
  4. Anyone know how GroupLayout works?
    By ProgrammingPup in forum Advanced Java
    Replies: 5
    Last Post: 12-01-2009, 11:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •