如何在Java中创建循环

时间:2015-09-24 16:01:22

标签: java loops

你好我正在为我的计算机科学课写一个程序。目标是制作一个计算毕达哥拉斯定理的计算器。我已经做了一些关于我需要用来循环的研究。喜欢while和do-while语句,但我无法弄清楚如何实现它们。我已经尝试了,并且有一些证据表明我的代码在我的代码中被注释掉了。我该如何制作循环?

我的代码:

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");
        }
        }
    }
}

4 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。一个简单的模式是使用布尔值,如

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

另一种方法是使用例外。如果您了解异常,这是我推荐的更先进的技术,但是由于您对如何使用循环的困惑,我猜您还没达到这个

答案 1 :(得分: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);
  }
}

答案 2 :(得分: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;
            }
            }
            }
        }
}

答案 3 :(得分:-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)

还可以使用try catch语句来捕获相应的异常并继续循环以实现您的功能。