请问这个对吗?java

来源:百度知道 编辑:UC知道 时间:2024/06/03 07:05:39
老师要求:write a program that:
1.asks the user for a length,width,and height of a box
2.print the volume of the box
3.ask the user how many boxes they want
4 print the total volume of all boxes.
我写的:
import java.util.Scanner;
public class box{
public static void main(String[]args){
int length,width,height,boxes;
double volume;
Scanner input=newScanner(System.in);

System.out.println("Enter the length:");
length=input.nextInt;
System.out.println("Enter the width:");
width=input.nextInt;
System.out.println("Enter the height:");
height=input.nextInt;

volume=length*width*height;
System.out.println(volume);

System.out.println("Enter the number of boxes");
boxes=input.nextInt;
total volume=boxes*volume;

System.out.println(total volume);

}//end of main
}//end of class

这种写法编译是不会通过的,原因有:
1,public class box{类名必须大写.
2.String[]args 改为String[] args
3.Scanner input=newScanner(System.in); 改为Scanner input=new Scanner(System.in);
此外要注意,存成JAVA文件的时候,编码方式按咱们这边的要求选成ANSI,
最后,然后就可以编译通过了,修改之后的:

import java.util.Scanner;
public class Box{
public static void main(String[] args){
int length,width,height,boxes;
double volume;
double total_volume;
//长
Scanner input=new Scanner(System.in);
System.out.println("Enter the length:");
//宽
length=input.nextInt();
System.out.println("Enter the width:");
//高
width=input.nextInt();
System.out.println("Enter the height:");
height=input.nextInt();
volume=length*width*height;
System.out.println(volume);
System.out.println("Enter the number of boxes");
boxes=input.nextInt();
total_volume=boxes*volume;
System.out.println(total_volu