PDA

View Full Version : یک عبارت و مقادیر هریک از متغییرهای آن را از کاربر بگیرد و حاصل عبارت را چاپ کند.



mf_arian
یک شنبه 04 بهمن 1388, 18:02 عصر
یک عبارت و مقادیر هریک از متغییرهای آن را از کاربر بگیرد و حاصل عبارت را چاپ کند.
منم این برنامه رو نوشتم ولی نمی دونم مشکلش چیه که ران نمی شه. please help me!:گریه:
package test;
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

final int[] number=new int[3];

System.out.println("enter 3 numbers:");
for(int i=0; i<3 ; i++)
{
number[i]=input.nextInt();
}

String[]operation=new String[2];
System.out.println("please enter 2 expression (+ , - ,* , /):");
for(int j=0 ; j<2 ;j++ )
{
operation[j]=input.next();
}

// package test;

class sub {
// public static void Main (String[]args)
//{
public int sub(String operation, int lhs, int rhs)
{

if (operation == "+")
{
return lhs + rhs ;
}
else if (operation == "-")
{
return lhs - rhs ;
}
else if(operation=="*")
{
return lhs * rhs ;
}
else if(operation == "/")
{
return lhs / rhs ;
}
// result String[] DoCalculation=new int[3];
int result=0;
int result = DoCalculation (operation[1], numbers[1], numbers[2])

result = DoCalculation(operation[2], result, number[3])
System.out.println(result);
}
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])
}
System.out.println(result);
}
}
}