2016-05-23 92 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它(它在每種方法中都很有用)。
  • 最好的方法是詢問操作之外的數字是否將值傳遞給方法,而不是讀取方法中的掃描器。 (當然,你需要確定選擇的價值(實際上,你可以在選擇之前詢問數字),然後再詢問是否已完成一項操作。