how to add to a global value from within an if statement

Put your programming skills to the test in these challenges.

how to add to a global value from within an if statement

Post by ghostheadx2 on Thu Jun 18, 2015 4:42 pm
([msg=88567]see how to add to a global value from within an if statement[/msg])

So, I have a value that is in an outer method. For the purposes of my program, its global. How do I use an if-statement so that the user can add, subtract, etc. to that value? Here is what my code looks like:

Code: Select all
import java.util.Scanner;

class accumulatorClass{
   public static void main(String args[]){
      Scanner option = new Scanner(System.in);
      Scanner a = new Scanner(System.in);
      Scanner b = new Scanner(System.in);
      System.out.println("Enter a number: ");
      double accumulator= a.nextDouble();
      System.out.println("Here are your options. Type the number of the option you prefer:");
      System.out.println("\n 1) Add \n 2) Subtract \n 3) Multiply \n 4) Divide \n 5) Raise to the power of \n 6) Quit");
      double input = option.nextDouble();
      main(args);
     
      if (input==1){
         System.out.println("Enter another number: ");
         double num2 = b.nextDouble();
         accumulator+=num2;
         System.out.println(accumulator);
         accumulatorClass.main(args);
      }
      else if (input==2){
         System.out.println("Enter another number:");
         double num2 = b.nextDouble();
         accumulator-=num2;
         System.out.println(accumulator);
         accumulatorClass.main(args);
      }
      else if (input==3){
         System.out.println("Enter another number: ");
         double num2 = b.nextDouble();
         accumulator*=num2;
         System.out.println(accumulator);
         accumulatorClass.main(args);
      }
      else if (input==4){
         System.out.println("Enter another number: ");
         double num2 = b.nextDouble();
         accumulator=(accumulator/num2)+(accumulator%num2);
         System.out.println(accumulator);
         accumulatorClass.main(args);
      }
      else if (input==5){
         System.out.println("Enter another number: ");
         double num2 = b.nextDouble();
         accumulator=Math.pow(accumulator, num2);
         System.out.println(accumulator);
         accumulatorClass.main(args);
      }
      else if (input==6){
         System.out.println(accumulator);
         System.exit(0);
      }
      else{
         System.out.println("not valid, try again");
         accumulatorClass.main(args);
      }
   }
}


Its kind of like I'm trying to have the user add, subtract etc. from the accumulator. When I try to do this it compiles but it starts the program from the beginning BEFORE it prints the accumulator value, which it never does. So I'm wondering why its doing this.

I also have another question using the same program but I think I worded it incorrectly and I didn't get a lot of replies.

Best,

ghostheadx2
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: how to add to a global value from within an if statement

Post by ghost107 on Thu Jun 18, 2015 5:26 pm
([msg=88568]see Re: how to add to a global value from within an if statement[/msg])

ghostheadx2 wrote:So, I have a value that is in an outer method. For the purposes of my program, its global. How do I use an if-statement so that the user can add, subtract, etc. to that value? Here is what my code looks like:

Its hard to read, too many variables, also try not to write all the program in the main method and use it as any normal method, that method is the program entry point(which does enter->execute->exist), and it is static, which means you cannot access any field that is not static from the class "accumulatorClass".

Also I don't see any global variables, global variables are variables that can be accessed globally(anywhere from your program), to do that in Java just declare the variable static.

ghostheadx2 wrote:Its kind of like I'm trying to have the user add, subtract etc. from the accumulator. When I try to do this it compiles but it starts the program from the beginning BEFORE it prints the accumulator value, which it never does. So I'm wondering why its doing this.

Most of the code is a good example of Anti-Pattern, Isn't this an infinite recursive loop(this way will crash your stack), I don't think it will reach the if statement, when the first main is calling itself over and over:
Code: Select all
class accumulatorClass{
   public static void main(String args[]){
      ...
      double input = option.nextDouble();
      main(args);
      ....
      accumulatorClass.main(args);
      ....
   }
}


Compared to python java has a control structure to what you are doing with if else, it's called switch.
Using If Else:
Code: Select all
if (input==1){
}
else if (input==2){
}
else if (input==3){
...
else{
}
}


Using Switch:
Code: Select all
switch(input){
   case 1:{
       break;
   }
   case 2:{
       break;
   }
   case 3:{
       break;
   }
...
   default:{
   }
}


ghostheadx2 wrote:I also have another question using the same program but I think I worded it incorrectly and I didn't get a lot of replies.

Code: Select all
class accumulatorClass{
   private static  double accumulator= 0;
   public static void main(String args[]){
        ...
        accumulator = a.nextDouble();

          while(true){
              input =  option.nextDouble();
              switch(input){
                 case 1:{
                        System.out.println("Enter another number: ");
                        accumulator+= option.nextDouble();
                        System.out.println(accumulator);
                        break;
                }
                ...
               }
         }
   }
}
User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron