英文:
Why does my code keep getting the error: <identifier> expected?
问题
好的,以下是你提供的代码的翻译部分:
import java.io.*;
public class ListadeExercicio1 {
public static void main(String args[]) {
String s = "";
float num1 = 0;
int numin1 = 0;
int numin2 = 0;
float cubo = 0;
float resto = 0;
float quadrado = 0;
float raizquadrada = 0;
float triplo = 0;
float acrescimo = 0;
float decrecimo = 0;
DataInputStream dado;
try {
System.out.println("Insira o primeiro número para as operações:");
dado = new DataInputStream(System.in);
s = dado.readLine();
num1 = Float.parseFloat(s);
System.out.println("Insira o segundo número para as operações:");
dado = new DataInputStream(System.in);
s = dado.readLine();
numin2 = Integer.parseInt(s);
System.out.println("Insira o terceiro número para as operações:");
dado = new DataInputStream(System.in);
s = dado.readLine();
numin2 = Integer.parseInt(s);
// Cálculos
cubo = num1 * num1 * num1;
System.out.println("Média: " + media);
} catch (IOException erro) {
System.out.println("Houve um erro na entrada de dados :( " + erro.toString());
}
}
}
请注意,我已经将代码中的一些变量名修正为正确的变量名,并进行了一些必要的修改。如果你有任何问题,请随时询问。
英文:
Okay so, I'm trying to do my programming homework and it's KILLING ME, because the online compilers I use keep giving me the same indentifier expected error, but I tried everything to fix it, and it never does!
Please, please explain me what's wrong!
It gives me this exact error:
/ListadeExercicio1.java:1: error: <identifier> expected
import:java.io.*;
Here's my code so far (it's in pt-br, please don't mind):
public class ListadeExercicio1 {
public static void main (String args[]) {
String s="";
float num1=0;
int numin1=0;
int numin2=0;
float cubo=0;
float resto=0;
float quadrado=0;
float raizquadrada=0;
float triplo=0;
float acrescimo=0;
float decrecimo=0;
DataInputStream dado;
try {
System.out.println("Insira o primeiro número para as operações:");
dado=new DataInputStream(System.in);
s=dado.readLine();
num1=Float.parseFloat(s);
System.out.println("Insira o segundo número para as operações:");
dado=new DataInputStream(System.in);
s=dado.readLine();
nota2=Integer.parseInt(s);
System.out.println("Insira o terceiro número para as operações:");
dado=new DataInputStream(System.in);
s=dado.readLine();
nota2=Integer.parseInt(s);
//Cálculos
cubo=num1*num1*num1;
System.out.println("Media: " + media);
}
catch (IOException erro) {
System.out.println("Houve um erro na entrada de dados :(" +erro.toString());
}
}
}```
</details>
# 答案1
**得分**: 3
你可能缺少导入语句。尝试将以下内容添加到你的代码中:
```java
import java.io.DataInputStream;
import java.io.IOException;
import java.lang.*;
此外,你还需要定义变量 media
和 nota2
。在 Java 中,不能在没有定义的情况下初始化变量。
英文:
You are probably missing import statements. Try adding these to your code,
import java.io.DataInputStream;
import java.io.IOException;
import java.lang.*;
In addition to that, you also need to define variables media
and nota2
. You cannot initialize variable without defining it in Java.
答案2
得分: 1
问题在于你应该导入 java.io.*;
,因为你正在使用该包中的类,但你没有导入。
你应该阅读有关Java 包的内容。
另外,我注意到你在问题中同时标记了 Java 和 JavaScript。请注意,尽管名称相似,但实际上这些是完全不相关的编程语言。
英文:
The problem is that you should've imported java.io.*;
because you're using classes from that package and you didn't.
You should read about Java packages.
Also, I noticed that you tagged your question with both Java and JavaScript. Please note that, in spite of the names being similar, these are actually completely unrelated languages.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论