Java I';我已经检查了这个代码好几个小时了,但是我';I’我不确定这有什么问题 公开课问题五 { //错误 公共静态void main(字符串[]args) { double fallingDistance;//方法的reutrn值 最终双g=9.8;//重力恒定值 int s;//秒 double value;//存储我们的方法值 值=下降距离(); 系统输出打印项次(值); 公共静态双下降距离(int s) { 对于(s=1;s
您需要将Java I';我已经检查了这个代码好几个小时了,但是我';I’我不确定这有什么问题 公开课问题五 { //错误 公共静态void main(字符串[]args) { double fallingDistance;//方法的reutrn值 最终双g=9.8;//重力恒定值 int s;//秒 double value;//存储我们的方法值 值=下降距离(); 系统输出打印项次(值); 公共静态双下降距离(int s) { 对于(s=1;s,java,Java,您需要将fallingDistance方法移出main方法的主体。Java不支持直接在其他方法中定义方法 public class QuestionFive { // errors public static void main (String [] args) { double fallingDistance; // reutrn value of the method final double g = 9.8; // constant value of
fallingDistance
方法移出main
方法的主体。Java不支持直接在其他方法中定义方法
public class QuestionFive
{
// errors
public static void main (String [] args)
{
double fallingDistance; // reutrn value of the method
final double g = 9.8; // constant value of gravity
int s; // seconds
double value; // stores our methods value
value = fallingDistance();
system.out.println(value);
public static double fallingDistance(int s)
{
for (s = 1; s <= 10; s++)
d = 0.5 * 9.8 * (s * s);
return d;
}
}
}
如果您学会正确缩进代码,您自己“调试”这些问题会容易得多。您需要在fallingDistance函数调用中传递一个值
public class QuestionFive {
public static void main (String [] args) {
// ...
} // Missing this brace.
public static double fallingDistance(int s)
// ...
}
// } // Remove this extraneous brace.
}
应使用大写字母s,如上所述,您不能在主方法内创建方法,因此必须将方法移到主方法外。这将清除非法的起始表达式 但是,我不认为您可以让一个方法显式返回多个值,就像您在上面代码中尝试做的那样。您可以做的是创建一个数组,该数组将存储每个值,然后您可以通过调用主方法中的数组索引来调用这些值。或者,现在您可以调用该方法来打印所有值价值观。希望这有帮助
System.out.println("");
公共类测试器
{
//这些是类变量,因此您可以调用它们
//从该类中的任何静态方法。
公共静态最终双重力=9.8;//米/秒
public static int seconds=0;//初始时间
public static double[]physics=new double[10];//声明并初始化数组以保存值
公共静态void main(字符串[]args)
{
doPhysics();//方法的调用
//i表示0-9之间的索引值(基于它是一个十索引数组)
对于(inti=0;这不是一个好的标题。标题应该具体描述问题,以便有相关技能的人能够轻松帮助您
System.out.println("");
public class Tester
{
//these are the class variables so that you can call them
//from any of the static methods in this class.
public static final double gravity = 9.8; // meters/second
public static int seconds = 0; //initial time
public static double[] physics = new double[10]; // declares and initializes the array to hold your values
public static void main(String[]args)
{
doPhysics(); // the call for your method
//i represents the index value from 0-9 (based on it being a ten index array)
for (int i = 0; i<10; i++){
System.out.println(Tester.physics[i]);
}
}
public static void doPhysics()
{
for (int second = Tester.seconds; second < Tester.physics.length; second++){ // Tester. is needed because the variables are static and require the class reference
Tester.physics[second] = 0.5 * Tester.gravity * (second * second); // second doubles as the array index and the current second
System.out.println("second: "+ second + " "+ Tester.physics[second]);
}
}
}