【问题标题】:Cannot find symbol if statement error如果语句错误,找不到符号
【发布时间】:2013-10-24 18:06:31
【问题描述】:

我一直在编写一个有趣的小程序,但我一直收到这个错误:

Compilation error   time: 0.11 memory: 380672 signal:0Main.java:22: 
error: cannot find symbol
            string dtext = "One";
        ^
  symbol:   class string
  location: class Ideone
Main.java:37: error: cannot find symbol
        System.out.println(dtext);
                       ^
  symbol:   variable dtext
  location: class Ideone
2 errors

我的代码:

import java.util.*;
import java.lang.*;
import java.io.*;
import static java.lang.System.*;
import java.util.Scanner;
import java.lang.String;

class Ideone
{
public static void main (String str[]) throws IOException
{
    Scanner sc = new Scanner(System.in);

    //System.out.println("Please enter the month of birth");
    //int month = sc.nextInt();
    System.out.println("Please enter the day of birth");
    int day = sc.nextInt();

    //day num to day text
    if (day == 1)
    {
        string dtext = "One";
    }
    else if (day == 2)
    {
        string dtext = "Two";
    }
    else if (day == 3)
    {
        string dtext = "Three";
    }
    else
    {
        System.out.println("Error, day incorrect.");
    }

    System.out.println(dtext);
}
}

我做了一些研究,发现java找不到字符串变量,但是为什么呢? 变量已定义,打印语句正确。

【问题讨论】:

  • Java iS 案例敏感度。

标签: java compiler-errors symbols


【解决方案1】:

java 中没有string 类。有String类。

string dtext = "Two";

应该是

   String dtext = "Two";

S 必须大写。

并查看您的 String variable 范围。你被限制为 If block.Move it to top,

那么你的代码看起来像

String dtext = "";
        if (day == 1) {
            dtext = "One";
        } else if (day == 2) {
            dtext = "Two";
        } else if (day == 3) {
            dtext = "Three";
        } else {
            System.out.println("Error, day incorrect.");
        }
        System.out.println(dtext);

【讨论】:

    【解决方案2】:

    你有错别字

    String dtext = "One";  
    

    String class

    还有一件事请检查variable scope

    if (day == 1)
    {
        String dtext = "One";  //it dtext has local scope here
    }//after this line dtext is not available  
    

    if之外声明dtext

    String dtext = "";
     if (day == 1)
     {
        dtext = "One";
     }
     else if (day == 2)
     {
        dtext = "Two";
     }
     else if (day == 3)
     {
        dtext = "Three";
     }
     else
     {
        System.out.println("Error, day incorrect.");
     }
    
    System.out.println(dtext);
    

    【讨论】:

      【解决方案3】:

      字符串在 java 中不存在。您的string 首字母应为大写 -> String

      例如

      string dtext = "One"; 更改为String dtext = "One";

      来自您的代码

      if (day == 1)
      {
          string dtext = "One";
      }
      else if (day == 2)
      {
          string dtext = "Two";
      }
      else if (day == 3)
      {
          string dtext = "Three";
      }
      else
      {
          System.out.println("Error, day incorrect.");
      }
      
      System.out.println(dtext);      //this line will get error dtext variable in not reachable.
      

      您的代码需要如下所示

      String dtext ="";
      if (day == 1)
      {
          dtext = "One";
      }
      else if (day == 2)
      {
          dtext = "Two";
      }
      else if (day == 3)
      {
          dtext = "Three";
      }
      else
      {
          System.out.println("Error, day incorrect.");
      }
      System.out.println(dtext);
      

      【讨论】:

        猜你喜欢
        • 2015-08-29
        • 1970-01-01
        • 2020-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-08
        • 2012-11-10
        相关资源
        最近更新 更多