import java.util.*;
public class Lab04B {
public static String toMeters (int unitNumber) {
String value;
switch (unitNumber) {
case 1:
value = "Meter";
break;
case 2:
value = "Nautical mile";
break;
case 3:
value = "Furlong";
break;
case 4:
value = "Mil";
break;
case 5:
value = "Rod";
break;
case 6:
value = "Vershok";
break;
case 7:
value = "Sheppey";
break;
case 8:
return 1.702;
default:
return -1;
}
{
public static double fromMeters (int unitNumber)
{
switch (unitNumber)
{
case 1:
return 1;
case 2:
return 1/1852.0;
case 3:
return 1/201.168;
case 4:
return 1/0.0254;
case 5:
return 1/5.029;
case 6:
return 1/0.04445;
case 7:
return 1/1408.0;
case 8:
return 1/1.702;
default:
return -1;
}
{
public static String getUnitName (int unitNumber)
{
String value;
switch (unitNumber)
{
case 1:
value = "Meter";
case 2:
value = "Nautical mile";
case 3:
value = "Furlong";
case 4:
value = "Mil";
case 5:
value = "Rod";
case 6:
value = "Vershok";
case 7:
value = "Sheppey";
case 8:
value = "Smoot";
default:
value = "faulty input";
}
{
public static void main (String[] args)
Scanner input = new Scanner (System.in);
System.out.println("Converting Measurements");
System.out.println("By: Ashleigh Pacewicz");
System.out.println("1.\tMeter");
System.out.println("2.\tNautical Mile");
System.out.println("3.\tFurlong");
System.out.println("4.\tMil");
System.out.println("5.\tRod");
System.out.println("6.\tVershok");
System.out.println("7.\tSheppey");
System.out.println("8.\tSmoot");
System.out.println("From what unit would you like to convert? ");
int = input.nextInt();
System.out.println("To what unit would you like to convert? ");
int = input.nextInt();
System.out.print("What measurement would you like to convert? ");
double = input.nextDouble();
}
}
}
我正在学习如何编码。我正在尝试编写程序来转换仪表,但我在第40行和第63行以及第96行仍然收到错误。错误:
'的.class'预期
我做错了什么?
答案 0 :(得分:1)
首先
int = input.nextInt();
System.out.println("To what unit would you like to convert? ");
int = input.nextInt();
你没有给他们起个名字
看看你的大括号
}
{
public static String getUnitName (int unitNumber)
{
每种方法都是一样的
应该是这样的
public void methodName() {
}
但你正在做这个
{
public void methodName()
{
你忘了休息;
你应该真正使用IDE
答案 1 :(得分:1)
我想说实话。我不知道你为什么会收到这个错误...
我刚刚复制了你的代码并进行了编译。删除2到3个大括号并添加一个后,您的代码编译时没有错误。我确定你从其他地方得到了错误,你粘贴到了你的问题中。
您必须应用一些修正:
System.out.println("To what unit would you like to convert? ");
int NAME_YOUR_VARS = input.nextInt();
在一些地方,你会在方法标题的前面放置开括号:
{
public static double fromMeters(int unitNumber){
或者你忘了关闭方法身体:
public static String toMeters (int unitNumber) {
switch(unitNumber) {
/* case statements were cut out here*/
}
//<- Here you forgot a brace!
跟踪代码块和支撑位置非常重要!