2017-09-25 107 views
1

我是編程方面的新手,我現在正在我的高中開設一門入門課程。我們現在正在處理的其中一項任務是向用戶詢問他們想要計算的公式(各種面積/體積/等公式類型),向他們詢問計算所需的數據,然後給他們答案。我對編程非常感興趣,主要是因爲這個任務,因爲我已經意識到這項工作可以做多少大腦傳情。如何創建一個while循環,只要用戶在每個循環結束時輸入正確的標記/代碼,該while循環就會繼續運行?

因此,我決定走得更遠一點。我清理了我的雜亂代碼,現在我正在嘗試創建一個while循環,它允許用戶在不運行程序的情況下繼續計算公式,並在輸入不正確的數據時給出錯誤消息。我已經想出了後面的部分,但是我想在將第二個問題的解決方案放入代碼之前實現前者。

import java.util.Scanner; 
public class FormulaRemake { 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    Scanner input = new Scanner(System.in); 

    System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n","What formula would you like to calculate?","Area of a Circle (AOC)","Circumference of a Circle (COC)","Area of a Trapezoid (AOT)","Volume of a Cylinder (VOC)","Volume of a Sphere (VOS)","Volume of a Cone (VON)"); 
    String formula = input.nextLine(); 

    while((formula.equalsIgnoreCase("AOC"))||(formula.equalsIgnoreCase("COC"))||(formula.equalsIgnoreCase("AOT"))||(formula.equalsIgnoreCase("VOC"))||(formula.equalsIgnoreCase("VOS"))||(formula.equalsIgnoreCase("VON"))) 
      { 
     if(formula.equalsIgnoreCase("AOC")) 
     { 
      System.out.println("What is the circle's radius?"); 
      double inputRadius = input.nextDouble(); 
      System.out.printf("%.2f",Math.PI*(Math.pow(inputRadius,2))); 

     } 

     if(formula.equalsIgnoreCase("COC")) 
     { 
      System.out.print("What is the circle's radius?"); 
     double inputRadius = input.nextDouble(); 
     System.out.printf("%.2f",2*Math.PI*inputRadius); 
     } 

     if(formula.equalsIgnoreCase("AOT")) 
     { 
      System.out.println("What is the height of the trapezoid?"); 
      double inputHeight = input.nextDouble(); 
      System.out.println("What is the first length of the trapezoid?"); 
      double inputLengthFirst = input.nextDouble(); 
      System.out.println("What is the second length of the trapezoid?"); 
      double inputLengthSecond = input.nextDouble(); 
      System.out.printf("%.2f",(1/2)*inputHeight*(inputLengthFirst+inputLengthSecond)); 
     } 

     if(formula.equalsIgnoreCase("VOC")) 
     { 
      System.out.println("What is the cylinder's radius?"); 
      double inputRadius = input.nextDouble(); 
      System.out.println("What is the cylinder's height?"); 
      double inputHeight = input.nextDouble(); 
      System.out.printf("%.2f",(Math.PI*(Math.pow(inputRadius,2)*inputHeight))); 
     } 

     if(formula.equalsIgnoreCase("VOS")) 
     { 
      System.out.println("What is the sphere's radius?"); 
      double inputRadius = input.nextDouble(); 
      System.out.println((4.0/3.0) * Math.PI * Math.pow(inputRadius, 3)); 
      System.out.printf("%.2f",(4.0/3.0)*Math.PI*Math.pow(inputRadius, 3)); 
     } 

     if(formula.equalsIgnoreCase("VON")) 
     { 
      System.out.println("What is the cone's radius?"); 
      double inputRadius = input.nextDouble(); 
      System.out.println("What is the cone's height?"); 
      double inputHeight = input.nextDouble(); 
      System.out.printf("%.2f",(1.0/3.0)*Math.PI*(Math.pow(inputRadius,2)*inputHeight)); 
      } 

} 


} 
} 

我有一段時間的線,但我不確定如何處理它。我不知道如何讓我的代碼循環,while(hehe,while)活動行在while塊內。我應該保存這個類,創建一個新的類,然後在另一個類引用它,就像

串mathDone = TRUE(在每個式結束)

