Results 1 to 3 of 3
  1. #1
    MartinRR is offline Member
    Join Date
    Sep 2011
    Location
    Canada
    Posts
    1
    Rep Power
    0

    Default New to Java - need help with adding many integers with rules such as ...

    Hi everyone,

    The teacher last class was blocking the entire board while speaking so I could not understand the logic be behind where to place certain parts of the code. I will ask him many questions this evening but I need your help please. 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) {

    int num1=0,num2=0,sumvalue=0,diffvalue=0,productvalue= 0,quotvalue=0,remaindervalue=0;

    Scanner sc = new Scanner(System.in);

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


    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".


    Any help/guidance would be greatly appreciated.

    Regards,

    MartinRR
    Last edited by MartinRR; 09-29-2011 at 06:59 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: New to Java - need help with adding many integers with rules such as ...

    The method println() will print a line on the console.
    System is a class you can read about in the API doc: Java Platform SE 6
    Look in the list on the lower left for System. Click it and the doc will be in the right hand frame.
    When you read the System class's doc you will see a Field named out.
    out is an instance of the PrintStream class.
    println() in a member of that class.

    To see what this statement does, add some to your code at different places.

    Add this following the line where you compute the sum:
    System.out.println("sum value=" + sumvalue); // display the sum

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: New to Java - need help with adding many integers with rules such as ...


Similar Threads

  1. adding integers of an array
    By Lene90 in forum New To Java
    Replies: 9
    Last Post: 05-08-2011, 12:12 PM
  2. Replies: 5
    Last Post: 12-06-2010, 12:43 AM
  3. adding integers in JTable
    By khanzaman in forum AWT / Swing
    Replies: 2
    Last Post: 06-14-2010, 09:12 PM
  4. Adding new rules in PMD eclipse plugin
    By karuna in forum Eclipse
    Replies: 1
    Last Post: 09-28-2008, 03:29 PM
  5. Replies: 1
    Last Post: 07-05-2008, 03:08 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
  •