d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Noobie Question > Java
Add Reply New Topic New Poll
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Jan 25 2015 07:51am
Yep this is my code

Code
import javax.swing.*;
/**
*
* @author Pär Lindman
*/
public class upg4 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String outdata, indata;
outdata = ""; // We can delcare both strings on one line.

double f, k, c;
double starttemp, slut, intervall;
final double absnoll = -273.15;
indata =JOptionPane.showInputDialog(null, "Ange start temperatur");
starttemp = Double.parseDouble(indata);
indata =JOptionPane.showInputDialog(null, "Ange slut temperatur");
slut = Double.parseDouble(indata);
indata =JOptionPane.showInputDialog(null, "Ange intervall");
intervall = Double.parseDouble(indata);

//indata = JOptionPane.showInputDialog(null, "Ange temperatur i celcius");

do(int i=starttemp; i <= slut; i+=1*intervall);

{

f = (i*9/5)+32; // Here we add two integers.
k = (i+273.15);
c = i;
outdata=(outdata + c + " " + k + " " + f + "\n");
}
while(starttemp < slut && starttemp > absnoll && starttemp <= slut);
JOptionPane.showMessageDialog(null, "Celsius " + "Fahrenheit " + "Kelvin " + "\n" + outdata);

}

}


I want to produce a window with tempratures in Celcius, Kelvin and Fahrenhet and the user should be able to write in the steplenght, temprature intervall and exit temprature. The indata should be looked up with the whileloop and the whileloop should produce the temprature table.

But as you see, im having trouble getting the program to calculate the tempratures.. im getting to write in start/intervall and exit but nothing happends after that.. Anyone got any idea how I get the while loop to work?

This post was edited by bomben on Jan 25 2015 07:52am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 25 2015 09:42am
Couple things.

1) what does slut mean in your language? exit?
2) f isn't going to be calculated correctly since your expression is an integer. use 9.0 instead of 9 to make it a double.
3) why is this in your while loop condition?
while(starttemp < slut && starttemp > absnoll && starttemp <= slut);
if your temperature is only increasing, there's no way for the start temperature to be below absolute zero. instead, move this above the loop as part of input validation
4) why do you have this in your while loop condition twice?
while(starttemp < slut && starttemp > absnoll && starttemp <= slut);
5) since starttemp is never changing, this shouldn't be in your while loop condition at all. i think you're just thinking of loop invariants?
6) you seem to be confusing do...while and for loops.
do(int i=starttemp; i <= slut; i+=1*intervall);
this parenthesis expression is in for loops, not do...while loops, and there's no semi colon at the end.
my advice is drop the do...while loop and use a for loop.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

This post was edited by carteblanche on Jan 25 2015 09:57am
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Jan 25 2015 10:09am
Ive done some changes now...

Slut = exit (slut is swedish for exit)

The only problem I got now is that it doesnt show the table :S

Code
import javax.swing.*;
/**
*
* @author Pär Lindman
*/
public class upg4 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String outdata, indata;
outdata = ""; // We can delcare both strings on one line.

double f, k;
double starttemp, slut, intervall;
final double absnoll;
absnoll = -273.15;
indata =JOptionPane.showInputDialog(null, "Ange start temperatur");
starttemp = Double.parseDouble(indata);
indata =JOptionPane.showInputDialog(null, "Ange slut temperatur");
slut = Double.parseDouble(indata);
indata =JOptionPane.showInputDialog(null, "Ange intervall");
intervall = Double.parseDouble(indata);

//indata = JOptionPane.showInputDialog(null, "Ange temperatur i celcius");

while (starttemp <= slut && starttemp >= absnoll);
{

f = (starttemp*9/5)+32; // Here we add two integers.
k = (starttemp+273.15);

outdata=(outdata + starttemp + " " + k + " " + f + "\n");

starttemp+=intervall;


}
JOptionPane.showMessageDialog(null, "Celsius " + "Fahrenheit " + "Kelvin " + "\n" + outdata);

}

}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 25 2015 10:36am
while (starttemp <= slut && starttemp >= absnoll);

why is there a semicolon there?

why do you have the second condition there?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll