【问题标题】:Java character comparison errorJava字符比较错误
【发布时间】:2014-09-02 20:13:14
【问题描述】:
char studentClass=(char)(br.read());        
if((studentClass>='1' && sudentClass<='10')) 

我希望程序仅在用户输入的值介于 1 到 10 之间(包括两个端点)时继续运行。 当我尝试上面的代码时,当我将 1 和 10 都括在单引号中时,我收到一条错误消息“未闭合字符文字”。

【问题讨论】:

    标签: java loops if-statement char character


    【解决方案1】:

    10 不是单个字符。 你可能想要这个

    int studentClass=Integer.parseInt(br.readLine());        
    if((studentClass>=1 && sudentClass<=10)) 
    

    【讨论】:

      【解决方案2】:

      '10' 不是字符。它是一组字符。我想知道你为什么不使用整数:

      int studentClass = Integer.valueOf(br.readLine());        
      if(studentClass >= 1 && studentClass <= 10) 
      {
        ...
      }
      

      【讨论】:

      • 谢谢,我避免使用 int 值作为用户的选择,因为如果用户输入类似 'a' 的内容,则会出错。它发生在我身上。无论如何,我很感激你的回答。
      猜你喜欢
      • 1970-01-01
      • 2014-11-28
      • 1970-01-01
      • 2013-12-08
      • 1970-01-01
      • 2014-11-11
      • 2012-06-28
      • 1970-01-01
      相关资源
      最近更新 更多