Java调试,语法问题

Java调试,语法问题

问题描述:

我有几个调试问题,我无法弄清楚。我在下面的代码中评论了他们,如果有人能纠正它并解释错误发生的原因,我会非常感激。我是新来的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

前几个项目将代码放在一个功能之一。例如:

public class handleexceptions2 { 

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

     ...your code here... 

    } 

} 

// 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:如果此答案解决了您的问题,请不要忘记将此答案标记为已接受! –