Java 字符串文本中标记的语法错误
在System.out.println的第一个实例之后,我几乎所有的System.out.println文本都出现了“令牌语法错误,请删除这些令牌”。我不知道这意味着什么,也不知道如何修复它?我是一个非常新的开始,所以这段代码中可能有多个错误。我还得到了“令牌上的语法错误”“Double is”“、无效的AssignmentOperator”“和”“squared is”“、无效的AssignmentOperator”错误。这是一个类的作业,其最终结果应该是 n的对立面是y n是y n的一半是y n的平方是y n的倒数是y n的十分之一是y,y的平方是z n减去n的最后一位是y n和n+1以及n+2之和是y 谢谢大家!Java 字符串文本中标记的语法错误,java,syntax-error,Java,Syntax Error,在System.out.println的第一个实例之后,我几乎所有的System.out.println文本都出现了“令牌语法错误,请删除这些令牌”。我不知道这意味着什么,也不知道如何修复它?我是一个非常新的开始,所以这段代码中可能有多个错误。我还得到了“令牌上的语法错误”“Double is”“、无效的AssignmentOperator”“和”“squared is”“、无效的AssignmentOperator”错误。这是一个类的作业,其最终结果应该是 n的对立面是y n是y n的一半是y
import java.util.Scanner;
public class Arithmetic {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = scanner.nextInt();
int opposite = n*-1;
System.out.println("The opposite of" n "is" opposite);
int twoTimes = n*2;
System.out.println(n "doubled is" twoTimes);
int half = n/2;
System.out.println("half of "n "is" half);
int square= n*n;
System.out.println(n "squared is" square);
int reciprocal= 1/n;
System.out.println("the reciprocal of" n "is" reciprocal);
double fraction = n*.10;
double fractionTwo = fraction*fraction;
System.out.println("one-tenth of" n "is" fraction "and" fraction "squared is" fractionTwo);
// int lastDigit =
// System.out.println();
int sum= n+1;
int sumTwo= n+2;
int sumTotal= sum + sumTwo;
System.out.println("the sum of" n "and" sum "and" sumTwo "is" sumTotal);
}
}
**另外,如果有人愿意帮助我找出“n+1”/“n+2”公式以及如何在代码中格式化,我将不胜感激 此代码有一些错误
System.out.println("The opposite of" n "is" opposite);
应该是:
System.out.println("The opposite of" + n + "is" + opposite);
当我们想要组合字符串时,我们使用+
符号
int倒数=1/n代码>将不起作用;
它应该是双倒数=1.0/n
假设n
是int
“n+1”/“n+2”只是:双结果=(n+1.0)/(n+2.0)
假设n
是int
这个代码有一些错误
您没有在任何打印到控制台上正确连接
System.out.println("The opposite of" n "is" opposite);
应该是:
System.out.println("The opposite of" + n + "is" + opposite);
当我们想要组合字符串时,我们使用+
符号
int倒数=1/n代码>将不起作用;
它应该是双倒数=1.0/n
假设n
是int
“n+1”/“n+2”只是:双结果=(n+1.0)/(n+2.0)
假设n
是int
这不是连接(链接)两个字符串的方式
该代码和其他类似代码
System.out.println(n "doubled is" twoTimes);
你错了
我想你想把n
,“double is”
和两次链接在一起,对吗
现在,您正在使用空格链接它们。但是Java中的空格字符不连接字符串。这就是编译器抱怨的原因
在Java中,+
用于字符串的添加和连接!因此,您应将上述内容更改为:
System.out.println(n + "doubled is" + twoTimes);
但是等等!你的空间到哪里去了?这是因为+
不会自动为您添加空间,您需要自己添加空间
System.out.println(n + " doubled is " + twoTimes);
或者,您可以使用String.format
格式化字符串。这个
/* Explanation: n will be "inserted" to the first %d and twoTimes will
be inserted to the second %d. And %d basically means "express the thing in
decimal"*/
String.format("%d doubled is %d", n, twoTimes)
与
n + " doubled is " + twoTimes
关于你的公式问题:
在Java中,有两种不同的数字类型,int
和double
。(实际上还有很多,但它们是不相关的)int
和double
在分开时做不同的事情。它们有不同的文字
5
是int
文本,5.0
是double
文本。看见没有小数点的数字称为int
s,有小数点的数字称为double
s
那么你的公式有什么问题?让我们首先看看除以int
和double
int / int: 1 / 5 = 0
int / double: 1 / 5.0 = 0.2
double / int: 1.0 / 5 = 0.2
double / double: 1.0 / 5.0 = 0.2
int / 0: 1 / 0 = Exception!
double / 0: 1.0 / 0 = NaN
在代码中:
int reciprocal= 1/n;
和其他类似的行,您正在进行int
的除法。这就是上面的代码不起作用的原因。你应该做的是把其中一个数字改成双精度!并将类型更改为double
double reciprocal = 1.0 / n;
------ ---
changes here as well!
这不是连接(链接)两个字符串的方式
该代码和其他类似代码
System.out.println(n "doubled is" twoTimes);
你错了
我想你想把n
,“double is”
和两次链接在一起,对吗
现在,您正在使用空格链接它们。但是Java中的空格字符不连接字符串。这就是编译器抱怨的原因
在Java中,+
用于字符串的添加和连接!因此,您应将上述内容更改为:
System.out.println(n + "doubled is" + twoTimes);
但是等等!你的空间到哪里去了?这是因为+
不会自动为您添加空间,您需要自己添加空间
System.out.println(n + " doubled is " + twoTimes);
或者,您可以使用String.format
格式化字符串。这个
/* Explanation: n will be "inserted" to the first %d and twoTimes will
be inserted to the second %d. And %d basically means "express the thing in
decimal"*/
String.format("%d doubled is %d", n, twoTimes)
与
n + " doubled is " + twoTimes
关于你的公式问题:
在Java中,有两种不同的数字类型,int
和double
。(实际上还有很多,但它们是不相关的)int
和double
在分开时做不同的事情。它们有不同的文字
5
是int
文本,5.0
是double
文本。看见没有小数点的数字称为int
s,有小数点的数字称为double
s
那么你的公式有什么问题?让我们首先看看除以int
和double
int / int: 1 / 5 = 0
int / double: 1 / 5.0 = 0.2
double / int: 1.0 / 5 = 0.2
double / double: 1.0 / 5.0 = 0.2
int / 0: 1 / 0 = Exception!
double / 0: 1.0 / 0 = NaN
在代码中:
int reciprocal= 1/n;
和其他类似的行,您正在进行int
的除法。这就是上面的代码不起作用的原因。你应该做的是把其中一个数字改成双精度!并将类型更改为double
double reciprocal = 1.0 / n;
------ ---
changes here as well!
您没有正确连接System.out.println(“n”的对立面是“对立面”)
应该是System.out.println(“与“+n+”相反的是“+contract”)代码>如果出现语法错误,为什么要告诉我们作业的目的?问你一个关于特定问题的问题。另外,标签eclipse
和token
不应该在这里。我使用了eclipse,这就是为什么我标记了它。我问了一个关于具体问题的问题。我告诉过你关于提供上下文的任务。你认为遵循eclipse
标签的人会乐意提供上下文吗