Java调试,语法问题

时间:2016-07-06 21:58:52

标签: java debugging syntax

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

2 个答案:

答案 0 :(得分: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');
  }
}

答案 1 :(得分:0)

将代码放在函数中:

public class handleexceptions2 {

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

         ...your code here...

    }

}