Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: New to Java : Need help with my integers rules

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Location
    Canada
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to Java : Need help with my integers rules

    Hi everyone,

    I was unable to see the board last Java class so I have a quick/easy question. I was asked to write a basic program with mathematical expressions so I wrote the following:

    _____________________________________

    public class Assign01 {
    public static void main(String[] args) {

    // creating my int variables here
    int num1=0,num2=0,sumvalue=0,diffvalue=0,productvalue= 0,quotvalue=0,remaindervalue=0;


    Scanner sc = new Scanner(System.in);

    // straight forward
    System.out.println("Please choose your first number: ");
    num1 = sc.nextInt();
    System.out.println("Please choose your second number: ");
    num2 = sc.nextInt();

    // straight forward
    sumvalue = num1+num2;
    diffvalue = num1-num2;
    productvalue = num1*num2;
    quotvalue = num1/num2;
    remaindervalue = num1%num2

    _____________________________________


    Under this I wrote "System.out.println ..." to write the answers and explain tips about math.
    Can someone explain where can I add something like this?

    ______________________
    int subtract(){

    int result;
    if (num1 > num2)
    result = num1-num2;
    else
    result = num2-num1;

    return result;
    ______________________

    and something like this for division: "divide = if 15 divided by 0 = zero then return the value 0 as a result". Where would I add this in my program?

    Any help/guidance would be greatly appreciated.

    Regards,

    MartinRR
    Last edited by MartinRR; September 29th, 2011 at 12:23 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: New to Java : Need help with my integers rules

    If you didn't see the board, good thing the java tutorials are so awesome: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Location
    Canada
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java : Need help with my integers rules

    Very nice! Thanks KevinWorkman.

    Cheers,

    Martin

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: New to Java : Need help with my integers rules

    Cross posted at New to Java - need help with adding many integers with rules such as ...

Similar Threads

  1. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  2. Cross Posting Forum Rules
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: February 3rd, 2011, 09:21 AM
  3. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  4. java integers
    By timeline in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2010, 02:54 AM