Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.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
Loops 嵌套循环Java_Loops_Nested - Fatal编程技术网

Loops 嵌套循环Java

Loops 嵌套循环Java,loops,nested,Loops,Nested,我正在尝试做一个家庭作业,使用继承和多态性,将极坐标转换为笛卡尔坐标,反之亦然,这也给出了点之间的距离、直线的斜率,以及给定用户输入的两个值的直线方程 我创建了我的父类,以及Polar和Cartesian类。我正试着去做一件事。调用我的test coordinates类中的方法(由于某种原因它无法识别),然后2。创建一个成功的嵌套循环 环路情况: 我有一个if语句,导致输入极坐标或笛卡尔坐标。这很有效,输入坐标后,会提示用户选择一个过程(获取直线斜率、直线方程、直线之间的距离等)。我不知道如何创

我正在尝试做一个家庭作业,使用继承和多态性,将极坐标转换为笛卡尔坐标,反之亦然,这也给出了点之间的距离、直线的斜率,以及给定用户输入的两个值的直线方程

我创建了我的父类,以及Polar和Cartesian类。我正试着去做一件事。调用我的test coordinates类中的方法(由于某种原因它无法识别),然后2。创建一个成功的嵌套循环

环路情况: 我有一个if语句,导致输入极坐标或笛卡尔坐标。这很有效,输入坐标后,会提示用户选择一个过程(获取直线斜率、直线方程、直线之间的距离等)。我不知道如何创建这个嵌套循环。每次我尝试时,一旦我完成了内部循环,它就会停止循环,并且不会返回到菜单的外部循环

如有任何见解,将不胜感激

父代码(调用方法时遇到问题):

代码:

import java.util.Scanner;


public class TestCoordinates {

    public static String getSelection(Scanner in) {

        System.out.println("(p) Polar Coordinates (r, theta (degrees))");
        System.out.println("(c) Cartesian Coordinates (x, y)");
        System.out.println("(x) Exit program");
        System.out.println("What type of coordinates?  Please enter p or c, or x to quit. ");
        return in.next();

    }

    public static void main(String [] args) {
        Scanner scan = new Scanner(System.in);

        String coordinateString = getSelection(scan);

        while(coordinateString.equalsIgnoreCase("p") || coordinateString.equalsIgnoreCase("c")) {


            if (coordinateString.equalsIgnoreCase("p")) {
                System.out.println("Coordinate 1 - Please enter the radius: ");
                int radius = scan.nextInt();
                System.out.println("Coordinate 1 - Please enter theta (degrees): ");
                int theta = scan.nextInt();
                System.out.println("Coordinate 2 - Please enter the radius: ");
                int radius1 = scan.nextInt();
                System.out.println("Coordinate 2 - Please enter theta (degrees): ");
                int theta1 = scan.nextInt();

                System.out.println("[1] Convert to Cartesian coordinates.");
                System.out.println("[2] Find the distance between the two points.");
                System.out.println("[3] Find the slope of the line between the points.");
                System.out.println("[4] Find the equation of the line between the points.");
                System.out.println("[5] Return to main menu.");

                System.out.println("What would you like to do? Enter the number: ");
                int num = scan.nextInt();





        /*if (num = 1) {
                    getCartesian();
                    System.out.println("The Cartesian coordinate for " + "(" + radius + "," + theta + ") is" + getCartesian);
                    System.out.println("The Cartesian coordinate for " + "(" + radius1 + "," + theta1 + ") is" + getCartesian);
                }*/

                /*if (int num = 2){
                    getDistance();
                    System.out.println("The distance between the Polar coordinates (" + radius + "," + theta + ") and " + radius1 +"," + theta1 + ") is" getDistance);
                }


                if (int num = 3) {
                    getSlopeOfLine;
                    System.out.println("The slope of the line between the Polar coordinates (" + radius + "," + theta + ") and " + radius1 +"," + theta1 + ") is" getSlopeOfLine);

                }

                if (int num = 4) {
                    System.out.println("The equation of the line between the points is: ")
                }

                else if (num = 5) {
                    return;
                }*/

如果需要帮助,您必须显示一些代码。我们需要查看一些代码。你的嵌套循环需要使用不同的变量。如果问题在你的循环条件下,作业问题的细节可能不相关。提供一个菜单/操作循环代码的简化示例,尽可能少地提供必要的辅助细节。如果我们可以看到您的内部和外部循环,我们可能可以看到您哪里出了问题“无法识别”。。。?
import java.util.Scanner;


public class TestCoordinates {

    public static String getSelection(Scanner in) {

        System.out.println("(p) Polar Coordinates (r, theta (degrees))");
        System.out.println("(c) Cartesian Coordinates (x, y)");
        System.out.println("(x) Exit program");
        System.out.println("What type of coordinates?  Please enter p or c, or x to quit. ");
        return in.next();

    }

    public static void main(String [] args) {
        Scanner scan = new Scanner(System.in);

        String coordinateString = getSelection(scan);

        while(coordinateString.equalsIgnoreCase("p") || coordinateString.equalsIgnoreCase("c")) {


            if (coordinateString.equalsIgnoreCase("p")) {
                System.out.println("Coordinate 1 - Please enter the radius: ");
                int radius = scan.nextInt();
                System.out.println("Coordinate 1 - Please enter theta (degrees): ");
                int theta = scan.nextInt();
                System.out.println("Coordinate 2 - Please enter the radius: ");
                int radius1 = scan.nextInt();
                System.out.println("Coordinate 2 - Please enter theta (degrees): ");
                int theta1 = scan.nextInt();

                System.out.println("[1] Convert to Cartesian coordinates.");
                System.out.println("[2] Find the distance between the two points.");
                System.out.println("[3] Find the slope of the line between the points.");
                System.out.println("[4] Find the equation of the line between the points.");
                System.out.println("[5] Return to main menu.");

                System.out.println("What would you like to do? Enter the number: ");
                int num = scan.nextInt();





        /*if (num = 1) {
                    getCartesian();
                    System.out.println("The Cartesian coordinate for " + "(" + radius + "," + theta + ") is" + getCartesian);
                    System.out.println("The Cartesian coordinate for " + "(" + radius1 + "," + theta1 + ") is" + getCartesian);
                }*/

                /*if (int num = 2){
                    getDistance();
                    System.out.println("The distance between the Polar coordinates (" + radius + "," + theta + ") and " + radius1 +"," + theta1 + ") is" getDistance);
                }


                if (int num = 3) {
                    getSlopeOfLine;
                    System.out.println("The slope of the line between the Polar coordinates (" + radius + "," + theta + ") and " + radius1 +"," + theta1 + ") is" getSlopeOfLine);

                }

                if (int num = 4) {
                    System.out.println("The equation of the line between the points is: ")
                }

                else if (num = 5) {
                    return;
                }*/