Results 1 to 5 of 5
Thread: Need help with program
- 03-01-2012, 04:41 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Need help with program
I'm trying to make an simple applet that changes background according to the temperature that someone inputs. But for some reason, whenever I try to compile it, it is unable to locate the variable c, I've been stumped for a couple days

Just trying to get the background to be blue when c <= 0, orange when c > 0 && c <100, and red when c >=100
Java Code:/* <applet code="TemperatureConversion.class" width="200" height= "200"> </applet> */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class TemperatureConversion extends Applet implements ActionListener{ Label fl, cl; TextField ft, ct; public void init(){ setLayout(new GridLayout(2,2)); fl = new Label("Farenheit"); cl = new Label("Celsius"); ft = new TextField(" "); ct = new TextField(" "); add(fl); add(cl); add(ft); add(ct); ft.addActionListener(this); ct.addActionListener(this); } public void actionPerformed(ActionEvent ae){ TextField temptext = (TextField)ae.getSource(); if(temptext == ft){ String temp = ft.getText().trim(); int f = Integer.parseInt(temp); int c =(int)((f-32)*(5.0/9)); ct.setText(" " + c); }else{ String temp = ct.getText().trim(); int f = Integer.parseInt(temp); int c = (int)((f*(9.0/5)+32)); ft.setText(" " + c); } if(c <= 0){ r = 0; g = 0; b = 255; }else if((c > 0) && ( c < 100)){ r = 255; g = 165; b = 0; }else{ r = 255; g = 0; b = 0; } setBackgroundColor(r,g,b); } }Last edited by JosAH; 03-01-2012 at 05:14 AM. Reason: added [code] ... [/code] tags and removed [color] tags
-
Re: Need help with program
Your post is extremely hard to read. I suggest that you get rid of all fancy and unreadable colors and instead post your code using [code] [/code] tags with the [code] tag going above the code block and the [/code] tag going below.
- 03-01-2012, 05:16 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: Need help with program
Those variable f and c are local to the if blocks and don't exist outside of them. Move the definition to the top of the method body so they will be visible in the entire body.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-01-2012, 05:17 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 03-01-2012, 05:19 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks