语句错误时找不到符号 [英] Cannot find symbol if statement error

查看:80
本文介绍了语句错误时找不到符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写一个有趣的小程序,但是却遇到此错误:

I have been coding a small program for fun, but I have been getting this error:

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

我的代码

My code:

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无法找到字符串变量,但是为什么呢?
变量已定义,并且打印语句正确。

I did some research and found that java cannot find the string variable, but why? The variable is defined and, the print statement is correct.

推荐答案

没有 Java中的string 类。有 String 类。

string dtext = "Two";

应该是

   String dtext = "Two";

S 必须为大写字母。

并查看您的String 变量范围。将其限制为If block 。将其移至顶部,

And have a look on your String variable scope. You are restricted it to 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);

这篇关于语句错误时找不到符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