如何让这个java代码回到代码中的某一点?

时间:2013-12-29 23:06:03

标签: java loops netbeans calculator

你可能会说,这是一个计算器。这是我用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);
    }
}    
}

5 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:5)

一个while循环,其中包含您选择的条件(例如,不会检查某个值),或者如果您满足于执行某个迭代次数的for循环例如,这组操作,例如10次。

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

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

答案 2 :(得分:2)

首先尝试将程序的各个部分分组为函数。您可以使用whilefor循环执行循环。您也可以使用labels

执行此操作

答案 3 :(得分:1)

确保使用方法减少了您拥有的代码量。我也认为你应该使用例如

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

此方法需要两个数字( doMath 第一个第二个))并返回总数/答案。

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

祝你好运

答案 4 :(得分:-2)

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

我的代码版本:

http://pastebin.com/H34FRWP3

祝你好运:)

我使用Eclipse IDE,它帮助了我 ALOT

http://eclipse.org