while(mathDone.equalsIgnoreCase(true)) 
{ 
String continueCalculation = input.nextLine; 
System.out.println("Would you like to continue calculation?"); 
if(continueCalculation.equalsIgnoreCase("yes"))||(continueCalulation.equalsIgnoreCase("y")) 

{ (無論運行命令是放在這裏)formulaRemake }} 從

旁白,我有點無知。我知道有關於類似主題的stackoverflow上的帖子,但我無法弄清楚如何將它們應用於我的情況。我太新了(顯然,太愚蠢)想弄清楚如何使用這些帖子來幫助我。

+2

採摘一種語言並將其作爲標籤添加到您的問題將是一個好的開始。請花一些時間[閱讀如何提出好問題](http://stackoverflow.com/help/how-to-ask)。 –

+0

你知道,我以爲我做到了。我打開了兩個單獨的選項卡,一個語法不好,另一個語法正確。 它似乎是我複製並將錯誤的主題粘貼到錯誤的標籤後編輯。主要歉意): –

回答

2

你問的是怎樣的一個菜單,即使是隻有兩個選擇:

  1. 選擇和計算公式
  2. 退出

的代碼第一個選項已經可用。我認爲它正常工作。最快的方式(但不推薦)將整個代碼放在while循環中。我建議你讓主函數只有這個while循環,它會調用另一個函數,你把你當前的代碼放在哪裏。如果您還不知道哪些功能尚未完成:它們在很多方面都很有用,其中之一是使代碼更具可讀性,因爲您將代碼的重要部分保留在一個地方。例如,如果您有一個非常長的數學任務,需要在代碼中的許多位置進行計算,最好不要將此計算複製到每個需要的部分,並且您可以調用許多代碼行只有一條線。

編輯澄清:

的方式你的程序是現在,在一個函數(主函數)的一切是,如果你把編程這種方式只會變得更長。這裏出現的問題是,代碼越長,它的可讀性和可維護性越差。雖然這對於一個小測試程序來說可能不是問題,但您會在團隊項目或公司等中遇到上述問題。

要解決這些問題,您可以通過添加函數來更改代碼,這可能是您應該檢查的下一章節。函數/方法是代碼的一部分,可以從每個地方調用,在哪裏允許。這是一個功能基本上看起來像:

visibility static/nonStatic type functionName (parameterType parameterName) { 
//codebody 
} 

可見:無論是公營,私營,封裝(如果省略)或保護 我們將使用唯一的公共眼下,這意味着,該功能是隨處可見。 static或nonStatic(如果省略):static表示「每個類一次」,這使得該函數可以由實例(nonStatic)或類(static)提供。但是現在這對你來說並不重要。 類型:函數的返回類型。您可以選擇一種數據類型,並將所選類/數據類型的實例返回到函數被調用的位置。如果沒有/必須返回,你可以選擇void類型。 functionName:可以自由選擇。注意:使用camelCase 參數列表:(參數類型1參數1,參數類型2參數2,...),例如(int a,String b)。在函數中你有一個int a和一個String b。

你的方案應該是這樣的:

