2015-09-24 51 views
0

您好,我正在爲我的計算機科學課程編寫一個程序。目標是製作一個計算畢達哥拉斯定理的計算器。我已經做了一些研究,以瞭解我需要用什麼來循環。像while和do-while語句一樣,但我無法弄清楚如何實現它們。我試過了,我的代碼中有一些證據表明我的嘗試被註釋掉了。我如何去做循環?如何在Java中創建循環

我的代碼:

import java.util.Scanner; 


public class Pythagorean_Theorem 
{ 
public static void main(String[] args) 
{ 

     // established scanner 
     Scanner Toby = new Scanner (System.in); 

     // Declares variables 
     double Leg1; 
     double Leg2; 
     double Hypotenuse; 
     double choice; 
     //double num1 
     //double num2 
     //double answer 
     //boolean False; 

    // Beginning of program offering choice for calculations 
    System.out.println ("This program is a basic calculator that also knows the Pythagorean theorem"); 
    System.out.println ("This program can solve for the hypotenuse or one of the legs \nOr you can use it as a simple calculator"); 
    System.out.println ("Are you solving for the hypotenuse or a leg?"); 
    System.out.println ("1 for hypotenuse \n2 for leg"); 

     choice = Toby.nextDouble(); 

    //Begins else if statements 
    //What you enter above corresponds to the choices below 

     //while ((Toby.nextDouble() != 1); 
     //while ((Toby.nextDouble() != 2); 
     { 
     if (choice == 1) 
     { 
      // Finding hypotenuse 
      System.out.println ("Enter first leg:"); 
       Leg1 = Toby.nextDouble(); 
      System.out.println ("Enter second leg:"); 
       Leg2 = Toby.nextDouble(); 
       Hypotenuse = Leg1 * Leg1 + Leg2 * Leg2; 
      System.out.println (Math.sqrt(Hypotenuse)); 
     } 

     else if (choice == 2) 
     { 
      // Finding leg 
      System.out.println ("Enter the leg:"); 
       Leg1 = Toby.nextDouble(); 
      System.out.println ("Enter hypotenuse"); 
       Hypotenuse = Toby.nextDouble(); 
       Leg2 = (Hypotenuse * Hypotenuse - Leg1 * Leg1); 
      System.out.println (Math.sqrt(Leg2)); 
     } 
     // Below here is invalid choices 
     else if (choice <= 0) 
     { 
       System.out.println ("This is not a valid choice"); 
     } 
     else if (choice >= 3) 
     { 
       System.out.println ("This is not a valid choice"); 
     } 
     } 
    } 
} 
+0

我不我認爲在這種情況下你甚至有一個循環的理由。 – Jim

+1

爲什麼你在'while'循環中添加分號?他們覺得他們做什麼? – Tom

+0

@Jim原因顯然是重新運行,如果輸入無效 – ControlAltDel

回答

-1
do { 
choice = toby.nextDouble() 
    check the choice is your expected output or not using if..else condition 
    and use an choice to exit 
}while(condition) 

還利用嘗試catch語句來捕捉適當的異常,並繼續循環,從而實現自己的功能。

+1

複製問題 – Shriram

+0

好的。你已經修好了。 – Tom

0

有很多方法可以做到這一點。一種簡單的模式是使用布爾值,如

boolean worked = false; 
do { 
    //try to get valid input 
    //if input is valid and calculation is done, worked = true 
} while (!worked); 

另一種方法是使用例外。這是一種更先進的技術,如果你知道例外,我建議,而是從如何使用循環你的困惑,我想你是達不到這個尚未

0
import java.util.Scanner; 

public class Pythagorean_Theorem{ 
    public static void main(String[] args){ 
    //... 
    boolean exit = false; 

    do { 
     // Beginning of program offering choice for calculations 
     System.out.println ("This program is a basic calculator that also knows the Pythagorean theorem"); 
     //... 
     System.out.println ("1 for hypotenuse \n2 for leg \n3 for exit"); 

     choice = Toby.nextDouble(); 

     //... 
     if (choice == 1){ 
     // ... 
     } else if (choice == 2) { 
     // ... 
     } else if (choice == 3) { 
      exit = true; 
      System.out.println ("Bye!"); 
     } else { 
      System.out.println ("This is not a valid choice"); 
     } 
    } while (!exit); 
    } 
} 
+0

檢查它[while和do-while語句](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html) –

0
package test.com; 

import java.io.File; 
import java.io.IOException; 
import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.*; 
import java.io.BufferedInputStream; 
import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.StringReader; 
import java.io.StringWriter; 
import java.net.InetAddress; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 

import org.apache.commons.net.ntp.NTPUDPClient; 
import org.apache.commons.net.ntp.TimeInfo; 

class Employee { 
    public int id; 
    private double balance; 
    public Employee(int id) { 
     // TODO Auto-generated constructor stub 
     this.id = id; 
    } 

    public boolean charge(double price){ 
     if (price + balance > 1) 
     return false; 
     balance += price; 
     return true; 
     } 
} 

class B extends Employee { 
    public B(int id) { 
     super(id); 
     // TODO Auto-generated constructor stub 
    } 

    public boolean charge(double price){ 
     boolean isSuccess = super.charge(price); 
     if(!isSuccess) 
     super.charge(5); //*** here the penalty 
     return isSuccess; 
     } 
} 
public class TestStrig { 


     public static void main(String[] args) throws Exception { 
      // established scanner 
      Scanner Toby = new Scanner (System.in); 

      // Declares variables 
      double Leg1; 
      double Leg2; 
      double Hypotenuse; 
      double choice; 
      //double num1 
      //double num2 
      //double answer 
      //boolean False; 

     // Beginning of program offering choice for calculations 
     while(true) { 
     System.out.println ("This program is a basic calculator that also knows the Pythagorean theorem"); 
     System.out.println ("This program can solve for the hypotenuse or one of the legs \nOr you can use it as a simple calculator"); 
     System.out.println ("Are you solving for the hypotenuse or a leg?"); 
     System.out.println ("1 for hypotenuse \n2 for leg \n3 to Exit"); 

      choice = Toby.nextDouble(); 

     //Begins else if statements 
     //What you enter above corresponds to the choices below 

      //while ((Toby.nextDouble() != 1); 
      while (true) { 
      if (choice == 1) 
      { 
       // Finding hypotenuse 
       System.out.println ("Enter first leg:"); 
        Leg1 = Toby.nextDouble(); 
       System.out.println ("Enter second leg:"); 
        Leg2 = Toby.nextDouble(); 
        Hypotenuse = Leg1 * Leg1 + Leg2 * Leg2; 
       System.out.println (Math.sqrt(Hypotenuse)); 
       break; 
      } 

      else if (choice == 2) 
      { 
       // Finding leg 
       System.out.println ("Enter the leg:"); 
        Leg1 = Toby.nextDouble(); 
       System.out.println ("Enter hypotenuse"); 
        Hypotenuse = Toby.nextDouble(); 
        Leg2 = (Hypotenuse * Hypotenuse - Leg1 * Leg1); 
       System.out.println (Math.sqrt(Leg2)); 
       break; 
      } 
      // Below here is invalid choices 
      else if (choice == 3) { 
       System.out.println("Bye Bye"); 
       System.exit(0); 
      } 
      else if (choice >= 4 || choice <= 0) 
      { 
        System.out.println ("This is not a valid choice"); 
        break; 
      } 
      } 
      } 
     } 
}