【问题标题】:Using If statements with indexOf from a Dialogbox在对话框中使用带有 indexOf 的 If 语句
【发布时间】:2016-10-16 21:04:52
【问题描述】:

这里的程序应该寻找特定的句子结尾格式。 无论是声明性、感叹性还是问号。但是,我在尝试配置对话框时遇到问题。我也不确定 if 语句是否已正确配置为返回正确的响应,如果正确输入了所需的三个句子之一。

import javax.swing.JOptionPane;
public class P45
{
public static void main(String [] args)
{
String s = JOptionPane.showMessageDialog( "Please input sentence.");

if (s.indexOf('!')!=0){
JOptionPane.showMessageDialog(null, " Sentence is exclaimatory");
}
else if (s.indexOf('.')!=0){
JOptionPane.showMessageDialog(null, " Sentence is a statement");
}
else if (s.indexOf('?')!=0){
JOptionPane.showMessageDialog(null, " Sentence is a question");
}





}

}

【问题讨论】:

    标签: if-statement joptionpane indexof


    【解决方案1】:

    我发现出了什么问题,一个简单的错字以及输入对话和显示消息之间的区别,最终我也找到了 if 语句的正确格式。

    import javax.swing.JOptionPane;
    public class P45
    {
    public static void main(String [] args)
    {
    String s = JOptionPane.showInputDialog( "Please input sentence.");
    
     if (s.indexOf('!')!=-1){
      JOptionPane.showMessageDialog(null, " Sentence is exclamatory");
      }
      else if (s.indexOf('.')!=-1){
      JOptionPane.showMessageDialog(null, " Sentence is a statement");
      }
      else if (s.indexOf('?')!=-1){
      JOptionPane.showMessageDialog(null, " Sentence is a question");
      }
    
    
    
    
    
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 2018-01-07
      • 1970-01-01
      相关资源
      最近更新 更多