//Now your calculations etc are put into this function. 
//Now you can write simply myFunction() somewhere else (outside of this function) 
//and the execution of the code where the function is called will be 
//stopped and wait until the function is finished (until the code of 
//the function is executed) 
public static void myFunction() { 

Scanner input = new Scanner(System.in); 

System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n","What formula would you like to calculate?","Area of a Circle (AOC)","Circumference of a Circle (COC)","Area of a Trapezoid (AOT)","Volume of a Cylinder (VOC)","Volume of a Sphere (VOS)","Volume of a Cone (VON)"); 
String formula = input.nextLine(); 

while((formula.equalsIgnoreCase("AOC"))||(formula.equalsIgnoreCase("COC"))||(formula.equalsIgnoreCase("AOT"))||(formula.equalsIgnoreCase("VOC"))||(formula.equalsIgnoreCase("VOS"))||(formula.equalsIgnoreCase("VON"))) 
     { 
    if(formula.equalsIgnoreCase("AOC")) 
    { 
     System.out.println("What is the circle's radius?"); 
     double inputRadius = input.nextDouble(); 
     System.out.printf("%.2f",Math.PI*(Math.pow(inputRadius,2))); 

    } 

    if(formula.equalsIgnoreCase("COC")) 
    { 
     System.out.print("What is the circle's radius?"); 
    double inputRadius = input.nextDouble(); 
    System.out.printf("%.2f",2*Math.PI*inputRadius); 
    } 

    if(formula.equalsIgnoreCase("AOT")) 
    { 
     System.out.println("What is the height of the trapezoid?"); 
     double inputHeight = input.nextDouble(); 
     System.out.println("What is the first length of the trapezoid?"); 
     double inputLengthFirst = input.nextDouble(); 
     System.out.println("What is the second length of the trapezoid?"); 
     double inputLengthSecond = input.nextDouble(); 
     System.out.printf("%.2f",(1/2)*inputHeight*(inputLengthFirst+inputLengthSecond)); 
    } 

    if(formula.equalsIgnoreCase("VOC")) 
    { 
     System.out.println("What is the cylinder's radius?"); 
     double inputRadius = input.nextDouble(); 
     System.out.println("What is the cylinder's height?"); 
     double inputHeight = input.nextDouble(); 
     System.out.printf("%.2f",(Math.PI*(Math.pow(inputRadius,2)*inputHeight))); 
    } 

    if(formula.equalsIgnoreCase("VOS")) 
    { 
     System.out.println("What is the sphere's radius?"); 
     double inputRadius = input.nextDouble(); 
     System.out.println((4.0/3.0) * Math.PI * Math.pow(inputRadius, 3)); 
     System.out.printf("%.2f",(4.0/3.0)*Math.PI*Math.pow(inputRadius, 3)); 
    } 
    if(formula.equalsIgnoreCase("VON")) 
    { 
     System.out.println("What is the cone's radius?"); 
     double inputRadius = input.nextDouble(); 
     System.out.println("What is the cone's height?"); 
     double inputHeight = input.nextDouble(); 
     System.out.printf("%.2f",(1.0/3.0)*Math.PI*(Math.pow(inputRadius,2)*inputHeight)); 
    } 
} 
} 


public static void main(String[] args) { //the main function, containing only the menu loop 
    Scanner input = new Scanner(System.in); 
    boolean stop = false; 
    while(!stop) { //menu loop 
    myFunction(); //Your formulas will be calculated in this function 
    System.out.println("Do you want to calculate another formula? 0: no, 1: yes")//ask the user if another calculation shall be done 
    int userInput = input.nextInt(); 
    if (userInput == 0) {//set stop accordingly, stop = true will exit the loop 
     stop = true; 
    } else if (userInput == 1) { // 
     stop = false; 
    } 

    } 
} 

我有最後一點要注意:該功能還很長這樣的,非常不可讀。你下一步可以使代碼通過進一步分裂更具可讀性功能爲更小的功能(你可以調用內部的另一個函數的函數)

+0

我有點理解你在說什麼,但是......並不完全。 :P 當你說「輸入代碼,準備工作等等」時,你指的是像聲明變量和內容那樣的東西,然後實現boolean/while和函數,然後在底部使用具有數學函數的實際代碼體? –

+0

對不起,我讓你感到困惑^^在這部分中,我建議你在進入循環之前聲明變量並完成所有應該執行的操作。我知道在你顯示的代碼中,這可能只是掃描器的情況,它必須初始化以詢問用戶程序是應該繼續還是停止,但是我認爲如果有更多的代碼,你可能沒有包含在你的問題中,所以你不要假定除了while循環之外,你不能在主函數中放置任何東西。 –

+0

我肯定會推薦你將你的代碼分解成函數。當然,如果你圍繞它進行循環,你的程序也會工作,但如果你想長遠,這是不好的做法。雖然現在它會好的,你會知道你的程序是如何工作的,以及在什麼地方被執行,你不會知道它在一個月的時間裏,當你回來重溫你是如何做到的。更糟糕的是,當你與同一代碼的人一起工作時,他們必須/希望能夠輕鬆地閱讀你的代碼。 –

1

使用continue和break語句,例如:

i=input; 

while(true) 
{ 
    if(i==1){break;} 
    else 
    { 
    i=input; 
    continue; 
    } 
}