2016-07-06 66 views
-1

我有幾個調試問題,我無法弄清楚。我在下面的代碼中評論了他們,如果有人能糾正它並解釋錯誤發生的原因,我會非常感激。我是新來的Java,這是我的工作Java調試,語法問題

public class handleexceptions2 { 

int sideA = -1; 
int sideB = -1; 
Scanner input = new Scanner(System.in){ 
do //Syntax Error on Token "do", delete this token 
{ 
    System.out.println("Enter your choice (a/b/c/q) : "); 
    char ch = in.nextChar(); 
    switch(ch) 
    { 
     case 'a': sideA = in.nextDouble(); 
        if(sideA<0) 
        System.out.println("Error! Please enter a valid number!"); 
        break; 
     case 'b': sideB = in.nextDouble(); 
        if(sideB<0) 
        System.out.println("Error! Please enter a valid number!"); 
        break; 
     case 'c': if(sideA<0 || sideB<0) 
        System.out.println("Other two sides not yet given! please provide a and b first. "); 
        else 
        System.out.print("Side C(the hypotenuse) is: "+ Math.sqrt((sideA*sideA) + (sideB*sideB))); 

        break; 
     case 'q': break; 
     default : System.out.println(" Enter a valid choice! "); 
    } 
while(ch!='q'); 

} //Multiple markers at this line 
    Syntax Error, insert "}" to complete class body 
    Syntax Error, insert ";" to complete FieldDecleration 
    // 
+3

你必須把一個方法中的語句。 – azurefrog

+2

^那,並且你在while條件之前缺少了一個結束括號。 – CConard96

+0

僅供參考:修復語法錯誤使代碼編譯不被視爲「調試」,至少在我的書中沒有。雖然我認爲,在一個非常鬆散的解釋中,編譯錯誤是一個「錯誤」,但是直到代碼首先編譯才能使用「調試器」(幫助查找錯誤的工具)。 – Andreas

回答

0

前幾個項目將代碼放在一個功能之一。例如:

public class handleexceptions2 { 

    public static void main(String... args) { 

     ...your code here... 

    } 

} 
1
// The Scanner class is found in java.util, so it needs to be imported. 
import java.util.*; 

// As per convention, classes should always start with an uppercase letter, and you should 
// capitalize each word. 
public class HandleExceptions2 { 

    // The main method will get called when you run the class. 
    public static void main(String[] args) { 

    // These need to be doubles, and not integers since you are reading doubles from the input. 
    double sideA = -1; 
    double sideB = -1; 

    // You had "{" at the end of this line, which would allow you to create an anonymous class 
    // extending Scanner, which was certainly not your intention. 
    Scanner input = new Scanner(System.in); 

    // 'ch' needs to be defined outside the loop since you are using it in your 'do while' loop 
    // condition. 
    char ch; 
    do { 

     System.out.println("Enter your choice (a/b/c/q) : "); 

     // Your scanner is called 'input' not 'in'. Also, nextChar() is not a method in the Scanner 
     // class, so perhaps you want to read the entire line and grab the first character? 
     ch = input.nextLine().charAt(0); 
     switch (ch) { 
     case 'a': 
      sideA = input.nextDouble(); 
      // We want to finish reading this line (so that the next time we call nextLine() we don't 
      // just get an end-of-line character). 
      input.nextLine(); 
      if (sideA < 0) 
      System.out.println("Error! Please enter a valid number!"); 
      break; 
     case 'b': 
      sideB = input.nextDouble(); 
      input.nextLine(); 
      if (sideB < 0) 
      System.out.println("Error! Please enter a valid number!"); 
      break; 
     case 'c': 
      if (sideA < 0 || sideB < 0) 
      System.out.println("Other two sides not yet given! please provide a and b first. "); 
      else 
      // You probably want to finish writing the line here (System.out.print() doesn't add a 
      // line break). 
      System.out.println("Side C(the hypotenuse) is: " + Math.sqrt((sideA*sideA) + (sideB*sideB))); 
      break; 
     case 'q': break; 
     default : System.out.println(" Enter a valid choice! "); 
     } 

    // The while loop condition needs to be placed right after the matching bracket after 'do' 
    } while (ch != 'q'); 
    } 
} 
+0

@Jay:如果此答案解決了您的問題,請不要忘記將此答案標記爲已接受! –