2014-05-15 37 views
-1
public static void main(String[] args) throws Exception 
{ 
    //intro 
    char rt,yn; 
    double v1,v2,v3, 
     w,  //width 
     d,  //depth 
     pc,  //pool capacity 
     rof= 50, //rate of flow 
     v,  //volume 
     ttf,  //time to fill 
     cap  //unit capacity 
     ; 
    int f; 

    Scanner sc = new Scanner(System.in); 

    System.out.println("Press 'enter' to get started"); 
    sc.nextLine(); 

    System.out.println("Please enter the pool type, press 'enter' after you finished"); 
    do { 
     System.out.println("Please enter a alphabet"); 
     System.out.println("'S'for Small(20x12x4)"); 
     System.out.println("'L'for Large(30x20x10)"); 
     System.out.println("'C'for Custom new size"); 

     //read rt S L C 
     rt= (char)System.in.read(); System.in.read(); 
     while (rt!='S' && rt!='s' && rt!='L' && rt!='l' && rt!='C' && rt!='c') 
     { 
      System.out.println("invalid input"); 
      System.out.println("please enter a alphabet"); 
      sc.next(); 
     } 

     if (rt=='S'||rt=='s') 
     { 
      calc(20,12,4); 
     } 
     else if(rt=='L'||rt=='l') 
     { 
      calc(20,12,4); 
     } 
     else if(rt=='C'||rt=='c') 
     { 
      System.out.println("Please enter the pool sizes, press enter after each side"); 
      System.out.println("Please enter Numbers, or errors may occur"); 
      System.out.println("now please enter your width(in ft)>>"); 

      v1= sc.nextDouble(); sc.nextLine(); 
      System.out.println("Please enter your length(in ft)>>"); 
      v2=sc.nextDouble(); sc.nextLine(); 
      System.out.println("Please enter your depth(in ft)>>"); 
      v3=sc.nextDouble(); sc.nextLine(); 
      calc(v1,v2,v3); 
     } 

     yn= (char)System.in.read(); 
    } 
    while(yn=='Y'); 

    System.out.println("Thank you for using"); 
} 

public static void calc(double l, double w, double d) throws Exception 
{ 
    double 
     pc,  //pool capacity 
     rof= 50, //rate of flow 
     v,  //volume 
     ttf1, //time to fill, whole num 
     cap= 7.5 //unit capacity 
     ; 

    v = l*w*d; 
    pc = v*cap; 
    ttf1 = pc /(rof*60); 

    System.out.println("It will take "+ ttf1 + " hours to fill up a pool with length: " + 
         l+" ft, width: "+w+" ft, and the depth of"+d+" ft"); 
    System.in.read(); 
    System.out.println("want do another one? Y/anykey"); 
} 

如果我運行此操作,在第一次輸入Y之後第二次返回無效輸入......導致此錯誤的原因是什麼?有沒有辦法避免這種情況發生?Java:循環後無法獲得正確的輸入

+1

我建議讓你的代碼看起來更乾淨。 –

回答

0

現在,它的工作。

import java.io.IOException; 
import java.util.Scanner; 


public class DataProva { 

    public static void main(String[] args) { 
     //intro 
     char rt = 0; 
     char yn = 'Y'; 
     double v1,v2,v3, 
      w,  //width 
      d,  //depth 
      pc,  //pool capacity 
      rof= 50, //rate of flow 
      v,  //volume 
      ttf,  //time to fill 
      cap  //unit capacity 
      ; 
     int f; 

     Scanner sc = new Scanner(System.in); 

     System.out.println("Press 'enter' to get started"); 
     sc.nextLine(); 
     sc.reset(); 

     System.out.println("Please enter the pool type, press 'enter' after you finished"); 
     while(yn=='Y') { 
      System.out.println("Please enter a alphabet"); 
      System.out.println("'S'for Small(20x12x4)"); 
      System.out.println("'L'for Large(30x20x10)"); 
      System.out.println("'C'for Custom new size"); 

      //read rt S L C 

      try { 
       rt= (char) System.in.read(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      while (rt!='S' && rt!='s' && rt!='L' && rt!='l' && rt!='C' && rt!='c') 
      { 
       System.out.println("invalid input"); 
       System.out.println("please enter a alphabet"); 

       rt=(char) sc.next().charAt(0); 
      } 

      if (rt=='S'||rt=='s') 
      { 
       try { 
        calc(20,12,4); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      else if(rt=='L'||rt=='l') 
      { 
       try { 
        calc(20,12,4); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      else if(rt=='C'||rt=='c') 
      { 
       System.out.println("Please enter the pool sizes, press enter after each side"); 
       System.out.println("Please enter Numbers, or errors may occur"); 
       System.out.println("now please enter your width(in ft)>>"); 

       v1= sc.nextDouble(); sc.nextLine(); 
       System.out.println("Please enter your length(in ft)>>"); 
       v2=sc.nextDouble(); sc.nextLine(); 
       System.out.println("Please enter your depth(in ft)>>"); 
       v3=sc.nextDouble(); sc.nextLine(); 
       try { 
        calc(v1,v2,v3); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      System.out.println("want do another one? Y/anykey"); 
      sc.reset(); 
      try { 
       System.in.read(); 
       sc.next(); 
       yn= (char) System.in.read(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      sc.reset(); 





     } 

     System.out.println("Thank you for using"); 
     sc.close(); 

    } 

    public static void calc(double l, double w, double d) throws Exception 
    { 
     double 
      pc,  //pool capacity 
      rof= 50, //rate of flow 
      v,  //volume 
      ttf1, //time to fill, whole num 
      cap= 7.5 //unit capacity 
      ; 

     v = l*w*d; 
     pc = v*cap; 
     ttf1 = pc /(rof*60); 

     System.out.println("It will take "+ ttf1 + " hours to fill up a pool with length: " + 
          l+" ft, width: "+w+" ft, and the depth of"+d+" ft"); 


    } 


} 

您在System.in中讀取a/r時遇到了問題。按Return鍵導致返回字符。 在你第一次檢查時,你沒有將sc.next()分配給rt,所以它只是無休止地循環。因爲rt總是和以前一樣。

作爲一般規則,main不應該拋出Exception,因爲沒有人可以管理該Exception。你應該使用try catch來管理Exception。

您的變量應該每次都被初始化。

你的代碼非常程序化,你可以用對象做更好的思考。

如果你使用Wrapper類,它會更好(char的包裝類是Charachter)。

+0

這有助於很多,即時通訊仍然是一個新手到Java,謝謝你這麼多 – user3642243

+0

@ user3642243那麼,作爲一個提示, 你可以想像一個像他的屬性的OBJECT池池。 ,然後用Pool pool = new Pool(attributes) 構建一個對象,然後使用它進行操作。 無論如何如果你滿意接受我的回答:) – user3384514