Java调试、语法问题
我有几个调试问题,我无法解决。我在下面的代码中对它们进行了注释,如果有人能纠正它并解释错误发生的原因,我将不胜感激。我是java新手,这是我正在从事的少数几个项目之一Java调试、语法问题,java,debugging,syntax,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 { Sys
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
//
公共类handleexceptions2{
int sideA=-1;
int sideB=-1;
扫描仪输入=新扫描仪(System.in){
do//标记“do”上出现语法错误,请删除此标记
{
System.out.println(“输入您的选择(a/b/c/q):”;
char ch=in.nextChar();
开关(ch)
{
案例“a”:sideA=in.nextDouble();
如果(sideA将代码放入函数中,例如:
public class handleexceptions2 {
public static void main(String... args) {
...your code here...
}
}
//Scanner类位于java.util中,因此需要导入它。
导入java.util.*;
//按照惯例,类应该总是以大写字母开头,并且
//每个单词大写。
公共类句柄异常2{
//运行该类时将调用main方法。
公共静态void main(字符串[]args){
//这些必须是双精度的,而不是整数,因为您正在从输入中读取双精度。
双侧A=-1;
双边b=-1;
//在这一行的末尾有“{”,这将允许您创建一个匿名类
//扩展扫描仪,这当然不是您的意图。
扫描仪输入=新扫描仪(System.in);
//“ch”需要在循环外部定义,因为您在“do while”循环中使用它
//条件。
char ch;
做{
System.out.println(“输入您的选择(a/b/c/q):”;
//您的扫描仪名为“input”,而不是“in”。此外,nextChar()不是扫描仪中的方法
//类,那么您可能想阅读整行内容并获取第一个字符?
ch=input.nextLine().charAt(0);
开关(ch){
案例“a”:
sideA=input.nextDouble();
//我们想读完这一行(这样下次调用nextLine()时就不会
//只需获得一个行尾字符)。
input.nextLine();
if(sideA<0)
System.out.println(“错误!请输入有效数字!”);
打破
案例“b”:
sideB=input.nextDouble();
input.nextLine();
如果(b侧<0)
System.out.println(“错误!请输入有效数字!”);
打破
案例“c”:
if(sideA<0 | | sideB<0)
System.out.println(“其他两面尚未给出!请先提供a和b”);
其他的
//您可能希望在此处完成这一行的编写(System.out.print()不添加
//换行)。
System.out.println(“C面(斜边)是:”+Math.sqrt((sideA*sideA)+(sideB*sideB));
打破
案例“q”:中断;
默认值:System.out.println(“输入有效选项!”);
}
//while循环条件需要放在“do”后面的匹配括号后面
}while(ch!=“q”);
}
}
您必须将语句放在方法中。^这样,您在while条件之前缺少了一个结束括号。仅供参考:修复语法错误使代码编译不被视为“调试”,至少在我的书中不被视为“调试”。尽管我猜在一个非常松散的解释中,编译错误是一个“bug”,而是一个“调试器”(帮助查找bug的工具)在代码首先编译之前都不能使用。@杰:如果这个答案解决了您的问题,别忘了将这个答案标记为已接受!
// 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');
}
}