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 2 of 2

Thread: Subtracting arrays in switch case adds them instead

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Subtracting arrays in switch case adds them instead

    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) {
        		System.out.println("please enter two number minimum");
        	} 
        	}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");
            	    System.out.println("type 4 for addition");
            	    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"))))));
     
        }
        }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,320
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: Subtracting arrays in switch case adds them instead

    There needs to be a break statement at the end of each case statement, otherwise the code falls through to the code in the next case.

    Having a loop makes no sense if the results of previous computations are not used in future computations.
    The final results will be those from the last computation. All previous computations will be replaced and lost.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. switch case
    By sathirish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 10th, 2014, 04:08 AM
  2. switch case
    By viyyapu harsha in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2013, 12:03 AM
  3. Need help with switch case
    By niko25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2012, 12:10 AM
  4. Switch Case Problem help
    By Kikiam in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 18th, 2012, 06:11 PM
  5. Case Switch Help?
    By xionyus in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 19th, 2011, 08:59 PM

Tags for this Thread