Hi guys,
I am pretty new to java, and I have to write out an algorithm for a few operations following each other. The point I am at, is adding two numbers and then dividing the product. This is the code I have come up with so far, excuse the incoherence of it.

public class AddNumbers{
public static void main(String[] args) {
System.out.println("Addition of two numbers!");
int a = Integer.parseInt(args[15]);
int b = Integer.parseInt(args[7]);
int sum = a + b;
System.out.println("Sum: " + sum);}
{int k = sum;
int j = 30;
int i = k/j ;

}}

I am confused about how to link the sum to the division operation following it... I tried writing instead of int sum = a + b; this : int sum = k = a+b; in order to use the variable k to do int i = k/j; but it does not work. I just need help with how to link these operations, i have a few more to do after, so understanding this would be an important part of continuing the algorithm.

Thanks guys

Recommended Answers

All 2 Replies

I just found this, http://download.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
and instead of sum, i used result. The program runs, but is it correct to do?

public class AddNumbers{
public static void main(String[] args) {
System.out.println("Addition of two numbers!");
int a = Integer.parseInt(args[15]);
int b = Integer.parseInt(args[7]);

int result = a + b;
System.out.println(result);
int k = result;
int j = 30;
int i = k/j ;}

}

Member Avatar for ztini

Using "sum" or "result" or "hotdog" or "sadkjsf" is arbitrary for variable names. Call them whatever you want.

A word of advice on integer division:

5 / 2 = 2

int / int ... must equal int. The compiler will simply cut anything to the right of the decimal off. However, if you use a double at any point, the compiler will output a double; just be careful not to cast (int) over it.

5.0 / 2 = 2.5
5 / 2.0 = 2.5
5.0 / 2.0 = 2.5

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.