import javax.swing.*;
public class VowelTest{
int choice;
public void init()
{
String input;
  input = JOptionPane.showInputDialog("Enter the letter:" );
 try{
    
      choice = Integer.parseInt( input );  //数据类型转换:String类型转换为int类型 }catch(NumberFormatException e){
choice=0;
}
}
public static void main(String args[]){
         switch(choice){
case 96: 
case 101:
case 105:
case 111:
case 117: 
JOptionPane.showMessageDialog(null,"yuan"); 
break;
default:
JOptionPane.showMessageDialog(null,"output"); 
}

}
}我这样写,调试出错。因为无法从静态上下文中引用非静态 变量 choice
点解??我想输入AEIOU时他会弹出“这是元音”  我试了很多次,发现,如果使用switch方法,他需要int类型 ,但是input 输入的是String类型。 应该如何转换比较好呢?

解决方案 »

  1.   

    我想你应该首先了解一下class和Object之间的关系。
      

  2.   

    int choice;把这句换成 static int choice;
      

  3.   

    /**
     * $Id:  $
     *//**
     * @author Administrator
     *
     */
    /**
     * $Id:  $
     *//**
     * @author Administrator
     *
     */
    import javax.swing.*;
    public class VowelTest{
        static int choice ;
        public static  void init()
        {
            String input;
              input = JOptionPane.showInputDialog("Enter the letter:" );
             try{
         char c = input.charAt(0);
          choice = c;  //数据类型转换:String类型转换为int类型    }catch(NumberFormatException e){
            choice=0;
            }
            }
    public static void main(String args[]){
              
                 init();
                 System.out.println(choice);
                 switch(choice){
                case 97: 
                case 101:
                case 105:
                case 111:
                case 117: 
                case 65: 
                case 69:
                case 73:
                case 79:
                case 85:
                JOptionPane.showMessageDialog(null,"yuan"); 
                break;
            default:
            JOptionPane.showMessageDialog(null,"output"); 
            }
        
        }
    }改好了 自己琢磨一下
      

  4.   

    楼上 nterpb(曾曾胡)正解.只要给 static
      

  5.   

    我有几个地方不太明白,
    static int choice ; 为什么要用静态呢?
      char c = input.charAt(0);
          choice = c;    不明白 将char型的c转为int型?init();
    System.out.println(choice);  作用是什么呢?
    不好意思阿,麻烦大家了,因为我是初学者。就教教我吧! 
    能不能教教我一些关于强制转换的方法和语句呢?