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 static void main(String[] args) {
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

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by ztini

and instead of sum, i used result. The program runs, but is it correct to do?

public static void main(String[] args) {
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 ;}

}

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.