2016-09-18 71 views
0

所以我想知道如何製作一個程序,該程序會提出多個問題,並導致多個值或其他問題的基礎上。延伸下面的程序爲急性和鈍角三角形|使用正弦cos函數

所以我建立了一個「直角三角形缺失的一面」程序。在一對夫婦嘗試後成功我想​​知道爲什麼不讓程序解決鈍角或銳角三角形。那麼,我可以編制正弦和餘弦的規律。問題是我不知道如何。我通過關於YouTube的2-3個視頻瞭解了Java的基礎知識。我試圖在if-else語句中嵌套一個if-else。但是,這是我的代碼我試圖建立在:

import java.util.*; 
import java.lang.Math; 

public class MissingSide { 

static java.util.Scanner userInput = new Scanner(System.in); 

public static void main(String[] args){ 
    int firstSideGiven = 0; 
    int hypotenuseGiven = 0; 
    Scanner userInput = new Scanner(System.in); 
    System.out.println("Are you trying to figure out a missing side given the hypotenuse and another side?"); 
    System.out.println("Then this is the place :)"); 
     System.out.print("What is the value of the first side, other than the hypotenuse?"); 
      if (userInput.hasNextInt()) { 
       firstSideGiven = userInput.nextInt(); 
      }else{ 
       System.out.println("Give me somthing else to work with!"); 
      } 
      System.out.println("What is your hypotenuse?"); 
      if (userInput.hasNextInt()){ 
       hypotenuseGiven = userInput.nextInt(); 
      }else{ 
       System.out.print("Wait, I want you to think about what you are doing with your life."); 
      } 
      System.out.println("Your missing side is: " + (Math.sqrt((Math.pow(hypotenuseGiven, 2)-Math.pow(firstSideGiven, 2))))); 
    } 
} 

,現在我想知道下一步該怎麼做,當我試圖窩在心裏的另一個if語句,它給了我一個錯誤,我不明白。

+1

如果你向我們展示了嘗試和錯誤 – Li357

+0

如果你正在製作菜單驅動程序,開關盒可以更好地工作 –

+0

如果用戶沒有提供整數,那麼wouldn'你想要回去再試一次還是退出程序?第一個需要一個循環。顯而易見的退出方式是將main方法的其餘行放入if語句中 - 所以是的,如果其他情況發生,您將會有其他內容。 –

回答

0

如果你能分享改變過程中面臨的問題和/或錯誤,那將是非常好的。

這是解決您的問題一個基本的邏輯 -

您可以使用Math.cosMath.sin函數來計算函數值。 這些值可用於計算缺失的一面。

注意 - 上述函數接受radian中的值,所以在使用前將度數值更改爲弧度。即 -

double angleInDegree = 354; 
double angleInRadian = Math.toRadians(angleInDegree); 
double cos = Math.cos(angleInRadian); 

希望它有幫助。 :)