...

View Full Version : I don't get this



nelttjen
01-05-2008, 08:27 PM
I am pretty new to java and learning it from a book, I made an excercice and got something weard...
A number between 100 and 300 shows a price with a lot of zeros...
When i tried it again (without the '//' ) it worked, but what was the problem at the beginning?

The task was: create an applet that calculates the price for pens

under 10 pens 2.00€ each
between 10 and 50 1.50€ each
between 50 and 100 1.25€ each
over 100 1.10€ each

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class NewServlet extends Applet
{
double vakTekst,b,c,d,e,f,g,temp,xtra;
boolean onder10,tss10En50,tss50En100,groterDan100,v,w,x
// ,u
;
TextField vak=new TextField(20);
Button knop=new Button("Klik Hier");

public void init()
{
knop.addActionListener(new KnopHandler());
vak.addActionListener(new KnopHandler());

x=false;
add(vak);
add(knop);
}

public void paint(Graphics g)
{
if(x)
{
if(onder10)
{
temp=vakTekst*2;
g.drawString("De prijs bedraagt "+temp+" euro.",50,50);
}
else if(tss10En50)
{
temp=vakTekst*1.5;
g.drawString("De prijs bedraagt "+temp+" euro.",50,50);
}
else if(tss50En100)
{
temp=vakTekst*1.25;
g.drawString("De prijs bedraagt "+temp+" euro.",50,50);
}
else if(groterDan100)
{
/* if(u)
{
temp=vakTekst*1.1;
xtra=vakTekst*0.1;
temp=vakTekst+xtra;
g.drawString("De prijs bedraagt "+temp+" euro.",50,50);
}
else
{
*/ temp=vakTekst*1.1;
g.drawString("De prijs bedraagt "+temp+" euro.",50,50);
// }
}
else
{
g.drawString("Ongeldig bedrag...",50,50);
}
}
}

class KnopHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
vakTekst=Double.parseDouble(vak.getText());
onder10=vakTekst<10;
tss10En50=vakTekst>=10 && vakTekst<50;
tss50En100=vakTekst>=50 && vakTekst<100;
groterDan100=vakTekst>=100;
// u=vakTekst>=100 && vakTekst<300;
x=true;

repaint();
}
}
}

Gox
01-06-2008, 08:24 AM
Your description here seems fairly vague. You have more than one case of // in your code, which one are you referring to? What compiler error did you get?

For the record, your code as you posted it compiles fine for me.

nelttjen
01-06-2008, 11:24 AM
when I delete all the '//' ,the problem solved
when I enter a number between 100 and 300
like 111 i get "De prijs bedraagt 122.10000000000001 euro"
but 111 *1.1 is supposed to be 122.1

i didn't get an error, its just weard...

I use NetBeans

BubikolRamios
01-06-2008, 09:51 PM
this is from java script forum, but mybe it wil help to explain things:

http://www.codingforums.com/showthread.php?t=52075&highlight=enigma

nelttjen
01-07-2008, 05:54 PM
I found something with the words floating and binarys from the javascript thread
Aperiantly there is a problem with converting a Decimal into a binary...
and i ll be different at every other converter i gues...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum