题
我试图用一个文件,以创建一个菜单在该命令的窗口。用户选择的那些菜单选择。提示他们输入一个数字。数是通过两个超载方法,确定该数字是一个整数或浮动。在计算之后完成的结果是印刷的画面和菜单的重新出现。这里是代码,从我的两个文件。
MyMathOpsTest类:
import java.util.Scanner; // import Scanner class
public class MyMathOpsTest
{
//method to pause until a key is pressed
public static void pause()
{
try
{
System.out.print("Press <Enter> to continue...");
System.in.read();
}
catch (Exception e)
{
System.err.printf("Error %s%c\n",e.getMessage(),7);
}
}//end pause
public static void main( String args[] )
{
//variables to capture keyboard input
Scanner keyBd = new Scanner( System.in );
char selection;
//int selection;
do{//display menu
System.out.println( "1. Square a Number");
System.out.println( "2. Cube a Number");
System.out.println( "3. Raise a Number to a Power");
System.out.println( "4. Maximum of Three Numbers");
System.out.println( "5. Minimum of Three Numbers");
System.out.println( "6. Exit");
System.out.print( "Selection[1-6]: " );
//get menu selection
selection = keyBd.next().charAt(0);
//selection = keyBd.nextInt();
//process menu selection
switch (selection){
case '1':
MyMathOpsTest.squareTheNumber();
pause();
break;
case '2':
MyMathOpsTest.cubeTheNumber();
pause();
break;
case '3':
MyMathOpsTest.raiseTheNumber();
pause();
break;
case '4':
MyMathOpsTest.maximumNumber();
pause();
break;
case '5':
MyMathOpsTest.minimumNumber();
pause();
break;
case '6':
//recognize as valid selection but do nothing
break;
default :
System.out.printf("%c\n",7);
System.out.println("Invalid Selection");
}//end switch
}while( selection != '6');
} // end method main
public static void squareTheNumber()
{
}
public static void cubeTheNumber()
{
}
public static void raiseTheNumber()
{
}
public static void maximumNumber()
{
MyMathOps.maximum();
}
public static void minimumNumber()
{
}
} // end class MyMathOpsTest
MyMathOps类:
import java.util.Scanner;
public class MyMathOps
{
public static int square(x:Integer):Integer
{
}
public static double square(x:Double):Double
{
}
public static int cube(x:Integer):Integer
{
}
public static double cube(x:Double):Double
{
}
public static int maximum(x:Integer, y:Integer, z:Integer):Integer
{
// create Scanner for input from command window
Scanner input = new Scanner( System.in );
// obtain user input
System.out.print( "Enter three integer values separated by spaces: ");
int numberl = input.nextInt();
// read first integer
int number2 = input.nextInt();
// read second double
int number3 = input.nextInt();
// read third double
// determine the maximum value
int result = maximum( numberl, number2, number3 );
// display maximum value
System.out.println( "Maximum is: " + result );
} // end method maximum
public static double maximum(x:Double, y:Double, z:Double):Double
{
// create Scanner for input from command window
Scanner input = new Scanner( System.in );
// obtain user input
System.out.print( "Enter three floating-point values separated by spaces: ");
number1 = input.nextDouble();
// read first double double
number2 = input.nextDouble();
// read second double
double number3 = input.nextDouble();
// read third double
// determine the maximum value
double result = maximum( numberl, number2, number3 );
// display maximum value
System.out.println( "Maximum is: " + result );
} // end method maximum
public static int minimum(x:Integer, y:Integer, z:Integer):Integer
{
// create Scanner for input from command window
Scanner input = new Scanner( System.in );
// obtain user input
System.out.print( "Enter three integer values separated by spaces: ");
int numberl = input.nextInt();
// read first integer
int number2 = input.nextInt();
// read second double
int number3 = input.nextInt();
// read third double
// determine the minimum value
int result = minimum( numberl, number2, number3 );
// display minimum value
System.out.println( "Minimum is: " + result );
} // end method minimum
public static double minimum(x:Double, y:Double, z:Double):Double
{
// create Scanner for input from command window
Scanner input = new Scanner( System.in );
// obtain user input
System.out.print( "Enter three floating-point values separated by spaces: ");
number1 = input.nextDouble();
// read first double double
number2 = input.nextDouble();
// read second double
double number3 = input.nextDouble();
// read third double
// determine the minimum value
double result = minimum( numberl, number2, number3 );
// display minimum value
System.out.println( "Minimum is: " + result );
} // end method minimum
} // end class MyMathOps
这个代码组合的类型代码,我自己和例代码,从我的文书。这将不编译为我在jGRASP。我得到这些错误。
MyMathOps.java:10: <identifier> expected
public static int square(x:Integer):Integer
^
MyMathOps.java:96: ')' expected
} // end method minimum
^
2 errors
----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
我做错了这里?我已经花了几个小时在这方面的工作和阅读我的教科书。如果我没有得到这一权利。我会得到一个坏的等级。我需要一个良好的等级在此类的所以我可以进入一个顶尖的计算机科学大学。谢谢你的帮助。
在不太可能发生的事件,我的老师或者任何管理员从盐湖社区学院曾经遇到这个问题,让我提出我的意图清楚。这个问题发表的最伟大精神的学术诚实。我问这个问题征求一般性的建议和帮助了解适当的方式使用Java编程语言。我没有办法使用其他人的工作并代表它作为我自己的工作。我用提供的答案在这里一般援助在我的理解。我做所有我自己的工作,不复制工作提供的人回答我的问题。
解决方案
这样的线是无效的Java语法:
public static int square(x:Integer):Integer
public static int maximum(x:Integer, y:Integer, z:Integer):Integer
...
这看起来像一个或UML伪代码的语法。 “×:整数”为“语言无关的”符号,这意味着x是一个整数类型(它映射到在Java中的int或整型对象)。的。“:整数”末意味着该方法返回一个整数类型,你正在做正确已经
试着改变你的所有的方法声明看起来是这样的:
public static int square(int x) // Or "Integer x" if you want to use the Integer class, rather than the primitive int
public static int maximum(int x, int y, int z)
....
其他提示
我猜你用帕斯卡尔(或衍生物)。
公共static int方(x:整数):整数
在Java,
公共static int方(int x)
此外,由于代码是"MyMathOpsTest"你不需要为前缀的方法调用"MyMathOpsTest.".
还有,为什么叫它"MyMathOps"而不是"MathOperationsTest"?当然这是你的-它不会很长对我或任何人!挑选的名字具有意义,尽量避免简写方法,如"行动",除非它是常见的该领域的工作(网址是一个很好的,"行动"不是).
现在为何塞编程的建议对于初学者:
- 写一个单一行代码
- 获得这一行代码汇编
- 一旦代码行编纂工作上的下一个
- 获得下一线的代码汇编
- 继续做,直到程序完成。
没有犯同样的错误一遍又一遍-你所得到的好处是犯错误,并且不多的乐趣。
所以让你开始...
步骤1:
public class MathOperations
{
public static int maximum(final int x, final int y, final int z)
{
}
}
(汇编上述代码)
步骤2:
public class MathOperations
{
public static int maximum(final int x, final int y, final int z)
{
final Scanner input;
}
}
(汇编上述代码)
步骤3:
public class MathOperations
{
public static int maximum(final int x, final int y, final int z)
{
final Scanner input;
intput = new Scanner(System.in);
}
}
(汇编上述代码)
然后继续走一行的时间。一旦你得到它,你可以做更多比一线,但是在开始,这样做这一行的时间会告诉你马上时,你犯了一个错误。确保你解决你所有的错误之前移动到下一个线。
另外,在第一种方法中暂停()的端部,则需要另一个大括号:
public static void pause()
{
try
{
System.out.print("Press <Enter> to continue...");
System.in.read();
}
catch (Exception e)
{
System.err.printf("Error %s%c\n",e.getMessage(),7);
}
}<-- this one is missing in yours
希望这有助于!
我不知道这次演习的重点是 - 数学老年退休金计划,超载,或菜单。但是我建议你重新开始与这些为你的基础。至少它们编译和运行:
public class MyMathOps
{
public static int square(int x)
{
return x*x;
}
public static double square(double x)
{
return x*x;
}
public static int cube(int x)
{
return x*x*x;
}
public static double cube(double x)
{
return x*x*x;
}
public static int maximum(Integer... values)
{
Integer maxValue = Integer.MIN_VALUE;
for (Integer value : values)
{
if (value.compareTo(maxValue) > 0)
{
maxValue = value;
}
}
return maxValue;
}
public static double maximum(Double... values)
{
Double maxValue = Double.MIN_VALUE;
for (Double value : values)
{
if (value.compareTo(maxValue) > 0)
{
maxValue = value;
}
}
return maxValue;
}
public static int minimum(Integer... values)
{
Integer minValue = Integer.MAX_VALUE;
for (Integer value : values)
{
if (value.compareTo(minValue) < 0)
{
minValue = value;
}
}
return minValue;
}
public static double minimum(Double... values)
{
Double minValue = Double.MIN_VALUE;
for (Double value : values)
{
if (value.compareTo(minValue) < 0)
{
minValue = value;
}
}
return minValue;
}
}
和测试类(简化的):
public class MyMathOpsTest
{
public static void main(String args[])
{
Integer [] intValues = { 1, 2, 3, };
Double [] doubleValues = { 11.0, 14.0, -6.0 };
for (Integer value : intValues)
{
System.out.println("value : " + value);
System.out.println("squared: " + MyMathOps.square(value));
System.out.println("cubed : " + MyMathOps.cube(value));
System.out.println("min : " + MyMathOps.minimum(intValues));
System.out.println("max : " + MyMathOps.maximum(intValues));
}
for (Double value : doubleValues)
{
System.out.println("value : " + value);
System.out.println("squared: " + MyMathOps.square(value));
System.out.println("cubed : " + MyMathOps.cube(value));
System.out.println("min : " + MyMathOps.minimum(doubleValues));
System.out.println("max : " + MyMathOps.maximum(doubleValues));
}
}
}
在此完成运行时,你就会知道,你的方法是正确的。饶自己的第一次尝试读取值的困难。