Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中创建循环_Java_Loops - Fatal编程技术网

如何在Java中创建循环

如何在Java中创建循环,java,loops,Java,Loops,你好,我正在为我的计算机科学课写一个程序。目标是制作一个计算毕达哥拉斯定理的计算器。我已经做了一些关于我需要使用什么来循环的研究。比如while和do-while语句,但我不知道如何实现它们。我已经试过了,有一些证据表明我的尝试在我的代码中被注释掉了。我如何开始做一个循环 我的代码: import java.util.Scanner; public class Pythagorean_Theorem { public static void main(String[] args) {

你好,我正在为我的计算机科学课写一个程序。目标是制作一个计算毕达哥拉斯定理的计算器。我已经做了一些关于我需要使用什么来循环的研究。比如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");
        }
        }
    }
}
import java.util.Scanner;
公共类勾股定理
{
公共静态void main(字符串[]args)
{
//固定扫描仪
扫描器Toby=新扫描器(System.in);
//声明变量
双Leg1;
双Leg2;
双斜边;
双重选择;
//双num1
//双num2
//双重回答
//布尔假;
//提供计算选择的程序开始
System.out.println(“这个程序是一个基本的计算器,也知道勾股定理”);
System.out.println(“此程序可以求解斜边或一条腿\也不能将其用作简单的计算器”);
System.out.println(“你是在解斜边还是一条腿?”);
System.out.println(“1表示斜边,n2表示腿”);
choice=Toby.nextDouble();
//从else if语句开始
//您在上面输入的内容与下面的选项相对应
//while((Toby.nextDouble()!=1);
//而((Toby.nextDouble()!=2);
{
如果(选项==1)
{
//找斜边
System.out.println(“输入第一段:”);
Leg1=Toby.nextDouble();
System.out.println(“输入第二段:”);
Leg2=Toby.nextDouble();
斜边=Leg1*Leg1+Leg2*Leg2;
System.out.println(数学sqrt(斜边));
}
else if(选项==2)
{
//寻找腿
System.out.println(“输入腿:”);
Leg1=Toby.nextDouble();
System.out.println(“输入斜边”);
斜边=Toby.nextDouble();
Leg2=(斜边*斜边-Leg1*Leg1);
System.out.println(Math.sqrt(Leg2));
}
//下面是无效的选择
else if(选项=3)
{
System.out.println(“这不是一个有效的选择”);
}
}
}
}

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

有很多方法可以做到这一点

boolean worked = false;
do {
  //try to get valid input
  //if input is valid and calculation is done, worked = true
} while (!worked);
另一种方法是使用异常。如果您了解异常,我建议您使用这项更高级的技术,但从您对如何使用循环的困惑来看,我猜您还不能胜任这项工作

package test.com;
 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);
  }
}
导入java.io.File; 导入java.io.IOException; 导入java.text.DateFormat; 导入java.text.ParseException; 导入java.text.simpleDataFormat; 导入java.util.*; 导入java.io.BufferedInputStream; 导入java.io.BufferedReader; 导入java.io.FileInputStream; 导入java.io.FileNotFoundException; 导入java.io.FileReader; 导入java.io.IOException; 导入java.io.InputStream; 导入java.io.InputStreamReader; 导入java.io.StringReader; 导入java.io.StringWriter; 导入java.net.InetAddress; 导入javax.xml.bind.JAXBContext; 导入javax.xml.bind.JAXBException; 导入javax.xml.bind.Marshaller; 导入javax.xml.bind.Unmarshaller; 导入org.apache.commons.net.ntp.NTPUDPClient; 导入org.apache.commons.net.ntp.TimeInfo; 班级员工{ 公共int id; 私人双平衡; 公共雇员(内部id){ //TODO自动生成的构造函数存根 this.id=id; } 公共收费(双倍价格){ 如果(价格+余额>1) 返回false; 余额+=价格; 返回true; } } B类雇员{ 公共B(内部id){ 超级(id); //TODO自动生成的构造函数存根 } 公共收费(双倍价格){ 布尔值isSuccess=超级费用(价格); 如果(!isSuccess) 超级。收费(5);//***这里是罚款 返回成功; } } 公共类TestStrig{ 公共静态void main(字符串[]args)引发异常{ //固定扫描仪 扫描器Toby=新扫描器(System.in); //声明变量 双Leg1; 双Leg2; 双斜边; 双重选择; //双num1 //双num2 //双重回答 //布尔假; //提供计算选择的程序开始 while(true){ System.out.println(“这个程序是一个基本的计算器,也知道勾股定理”); System.out.println(“此程序可以求解斜边或一条腿\也不能将其用作简单的计算器”); System.out.println(“你是在解斜边还是一条腿?”); System.out.println(“1表示斜边\n2表示支腿\n3要退出”); choice=Toby.nextDouble(); //从else if语句开始 //您在上面输入的内容与下面的选项相对应 //while((Toby.nextDouble()!=1); while(true){ 如果(选项==1) { //找斜边 System.out.println(“输入第一段:”); Leg1=Toby.nextDouble(); System.out.println(“输入第二段:”); Leg2=Toby.nextDouble(); 斜边=Leg1*Leg1+Leg2*Leg2; System.out.println(数学sqrt(斜边)); 打破 } else if(选项==2) { //寻找腿 System.out.println(“输入腿:”); 立法1=
 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);
  }
}
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;
            }
            }
            }
        }
}