2016-05-23 78 views
3

我对编程本身并不陌生,我已经研究了C#很长一段时间了,但我自己并没有真正做过很多练习,我现在刚开始使用java,因为我想要做Android应用程序和测试我新学到的知识,我想要做一个基本的控制台计算器,这里就是我有这么远:如何在java中退出循环?

package calculatorSource; 

import java.util.*; 

public class calculatorJava { 
    private static Scanner input; 

    public static void main(String[] args) { 
     System.out.println("Select an operation"); 

     String choice = null; 
     input = new Scanner(System.in); 

     while(choice == null) { 
      System.out.println("Type 'a' for adition, 's' for subtraction, 'm' for multiplication," + " 'd' for division, or 'mo' for module."); 
      choice = input.next(); 
     } 

     while(choice != null) { 
      if(choice.equals("a")) { 
       System.out.println(adition(0, 0)); 
       choice = null; 
      } else if(choice.equals("s")) { 
       System.out.println(subtraction(0, 0)); 
       choice = null; 
      } else if(choice.equals("m")) { 
       System.out.println(multiplication(0, 0)); 
       choice = null; 
      } else if(choice.equals("d")) { 
       System.out.println(division(0, 0)); 
       choice = null; 
      } else if(choice.equals("mo")) { 
       System.out.println(module(0, 0)); 
       choice = null; 
      } else { 
       System.out.println("Option not available, please try again."); 
       choice = null; 
      } 
     } 
    } 

    public static float adition(float n1, float n2) { 
     input = new Scanner(System.in); 

     float result; 

     System.out.println("Enter first number:"); 
     n1 = input.nextFloat(); 

     System.out.println("Enter second number:"); 
     n2 = input.nextFloat(); 

     result = n1 + n2; 

     System.out.println("Result is: " + result); 

     return adition(result, result); 
    } 

    public static float subtraction(float n1, float n2) { 
     input = new Scanner(System.in); 

     float result; 

     System.out.println("Enter first number:"); 
     n1 = input.nextFloat(); 

     System.out.println("Enter second number:"); 
     n2 = input.nextFloat(); 

     result = n1 - n2; 

     System.out.println("Result is: " + result); 

     return subtraction(result, result); 
    } 

    public static float multiplication(float n1, float n2) { 
     input = new Scanner(System.in); 

     float result; 

     System.out.println("Enter first number:"); 
     n1 = input.nextFloat(); 

     System.out.println("Enter second number:"); 
     n2 = input.nextFloat(); 

     result = n1 * n2; 

     System.out.println("Result is: " + result); 

     return multiplication(result, result); 
    } 

    public static float division(float n1, float n2) { 
     input = new Scanner(System.in); 

     float result; 

     System.out.println("Enter first number:"); 
     n1 = input.nextFloat(); 

     System.out.println("Enter second number:"); 
     n2 = input.nextFloat(); 

     result = n1/n2; 

     System.out.println("Result is: " + result); 

     return division(result, result); 
    } 

    public static float module(float n1, float n2) { 
     input = new Scanner(System.in); 

     float result; 

     System.out.println("Enter first number:"); 
     n1 = input.nextFloat(); 

     System.out.println("Enter second number:"); 
     n2 = input.nextFloat(); 

     result = n1 % n2; 

     System.out.println("Result is: " + result); 

     return module(result, result); 
    } 
} 

我知道这可能不是最好的或更有效的计算器在那里,但就像我说的,我刚刚开始使用java,这是迄今为止我所知道的几乎所有,该程序的作品,因为我可以做一个补充,一个部门或我选择的其他任何东西,但在此之后,我想让它选择选择一个不同的操作,我把“choice = null”放在返回之后,但它似乎并没有工作,I我已经尝试了几件事情,但我开始认为我可能误解了实际的回报,所以我认为最好转向你们寻求帮助。

任何意见表示赞赏,谢谢!

+7

而不是'返回adition ,result);'在你的'adition'方法中,返回'result',否则你只要重复相同的方法直到堆栈溢出(对于其他方法也是如此) –

+1

'while(choice!= null ){'循环没有必要,因为你总是设置'choice = null;'。因为你也不会改变那个循环中的'choice'的值,所以你可以直接删除循环。 –

