// Patrik Maco
// 18/10/21
// VERSION 1
// Write a program that allows the user to input the length, width and height of a room (cm) - volme of balloon (m^3) prints the volume of the room in m^3 and the amount of balloons needed to fill the room.
import java.util.Scanner;
class volume
{
public static void main (String [] a)
{
Run();
System.exit(0);
}
public static int Length()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Values as whole numbers- not as decimals. Do not include measurement. Good example : 5,7,9 Bad example: 3.2, 5cm");
System.out.println("Length of room (in cm) ? ");
length = scanner.nextInt();
return length;
}
public static int Width()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Values as whole numbers- not as decimals. Do not include measurement. Good example : 5,7,9 Bad example: 3.2, 5cm");
System.out.println("Width of room (in cm) ? ");
int width = scanner.nextInt();
return width;
}
public static int Height(length, width)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Values as whole numbers- not as decimals. Do not include measurement. Good example : 5,7,9 Bad example: 3.2, 5cm");
System.out.println("Height of room (in cm) ? ");
int height = scanner.nextInt();
return height;
System.out.println("Enter the volume of the baloon in M cubed");
float volume_b;
volume_b = scanner.nextFloat();
float RoomVolume;
RoomVolume = ( height * length * width);
System.out.println("The volume of the room is : " + RoomVolume);
float balloon3;
balloon3 = RoomVolume/1000000;
float BalloonAmount;
BalloonAmount = balloon3/volume_b;
int n;
n = (int)BalloonAmount;
System.out.println("The number of balloons needed is : " + n);
}
public static void Run()
{
int length = Length();
int width = Width();
int height = Height();
}
}
方法Run()返回一个错误
方法Height(int, int)不适用于arguments()。
我是很新的编码,我想一个解释,如果可能的话,我怎么能解决这个问题?
Height在它自己的方法中包含两个参数,但是如果我在Run()中输入相同的参数,我的程序不会运行
您的方法Height
存在多个问题。
- 在Java中,所有参数都需要指定它们的类型,所以应该声明为
public static int Height(int length, int width)
- 无条件返回语句后的所有代码都不可达
return height;
// any code after this point is unreachable
- 如果一个方法有参数,你需要显式地提供它们:
public static void Run()
{
int length = Length();
int width = Width();
int height = Height(length, width); // you need to pass length and width!
}
其他观察结果:
- 不需要在
main
的末尾显式地调用System.exit(0)
,程序将在此时自动结束。 - 在Java中,将字段和类视为名词,将方法视为动词并相应地命名是一种非常普遍的命名约定。此外,类名通常以大写字母
SomeClassName
开头,而方法名通常以小写字母doSomething()
开头。