Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
package expnum;
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
     int numbers[];
     int number;
     String expression[]={"+" , "-" , "*" , "/"};
     System.out.println("please enter 3 numbers:");
     number=input.nextInt();
     System.out.println("please enter 2 expression (+ , - ,* , /):");
     if(number=='+')
       System.out.println(number,"+",number);
     else if(number=='-')
       System.out.println(number,"-",number);
     else if(number=='*')
       System.out.println(number,"*",number);
     else if(number=='/')
       System.out.println(number,"/",number);

    
}

where is my problem?
Posted

1 solution

It's suppose to read 3 numbers and 2 operations? Where are the loops? I see only one call to read anything from the console at all. Where are expression or numbers ever used?

If this is a school assignment then you need to go to your professor and ask for help. If not then you really should pick up a beginners book and work through it because what you have so far is way off base.

Regardless here's some psuedo code to help:
int number[3]
string operations[2]

for(i = 1 to 3)
{
    print "Enter a number"
    read numbers[i]
}

for(i = 1 to 2)
{
    print "Enter an operation"
    read operation[i]
}

That will read your inputs, you should also validate them to make sure that what they enter is valid input and ask them again until what they enter is OK.

Then make a simple subroutine to do a calculation:
sub DoCalculation(string operation, int lhs, int rhs)
{
    if (operation == "+")
    {
        return lhs + rhs
    }
    else if (operation == "-")
    {
        return lhs - rhs
    }
    // etc.
}


Then you need to calculate. Is order of operations important or should it use a simple left to right scheme?

Left to right ordering would be this:
int result = DoCalculation(operation[1], numbers[1], numbers[2])
result = DoCalculation(operation[2], result, number[3])
print result


Proper Order of Operations would be this:
int result = 0
if (operation[1] == "*" or operation[1] == "/")
{
    // the first is a high order so it'll be executed first regardless of whatever the second is
    result = DoCalculation(operation[1], numbers[1], numbers[2])
    result = DoCalculation(operation[2], result, number[3])
}
else if (operation[2] == "*" or operation[2] == "/")
{
    // the first is a low order but the second is a high order so do it first
    result = DoCalculation(operation[2], numbers[2], numbers[3])
    result = DoCalculation(operation[1], numbers[1], result)
}
else
{
    // both are low order
    result = DoCalculation(operation[1], numbers[1], numbers[2])
    result = DoCalculation(operation[2], result, number[3])
}
print result

Both of the calculations bits could/should be loop-ified but I'll leave that as an exercise for the reader :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900