how to replace a previous value

Put your programming skills to the test in these challenges.

how to replace a previous value

Post by ghostheadx2 on Wed Jun 17, 2015 2:10 am
([msg=88543]see how to replace a previous value[/msg])

So, in the assignment, I have a value called "accumulator_val." I'm trying to edit the program so that every time it presents the options again, the function adds, subtracts, multiplies, divides, or raises the accumulator value to the power of the next number. At the beginning, I need to have the accumulator equal whatever value the user inputs. Then the menu keeps popping up asking the user what they'd like to add onto, subtract, etc. from that value, but not ask for the original accumulator more than one each time I run the program. Here's what I need right now:

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();
      editVal();
     
   public void calcNum(){
     
      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.editVal();
         }
      }
   }
}


Thanks for the help.
Last edited by ghostheadx2 on Wed Jun 17, 2015 7:31 pm, edited 1 time in total.
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: how to replace a previous value

Post by cyberdrain on Wed Jun 17, 2015 3:41 pm
([msg=88554]see Re: how to replace a previous value[/msg])

It would be easier to spot if you would put the rest of the code outside the if-statements. Functions would be nice too, but let's not dwell on that. I mean something like this:
Code: Select all
System.out.println("Enter a number: ");
double num1 = a.nextDouble();
System.out.println("Enter another number: ");
double num2 = b.nextDouble();

if (input==1){
    accumulator_val=num1+num2;
}
else if (input==2){
    accumulator_val=num1-num2;
}

// do the same for the rest of your code here

System.out.println(accumulator_val);
accumulator.main(args);

Of course you'd need some extra logic for option 6, but that shouldn't be a problem. Lastly, a while true loop might be easier to use for cases like this, now you're constantly calling main with arguments if I read it correctly.
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: how to replace a previous value

Post by ghostheadx2 on Wed Jun 17, 2015 7:25 pm
([msg=88556]see Re: how to replace a previous value[/msg])

I'm editing my thread because I found out I'm not just trying to make a simple calculator.
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests