package com.company;
import java.util.*;
import java.lang.*;
public class ENCRYPTED {
public static void main (String args[]) {
//Declaring scanner
Scanner sc = new Scanner(System.in);
//Declaring Arrays
double[] numbers = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
String[] op = {"+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+",};
double[] ans = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
//Getting Number of Numbers
System.out.println(" How many numbers to add/subtract ? : ");
int nofns = sc.nextInt();
// for loop that executes to get the numbers
for (int i = 0; i < nofns; i++) {
System.out.println("Enter Number " + i + " : ");
numbers[i] = sc.nextDouble();
System.out.println("Enter Operator for number " + i + " : ");
op[i] = sc.next();
ans[i] = numbers[i] + numbers[i+1];
System.out.println(ans[i]);
System.out.println(numbers[i]);
System.out.println(op[i]);
}
}
}
this is a program I made that takes in x numbers of numbers and stores them in the array (numbers) and operators (op). I want to store the operations done (-,+,*,/) on the ans array. I want to get a logic for this program that does the operations and stores them in the array. so these are my question.
- How do I write this Write this : (This taken from numbers array:5) ((This taken from op array)-) (This taken from numbers array:4) = (Stored and printed in ans aray)1
thanks for all for reading this
EDIT: "I just discovered the method for adding and substracting but I still want to find the multiplication and division, Sorry for not commenting the code"
THIS IS THE IMPROVED CODE
package com.company;
import java.util.*;
import java.lang.*;
public class ENCRYPTED {
public static void main (String args[]) {
//Declaring scanner
Scanner sc = new Scanner(System.in);
//Declaring Arrays
double[] numbers = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
String[] op = {"+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+","+",};
double[] ans = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
double sum=0;
//Getting Number of Numbers
System.out.println(" How many numbers to add/subtract ? : ");
int nofns = sc.nextInt();
// for loop that executes to get the numbers
for (int i = 0; i < nofns; i++) {
System.out.println("Enter Number " + i + " : ");
numbers[i] = sc.nextDouble(); //Ex : 5
// System.out.println("Enter Operator for number " + i + " : ");
// op[i] = sc.next(); //Ex: +
System.out.println("Enter Number " + i + " : ");
numbers[i+1] = sc.nextDouble(); //Ex : 6
ans[i] = numbers[i] + numbers[i+1];
System.out.println("Answer is " + ans[i]);
if (i == nofns) {
break;
}
}
for(int i=0; i<nofns; i++){
sum = sum + ans[i];
}
System.out.println("Elements of the sums are: "+Arrays.toString(ans));
System.out.println("Sum of the numbers :: "+sum);
}
}
IF anyone can suggest the method for multiplication and division, thanks for the help "OUTPUT: Number one : 4 Number Two : -2 and : 2" NOTE: YOU NEED TO SUFFIX "-" SIGN BEFORE THE NUMBER TO SUBSTRACT IT