当前位置: 代码迷 >> Java相关 >> 关于paint方法里不能输出变量的问题
  详细解决方案

关于paint方法里不能输出变量的问题

热度:184   发布时间:2005-09-20 09:20:00.0
关于paint方法里不能输出变量的问题
import java.awt.Graphics ;
import javax.swing.JApplet ;
import javax.swing.JOptionPane;
public class Float2 extends JApplet{
public void init()
{
  String firstNumber;
  String secondNumber;
  String thirdNumber;
  float   first;
  float   second;
  float   third;
  float   sum,average,mul,max,min;
  firstNumber = JOptionPane.showInputDialog("Enter first number");
  secondNumber = JOptionPane.showInputDialog("Enter second number");
  thirdNumber = JOptionPane.showInputDialog("Enter third number");
  
  first = Float.parseFloat(firstNumber);
  second = Float.parseFloat(secondNumber);
  third = Float.parseFloat(thirdNumber);
  sum = first + second + third;
  average = sum/3;
  
     if((second>first)&&(second>third))  max = second ;
       else if((second>first)&&(second<third))  max = third ;
      else max = first ;
     
     if((second<first)&&(second<third))  min = second ;
       else if((second<first)&&(second>third)) min = third ;
      else min = first ;   
      mul = first * second * third ;
      
      String str = new String("max = " + max + "\nmin = " + min + "\naverage = "+average + "\nmul = "+ mul);
        //String title = "VC++.NET";
        //JOptionPane.showMessageDialog(null,str,title,JOptionPane.PLAIN_MESSAGE);
     }
     public void paint(Graphics g)
     {
      super.paint(g);
      g.draw3DRect(15,10,270,60,true);
      g.drawString(str,25,25);  ////编译器致使 str 出错, 我改个 “ abc” 就可以运行输出
        System.exit(0);
  }
}
搜索更多相关的解决方案: 变量  paint  输出  

----------------解决方案--------------------------------------------------------
import java.awt.Graphics ;
import javax.swing.JApplet ;
import javax.swing.JOptionPane;

public class Float2 extends JApplet
{
  String str = null;
  public void init()
  {
    String firstNumber;
    String secondNumber;
    String thirdNumber;
    float   first;
    float   second;
    float   third;
    float   sum,average,mul,max,min;
    firstNumber = JOptionPane.showInputDialog("Enter first number");
    secondNumber = JOptionPane.showInputDialog("Enter second number");
    thirdNumber = JOptionPane.showInputDialog("Enter third number");
  
    first =java.lang.Float.parseFloat(firstNumber);         // 请你注意这里
    second = java.lang.Float.parseFloat(secondNumber);
    third = java.lang.Float.parseFloat(thirdNumber);
    sum = first + second + third;
    average = sum/3;
  
    if((second>first)&&(second>third))  
      max = second ;
    else if(third>first)  
      max = third ;
    else
      max = first ;
   
    if((second<first)&&(second<third))  
      min = second ;
    else if(third<first)
      min = third ;
    else
      min = first ;  
    mul = first * second * third ;
      
    str = new String("max = " + max + "\nmin = "
                           + min + "\naverage = "
                           + average + "\nmul = " + mul);
    //String title = "VC++.NET";
    //JOptionPane.showMessageDialog(null,str,title,JOptionPane.PLAIN_MESSAGE);
  }
  
  public void paint(Graphics g)
  {
    super.paint(g);
    g.draw3DRect(15,10,270,60,true);
    g.drawString(str,25,25);    // 你再看看 str 这个变量在你的程序里定义在哪里了。
   //System.exit(0);               // 把这一行去掉,这个又不是 application
  }
}

// 你的程序是不严格的,也就是说,如果用户不合理的输入, 你的程序就会出错. 比如, 输入字母或其他符号或者什么也不输入, 光敲回车. 我这里不帮你改了, 严格的代码在你另外一个帖子里.
----------------解决方案--------------------------------------------------------
  相关解决方案