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

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

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=&quot;&quot;;
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(&quot;Insira o primeiro n&#250;mero para as opera&#231;&#245;es:&quot;);
dado=new DataInputStream(System.in); 
s=dado.readLine(); 
num1=Float.parseFloat(s); 
System.out.println(&quot;Insira o segundo n&#250;mero para as opera&#231;&#245;es:&quot;);
dado=new DataInputStream(System.in);  
s=dado.readLine(); 
nota2=Integer.parseInt(s);
System.out.println(&quot;Insira o terceiro n&#250;mero para as opera&#231;&#245;es:&quot;);
dado=new DataInputStream(System.in);  
s=dado.readLine(); 
nota2=Integer.parseInt(s);
//C&#225;lculos
cubo=num1*num1*num1;
System.out.println(&quot;Media: &quot; + media);
} 
catch (IOException erro) {
System.out.println(&quot;Houve um erro na entrada de dados :(&quot; +erro.toString());
} 
}
}```
</details>
# 答案1
**得分**: 3
你可能缺少导入语句。尝试将以下内容添加到你的代码中:
```java
import java.io.DataInputStream;
import java.io.IOException;
import java.lang.*;

此外,你还需要定义变量 medianota2。在 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.

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:

确定