我是编程的新手,我做了一个简单的计算器,无法弄清楚它为什么不能编译。所有帮助赞赏。
import java.util.Scanner;
public class Cal
{
Scanner sc = new Scanner(System.in);
第一个号码
public static double getInput1()
{
while(true)
{
try
{
double num1 = sc.nextDouble();
return num1;
}
catch (Exception e)
{
sc.next();
System.out.print("Error, please try again:");
}
}
}
操作
public static int getInput2()
{
while(true)
{
try
{
int operation = sc.nextInt();
return operation;
}
catch (Exception e)
{
sc.next();
System.out.print("Error, please try again:");
}
}
}
第二个号码
public static double getInput3()
{
while(true)
{
try
{
double num2 = sc.nextDouble();
return num2;
}
catch (Exception e)
{
sc.next();
System.out.print("Error, please try again:");
}
}
}
如果计算器返回true,则该方法应该重复计算器
public static boolean continue()
{
while(true)
{
try
{
boolean answer = sc.nextBoolean();
return answer;
}
catch (Exception e)
{
sc.next();
System.out.print("Error, please try again:");
}
}
}
主要方法
public static void main(String[] args)
{
System.out.println("Welcome to Simple Calculator Version 1.1");
do
{
System.out.print("Enter the first number :");
double num1 = getInput1();
System.out.print("Enter 1 for +, 2 for -, 3 for *, 4 for /, :");
int operation = getInput2();
System.out.print("Enter the second number :");
double num2 = getInput3();
switch (operation)
{
case 1:
System.out.println("The answer is " + (num1 + num2));
break;
case 2:
System.out.println("The answer is " + (num1 - num2));
break;
case 3:
System.out.println("The answer is " + (num1 * num2));
break;
case 4:
System.out.println("The answer is " + (num1 / num2));
break;
}
System.out.print("Do you want to continue true or false :");
boolean reply = continue();
}
while(reply);
System.out.println("Thankyou for using Simple Calculator 1.1, have a nice day. ");
}
}
以下是错误消息
Cal.java:54: error: <identifier> expected
public static boolean continue()
^
Cal.java:54: error: illegal start of type
public static boolean continue()
^
Cal.java:54: error: <identifier> expected
public static boolean continue()
^
Cal.java:54: error: ';' expected
public static boolean continue()
^
Cal.java:56: error: illegal start of type
while(true)
^
Cal.java:56: error: <identifier> expected
while(true)
^
Cal.java:56: error: ';' expected
while(true)
^
Cal.java:56: error: illegal start of type
while(true)
^
Cal.java:56: error: <identifier> expected
while(true)
^
Cal.java:57: error: ';' expected
{
^
Cal.java:63: error: illegal start of type
catch (Exception e)
^
Cal.java:69: error: class, interface, or enum expected
}
^
Cal.java:70: error: class, interface, or enum expected
public static void main(String[] args)
^
Cal.java:73: error: class, interface, or enum expected
do
^
Cal.java:76: error: class, interface, or enum expected
double num1 = getInput1();
^
Cal.java:77: error: class, interface, or enum expected
System.out.print("Enter 1 for +, 2 for -, 3 for *, 4 for /, :");
^
Cal.java:78: error: class, interface, or enum expected
int operation = getInput2();
^
Cal.java:79: error: class, interface, or enum expected
System.out.print("Enter the second number :");
^
Cal.java:80: error: class, interface, or enum expected
double num2 = getInput3();
^
Cal.java:81: error: class, interface, or enum expected
switch (operation)
^
Cal.java:85: error: class, interface, or enum expected
break;
^
Cal.java:86: error: class, interface, or enum expected
case 2:
^
Cal.java:88: error: class, interface, or enum expected
break;
^
Cal.java:89: error: class, interface, or enum expected
case 3:
^
Cal.java:91: error: class, interface, or enum expected
break;
^
Cal.java:92: error: class, interface, or enum expected
case 4:
^
Cal.java:94: error: class, interface, or enum expected
break;
^
Cal.java:95: error: class, interface, or enum expected
}
^
Cal.java:97: error: class, interface, or enum expected boolean reply = continue();
^
Cal.java:98: error: class, interface, or enum expected
}
^
Cal.java:100: error: class, interface, or enum expected
System.out.println("Thankyou for using Simple Calculator 1.1, have a nice day. ");
^
Cal.java:101: error: class, interface, or enum expected
}
^
32 errors