回答

3
adition方法

而不是

return adition(result, result); 

,返回result

否则,你只是重复相同的方法,直到堆栈溢出。 (其他方法也一样)。


while (choice != null) {循环是没有必要的,因为你总是设置choice = null;。既然你也不会在那个循环中改变choice的值,那么你可以直接删除循环。


有在传递参数到adition(ETC)的方法是没有意义的,因为你总是覆盖它们。只需在这些方法中声明它们为局部变量:

public static float adition() { // No parameters 
    // ... 
    float n1 = input.nextFloat(); 
    // ... 
    float n2 = input.nextFloat(); 
+1

I'在这里只需要添加一个'Scanner'实例,其中每个方法都会创建一个新的实例。 – SomeJavaGuy

+0

他需要更改Scanner循环以便在操作之后能够返回到那里问。静态方法更容易。**但在此之后,我希望它给我选择不同的操作** – AxelH

+0

@AxelH是的,这正是我所要求的,我的计算器已经工作(种),但似乎没有人读过最后一个部分,我希望它备份,但我真诚地不知道如何或在哪里放置指令返回,你是否介意扩大你的答案?谢谢! –

2

您正在无限地递归调用所有方法。 choice = null是可以的。它会终止,如果你会纠正你的代码:
而不是

MethodaName(result, result); 

只写

return result; 

而且如果方法本身采取从用户的输入可以不传递任何参数。

1

看来你误解了函数内部的return语句。 每当我们在java中调用一个函数/方法时,它应该有一个返回类型,并且返回的值应该是该函数在处理参数后实际返回的值。

因此,在你的情况下,添加功能应该是这样的

public static float adition(float n1, float n2) { 
    input = new Scanner(System.in); 

    float result; 

    System.out.println("Enter first number:"); 
    n1 = input.nextFloat(); 

    System.out.println("Enter second number:"); 
    n2 = input.nextFloat(); 

    result = n1 + n2; 

    System.out.println("Result is: " + result); 

    return result; 
    } 

这里的结果变量是它被创建你的功能的处理的值。

在你的情况下,你在return语句中调用了相同的函数,并且它正在成为一个递归函数,而在递归函数中,我们总是需要一个break语句来实现它。

1

有两件事你可以做。

  1. 如果你想运行操作只是一次,然后代替

    return addition(result,result) 
    

    使用

    return result. 
    

2。假设你想运行循环加法,直到用户说,他想做其他操作,然后使用全局变量,并假设你在用户输入-999时添加条件,然后返回。 因此,在第二种情况下,循环直到用户输入-999。

import java.util.*; 

public class calculatorJava { 
    private static Scanner input; 
    static int flag = 1; 
    public static void main(String[] args) { 
    System.out.println("Select an operation"); 

    String choice = null; 
    input = new Scanner(System.in); 

    while (choice == null) { 
     System.out.println(
      "Type 'a' for adition, 's' for subtraction, 'm' for multiplication," 
       + " 'd' for division, or 'mo' for module."); 
     choice = input.next(); 
    } 

    while (choice != null) { 
     if (choice.equals("a")) { 
      flag = 1; 
     System.out.println(adition(0, 0)); 
     //choice = null; 
     } else if (choice.equals("s")) { 
      flag = 1; 
     System.out.println(subtraction(0, 0)); 
     //choice = null; 
     } else if (choice.equals("m")) { 
      flag = 1; 
     System.out.println(multiplication(0, 0)); 
     //choice = null; 
     } else if (choice.equals("d")) { 
      flag = 1; 
     System.out.println(division(0, 0)); 
     //choice = null; 
     } else if (choice.equals("mo")) { 
      flag = 1; 
     System.out.println(module(0, 0)); 
     //choice = null; 
     } else { 
     System.out.println("Option not available, please try again."); 
     choice = null; 
     } 
    } 
    } 

    public static float adition(float n1, float n2) { 
    input = new Scanner(System.in); 

    float result; 

    System.out.println("Enter first number:"); 
    n1 = input.nextFloat(); 
    if(n1 == -999){ 
     flag = 0; 
    } 
    System.out.println("Enter second number:"); 
    n2 = input.nextFloat(); 

    result = n1 + n2; 

    System.out.println("Result is: " + result); 
    if(flag==0) 
     return result; 

    return adition(result, result); 
    } 

    public static float subtraction(float n1, float n2) { 
    input = new Scanner(System.in); 

    float result; 

    System.out.println("Enter first number:"); 
    n1 = input.nextFloat(); 
    if(n1 == -999){ 
     flag = 0; 
    } 
    System.out.println("Enter second number:"); 
    n2 = input.nextFloat(); 

    result = n1 - n2; 

    System.out.println("Result is: " + result); 
    if(flag==0) 
     return result; 
    return subtraction(result, result); 
    } 

    public static float multiplication(float n1, float n2) { 
    input = new Scanner(System.in); 

    float result; 

    System.out.println("Enter first number:"); 
    n1 = input.nextFloat(); 
    if(n1 == -999){ 
     flag = 0; 
    } 
    System.out.println("Enter second number:"); 
    n2 = input.nextFloat(); 

    result = n1 * n2; 

    System.out.println("Result is: " + result); 
    if(flag==0) 
     return result; 
    return multiplication(result, result); 
    } 

    public static float division(float n1, float n2) { 
    input = new Scanner(System.in); 

    float result; 

    System.out.println("Enter first number:"); 
    n1 = input.nextFloat(); 
    if(n1 == -999){ 
     flag = 0; 
    } 
    System.out.println("Enter second number:"); 
    n2 = input.nextFloat(); 

    result = n1/n2; 

    System.out.println("Result is: " + result); 
    if(flag==0) 
     return result; 
    return division(result, result); 
    } 

    public static float module(float n1, float n2) { 
    input = new Scanner(System.in); 

    float result; 

    System.out.println("Enter first number:"); 
    n1 = input.nextFloat(); 
    if(n1 == -999){ 
     flag = 0; 
    } 
    System.out.println("Enter second number:"); 
    n2 = input.nextFloat(); 

    result = n1 % n2; 

    System.out.println("Result is: " + result); 
    if(flag==0) 
     return result; 
    return module(result, result); 
    } 
} 
1

要再次询问

首先,对于另外未有用while循环中,如果能够足够了,但是没用,因为你检查前值(在第一循环)

这是循环使用代码的一种可能性。

boolean letDoMath = true; 
    while(letDoMath) { // aka while(letDoMath == true){ 
     while(choice == null) { 
     System.out.println("Type 'a' for adition, 's' for subtraction, 'm' for multiplication," + " 'd' for division, or 'mo' for module."); 
      choice = input.next(); 
     } 
     //HERE YOU KNOW CHOISE ISN'T NULL BECAUSE OF THE WHILE LOOP, NO NEED TO CHECK AGAIN. 

     if(choice.equals("a")) { 
      System.out.println(adition(0, 0)); 
      choice = null; 
     } else if(choice.equals("s")) { 
      System.out.println(subtraction(0, 0)); 
      choice = null; 
     } else if(choice.equals("m")) { 
      System.out.println(multiplication(0, 0)); 
      choice = null; 
     } else if(choice.equals("d")) { 
      System.out.println(division(0, 0)); 
      choice = null; 
     } else if(choice.equals("mo")) { 
      System.out.println(module(0, 0)); 
      choice = null; 
     } else if(choice.equlas("end"){ 
      letDoMath = false; 
     } 
    } 

布尔同时给你的可能性,以循环的一次又一次,直到你输入end。这当然不完美,但这是一个开始。 (我没有尝试,甚至它做可能包含错误写在IDE中。

操作方法错误

一样精确无处不在,你的操作方法是递归的,除了调用本身在最后。卸下回加法(X,Y),就回到你做加法的结果

一些改良效果的想法:

  • 创建一个方法来获得一个数量RET它(它在每种方法中都很有用)。
  • 最好的方法是询问操作之外的数字是否将值传递给方法,而不是读取方法中的扫描器。 (当然,你需要确定选择的价值(实际上,你可以在选择之前询问数字),然后再询问是否已完成一项操作。