| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 888 人关注过本帖
标题:关于paint方法里不能输出变量的问题
只看楼主 加入收藏
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
收藏
 问题点数:0 回复次数:1 
关于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 输出 
2005-09-20 09:20
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
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
  }
}

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

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-20 10:29
快速回复:关于paint方法里不能输出变量的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026368 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved