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

So I am trying to make my first basic calculator
my problem is in "case 2" which was suppose to subtract two numbers is adding them even though i used "-" in between them
here is the program
```import java.util.Scanner;
public class MyClass {
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int s1=0, calculation = 0;
double sum = 0, sum1 = 1; int o;
String s;
do {
do {
System.out.println("enter the quantity of numbers to calculate");
s1 = input.nextInt();
if (s1<=1) {
}
}while (s1<=1); /* this means that if we have only 1 number to calculate
we cant do that so we ask user again to enter the number */
int quantity[]= new int[s1];
System.out.println("enter the numbers");
for (int i=0;i<s1;i++)  /*this means quantity[0] is the first empty box to input
the first number, and its gonna continue till it reaches s1 number of quantity.*/
{
quantity[i] = input.nextInt();
}
if (s1<=2) /*we are making seperate condition for division and subtraction cuz
they only use two numbers*/
{
System.out.println("choose the method of calculation");
System.out.println("type 1 for division");
System.out.println("type 2 for subtraction");
calculation = input.nextInt();
}
else {
System.out.println("choose the method of calculation");
System.out.println("type 3 for multiplication");
calculation = input.nextInt();
}
switch (calculation) {
case 1:
for (int i=0;i<2;i++) {
sum = quantity[i] / quantity[i++]; /* this means that its gonna its gonna
take the number stored in quantity[0](in first run) and divide it with
the value stored in quantity[1] */
}
case 2:
for (int i=0;i<2;i++) {
sum = quantity[i] - quantity[i++];
}
case 3:
for (int i=0;i<s1;i++) {
sum1 = quantity[i] * sum1; /* we are taking sum1=1 from main class because
if we use sum whose value is 0 the result is alwasy gonna be 0 since anythngX0=0*/
}
case 4:
for (int i=0;i<s1;i++ ) {
sum = quantity[i] + sum;
}
}
if ( (calculation)==3) /* since multiplication use variable sum1 to store the sum we
are gonna have to make a seperate condition to print it*/
{ System.out.println("the sum of ");
for (int i=0;i<s1;i++) {
System.out.print(quantity[i]+", "); /* this code will print "the sum of"
first and then loop itself by printing the numbers stored in quantity[0]
(in first run) and then print quantity[1] in the same line till we reach the
last quanity i.e s1 */

} 	System.out.println("is"+sum1);
}
else {
System.out.println("the result is"+ sum);
}
System.out.println("do you want to calculate again?");
s = input.next();
}         while (s.equalsIgnoreCase("yes") || (s.equalsIgnoreCase("yep") || (s.equalsIgnoreCase("yea") || (s.equalsIgnoreCase("ok") ||
(s.equalsIgnoreCase("okay"))))));

}
}```