我怎样才能让这个java代码回到代码中的某个点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20831100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:38:54  来源:igfitidea点击:

How can I make this java code go back to a certain point in the code?

javaloopsnetbeanscalculator

提问by Henket

So as you could probably tell, it is a calculator. This is the second program I have made in Java, besides robotics code. I am pretty new to Java, so I don't know a ton about it. I want to be able to go back to the menu (lines 19-27) after it gives the answer to the question. If I can do this, I will put something like M for menu or E to exit after the answer. I want it so if they put M, it will go back to those lines of code to be able to start over again instead of restarting the program. Thanks, Matt.

所以你可能会说,它是一个计算器。这是我用 Java 编写的第二个程序,除了机器人代码。我对 Java 很陌生,所以我对它一无所知。在给出问题的答案后,我希望能够返回菜单(第 19-27 行)。如果我能做到这一点,我会在回答后输入 M 之类的东西作为菜单或 E 退出。我想要它,所以如果他们放 M,它会回到那些代码行,以便能够重新开始而不是重新启动程序。谢谢,马特。

public class Calculator {
public static void main(String[] args) {
    System.out.println("Calculator");
    System.out.println("----------");
    System.out.println("Press 1 for addition");
    System.out.println("Press 2 for subtraction");
    System.out.println("Press 3 for multiplication");
    System.out.println("Press 4 for division");
    int Menu;
    Scanner menu_scanner = new Scanner(System.in);
    Menu = menu_scanner.nextInt();
    if (Menu == 1){
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("Please enter the first number in the problem");
        System.out.println("--------------------------------------------");
        double iAddition1;
        double iAddition2;
        double aAddition;
        Scanner sAddition1 = new Scanner(System.in);
        iAddition1 = sAddition1.nextDouble();
        System.out.println("Please enter the second number in the problem");
        System.out.println("---------------------------------------------");
        Scanner sAddition2 = new Scanner(System.in);
        iAddition2 = sAddition2.nextDouble();
        aAddition = iAddition1 + iAddition2;
        System.out.println("The answer to " + iAddition1 + " + " + iAddition2 + " = " + aAddition);
    }
    if (Menu == 2){
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("Please enter the first number in the problem");
        System.out.println("--------------------------------------------");
        double iSubtraction1;
        Scanner sSubtraction1 = new Scanner(System.in);
        iSubtraction1 = sSubtraction1.nextDouble();
        System.out.println("Please enter the second number in the problem");
        System.out.println("---------------------------------------------");
        double iSubtraction2;
        Scanner sSubtraction2 = new Scanner(System.in);
        iSubtraction2 = sSubtraction2.nextDouble();
        double aSubtraction = iSubtraction1 - iSubtraction2;
        System.out.println("The answer to " + iSubtraction1 + " - " + iSubtraction2 + " = " + aSubtraction);
    }
    if (Menu == 3){
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("Please enter the first number in the problem");
        System.out.println("--------------------------------------------");
        double iMultiplication1;
        double iMultiplication2;
        Scanner sMultiplication1 = new Scanner(System.in);
        iMultiplication1 = sMultiplication1.nextDouble();
        System.out.println("Please enter the second number in the problem");
        System.out.println("---------------------------------------------");
        Scanner sMultiplication2 = new Scanner(System.in);
        iMultiplication2 = sMultiplication2.nextDouble();
        double aMultiplication = iMultiplication1 * iMultiplication2;
        System.out.println("The answer to " + iMultiplication1 + " * " + iMultiplication2 + " = " + aMultiplication);
    }
    if (Menu == 4){
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("Please enter the first number in the problem");
        System.out.println("--------------------------------------------");
        Scanner sDivision1 = new Scanner(System.in);
        double iDivision1;
        iDivision1 = sDivision1.nextDouble();
        System.out.println("Please enter the second number in the problem");
        System.out.println("---------------------------------------------");
        Scanner sDivision2 = new Scanner(System.in);
        double iDivision2;
        iDivision2 = sDivision1.nextDouble();
        double aDivision = iDivision1 / iDivision2;
        System.out.println("The answer to " + iDivision1 + " / " + iDivision2 + " = " + aDivision);
    }
}    
}

回答by Tim B

You are looking for a loop. Probably a while loop.

你正在寻找一个循环。可能是一个while循环。

回答by splrs

A whileloop with a condition of your choice (such as checking a certain value isn't encountered), or a forloop with a certain number of iterations if you're content with doing the set of operations, say, 10 times for example.

一个while与您选择的条件下环(如检查一定值时没有遇到),或for具有一定的迭代次数的循环,如果你满足于做的一组操作,说,例如10倍。

One solution is to wrap the entire block of code after Menu = menu_scanner.nextIntin a whileloop such as while(Menu < 5), and offer another option of 5 to exit the program.

一种解决方案是将整个代码块包装Menu = menu_scanner.nextIntwhile循环之后,例如while(Menu < 5),并提供另一个选项 5 以退出程序。

Essentially though, you need to learn about the control structures in Java from a guide e.g. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html

但本质上,您需要从指南中了解 Java 中的控制结构,例如http://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html

回答by user987339

First off all try to group parts of your program into functions. You can do a loops with a whileor forloop. You can do this also with labels.

首先,尝试将程序的各个部分分组为函数。你可以用 awhileforloop做一个循环。您也可以使用标签来做到这一点。

回答by Creepers-Help

I am also new to Java programing, but i agree a "While Loop"is what you need.

我也是 Java 编程的新手,但我同意“While Loop”是您所需要的。

My Version of your Code:

我的代码版本:

http://pastebin.com/H34FRWP3

http://pastebin.com/H34FRWP3

Good Luck :)

祝你好运 :)

I use Eclipse IDE, it has helped meALOT.

我使用Eclipse IDE,它帮助我ALOT

http://eclipse.org

http://eclipse.org

回答by KhaledMohamedP

Make sure you reduce the amount of code you have by using method. I also think you should use cases for e.g

确保通过使用方法减少代码量。我也认为你应该使用案例例如

public static double doMath(double firstNum,double secondNum){
    Scanner scan = new Scanner();  
    System.out.println("Enter the operation number?"); 
    int i = scan.nextInt(); 

    switch (i) {
        case 1: total = firstnum + secondNum; 
        break;
        case 2: total = firstnum - secondNum; 
        break;
        ......
    }

    return total; 
}

This method take two numbers (doMath(first, second)) and return the total/answer.

此方法采用两个数字(doMath( first, second) )并返回总数/答案。

It's a very good start but every time you see yourself repeating lines of code take advantage of methods/functions they are very important in programming. Also use while loops to repeat the call until the user wants to end the program.

这是一个很好的开始,但是每次你看到自己重复的代码行都利用了方法/函数,它们在编程中非常重要。还可以使用 while 循环重复调用,直到用户想要结束程序。

Wish you the best of luck

祝你好运