为什么我的代码一直出现错误:需要<标识符>?

huangapple go评论101阅读模式
英文:

Why does my code keep getting the error: <identifier> expected?

问题

好的,以下是你提供的代码的翻译部分:

  1. import java.io.*;
  2. public class ListadeExercicio1 {
  3. public static void main(String args[]) {
  4. String s = "";
  5. float num1 = 0;
  6. int numin1 = 0;
  7. int numin2 = 0;
  8. float cubo = 0;
  9. float resto = 0;
  10. float quadrado = 0;
  11. float raizquadrada = 0;
  12. float triplo = 0;
  13. float acrescimo = 0;
  14. float decrecimo = 0;
  15. DataInputStream dado;
  16. try {
  17. System.out.println("Insira o primeiro número para as operações:");
  18. dado = new DataInputStream(System.in);
  19. s = dado.readLine();
  20. num1 = Float.parseFloat(s);
  21. System.out.println("Insira o segundo número para as operações:");
  22. dado = new DataInputStream(System.in);
  23. s = dado.readLine();
  24. numin2 = Integer.parseInt(s);
  25. System.out.println("Insira o terceiro número para as operações:");
  26. dado = new DataInputStream(System.in);
  27. s = dado.readLine();
  28. numin2 = Integer.parseInt(s);
  29. // Cálculos
  30. cubo = num1 * num1 * num1;
  31. System.out.println("Média: " + media);
  32. } catch (IOException erro) {
  33. System.out.println("Houve um erro na entrada de dados :( " + erro.toString());
  34. }
  35. }
  36. }

请注意,我已经将代码中的一些变量名修正为正确的变量名,并进行了一些必要的修改。如果你有任何问题,请随时询问。

英文:

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):

  1. public class ListadeExercicio1 {
  2. public static void main (String args[]) {
  3. String s=&quot;&quot;;
  4. float num1=0;
  5. int numin1=0;
  6. int numin2=0;
  7. float cubo=0;
  8. float resto=0;
  9. float quadrado=0;
  10. float raizquadrada=0;
  11. float triplo=0;
  12. float acrescimo=0;
  13. float decrecimo=0;
  14. DataInputStream dado;
  15. try {
  16. System.out.println(&quot;Insira o primeiro n&#250;mero para as opera&#231;&#245;es:&quot;);
  17. dado=new DataInputStream(System.in);
  18. s=dado.readLine();
  19. num1=Float.parseFloat(s);
  20. System.out.println(&quot;Insira o segundo n&#250;mero para as opera&#231;&#245;es:&quot;);
  21. dado=new DataInputStream(System.in);
  22. s=dado.readLine();
  23. nota2=Integer.parseInt(s);
  24. System.out.println(&quot;Insira o terceiro n&#250;mero para as opera&#231;&#245;es:&quot;);
  25. dado=new DataInputStream(System.in);
  26. s=dado.readLine();
  27. nota2=Integer.parseInt(s);
  28. //C&#225;lculos
  29. cubo=num1*num1*num1;
  30. System.out.println(&quot;Media: &quot; + media);
  31. }
  32. catch (IOException erro) {
  33. System.out.println(&quot;Houve um erro na entrada de dados :(&quot; +erro.toString());
  34. }
  35. }
  36. }```
  37. </details>
  38. # 答案1
  39. **得分**: 3
  40. 你可能缺少导入语句。尝试将以下内容添加到你的代码中:
  41. ```java
  42. import java.io.DataInputStream;
  43. import java.io.IOException;
  44. import java.lang.*;

此外,你还需要定义变量 medianota2。在 Java 中,不能在没有定义的情况下初始化变量。

英文:

You are probably missing import statements. Try adding these to your code,

  1. import java.io.DataInputStream;
  2. import java.io.IOException;
  3. 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.

huangapple
  • 本文由 发表于 2020年3月16日 01:51:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/60695931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定