2016-11-06 313 views
-1
import java.util.Scanner; 

public class Calculator 
{ 
    public static void main(String[] args) 
    { 
     Scanner userInput = new Scanner(System.in); 
     String operator; 
     double num1,num2,answer = 0; 

     System.out.println("Enter first number: "); 
     num1 = userInput.nextDouble(); 
     System.out.println("Enter operator: "); 
     operator = userInput.next(); 
     System.out.println("Enter second number: "); 
     num2 = userInput.nextDouble(); 

     if (operator.equals ("+")){ 
      answer = num1 + num2; 
     } 
     else if (operator.equals ("-")){ 
      answer = num1 - num2; 
     } 
     else if (operator.equals ("*")){ 
      answer = num1 * num2; 
     } 
     else if (operator.equals ("/")){ 
      answer = num1/num2; 
     } 
     System.out.println("First number:" + num1); 
     System.out.println("Operator:" + operator); 
     System.out.println("Second number:" + num2); 
     System.out.println("Answer: " + answer); 
    } 
} 
+1

WUT?請真正解釋你的問題。 – Carcigenicate

+1

['Math.pow'(https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#pow-double-double-) – 4castle

+0

我已經添加的聲明如果用戶想要在每個運營商中繼續(y/n)。我是新來的這種編碼的東西......任何人都可以幫助我? – shaun

回答

0

使用添加指數這樣的功能:

Math.pow(x, y) 

這樣:地方

else if (operator.equals ("^")){ 
     answer = Math.pow(num1, num2); 
    } 

只是你現在的代碼之後的代碼

else if (operator.equals ("/")){ 
     answer = num1/num2; 
    } 

所以這部分代碼後會有

else if (operator.equals ("/")){ 
     answer = num1/num2; 
    } 
    else if (operator.equals ("^")){ 
     answer = Math.pow(num1, num2); 
    } 
+0

我應該在哪裏寫這篇Math.pow(X,Y)? – shaun

+0

我擴展了我的答案,告訴你在哪裏。 – MarianD

+0

感謝ü這麼多 – shaun