我是编码的新手,并且在令牌“。”上一直遇到语法错误。 在第21、22和23行(System.out.println)。我能弄清楚我们是错误的吗?
// calculator for the price of milk
import java.util.Scanner;
public class DairyCalculator
{
public static void main( String [] args)
{
double cartonhold = 3.78;
double productioncost = 0.38;
double cartonprofit = 0.27;
Scanner sc = new Scanner(System.in);
System.out.print( "Enter how many liters of milk was produced; "); // input
double produced = sc.nextInt();
double cartonsneeded = produced / cartonhold;
int System.out.println( "Milk cartons needed" = cartonsneeded);
double System.out.println("Cost of production" = produced * productioncost);
double System.out.println("Profit" = produced * cartonprofit);
sc.close();
}
}
答案 0 :(得分:1)
将代码更新为
// calculator for the price of milk
import java.util.Scanner;
public class DairyCalculator {
public static void main(String[] args) {
double cartonhold = 3.78;
double productioncost = 0.38;
double cartonprofit = 0.27;
Scanner sc = new Scanner(System.in);
System.out.print("Enter how many liters of milk was produced; "); // input
double produced = sc.nextInt();
double cartonsneeded = produced / cartonhold;
System.out.println("Milk cartons needed = " + cartonsneeded);
System.out.println("Cost of production = " + produced * productioncost);
System.out.println("Profit = " + produced * cartonprofit);
sc.close();
}
}
在System.out ..之前不需要int和double,并且还可以阅读有关Java语法的更多信息。 您可以使用'+'运算符来连接字符串
答案 1 :(得分:0)
每个System.out.println()
语句中都有一个错误。该语句用于在控制台上打印输出。因此,您不能将其声明为int
,double
或任何其他数据类型。因此,每个打印语句中都有一个简单的语法错误。 =
也是一个赋值运算符。如果要将其打印为控制台输出,则必须将其括在反逗号中。并使用+
运算符来连接两个字符串。因此正确的代码如下:
// calculator for the price of milk
import java.util.Scanner;
public class DairyCalculator {
public static void main( String [] args) {
double cartonhold = 3.78;
double productioncost = 0.38;
double cartonprofit = 0.27;
Scanner sc = new Scanner(System.in);
System.out.print( "Enter how many liters of milk was produced; "); // input
double produced = sc.nextInt();
double cartonsneeded = produced / cartonhold;
System.out.println("Milk cartons needed = " + cartonsneeded);
System.out.println("Cost of production = " + produced * productioncost);
System.out.println("Profit = " + produced * cartonprofit);
sc.close();
}
}
答案 2 :(得分:0)
请勿在{{1}}之前使用数据类型,因为它仅用于打印目的,而数据类型用于变量声明。
在“”引号中,您可以写下要打印的任何消息,即,即您要提供的硬编码消息。
System.out.println
运算符“”之后用于将所需对象的值连接到硬编码消息中。
为此,您使用+
运算符并在其后写对象名称,该名称将打印硬编码消息和要打印的对象的值。