JAVA user input
Author |
Message |
Darkshining
|
Posted: Mon Jan 23, 2006 7:35 pm Post subject: JAVA user input |
|
|
Umm.. Extremely new to JAVA
how can i write a program that ask for input on the temperature of water and see if it's below 0. if it is, then say water is frozen, if not say water is water..
thanks a ton |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Martin
|
|
|
|
|
turboliux
|
Posted: Tue Jan 24, 2006 1:21 pm Post subject: (No subject) |
|
|
this one takes input from user and gives the square of it code: | import java.io.*;
class mySquare {
public static void main (String args []) {
BufferedReader in;
String text;
int x = 0;
try {
in = new BufferedReader(new InputStreamReader(System.in));
text = in.readLine();
x = Integer.parseInt(text);
}
catch (Exception e) {
System.out.println ("error! Type only numbers!");
}
x = x*x;
System.out.println(x);
}
} |
btw, if you dont know about "if" statements, then read this example:
code: | String message = "";
boolean statement = true;
boolean statement2 = (4>5); //obviously, if you know math, it means false
if (statement){
//do something here like:
message = "awesome!";
}
else if (statement2){
message = "its ok!";
}
else {
message = "useless =(";
} |
the string message becomes "its ok!" |
|
|
|
|
|
turboliux
|
Posted: Thu Jan 26, 2006 11:04 am Post subject: (No subject) |
|
|
note: the message becomes "awesome!", not "its ok!" |
|
|
|
|
|
[Gandalf]
|
Posted: Fri Jan 27, 2006 7:18 pm Post subject: (No subject) |
|
|
Look up the Scanner class for the best way to do it.
I was in the process of making a tutorial of input using the Scanner class a while ago, but never got around to finishing it... Maybe after exams. |
|
|
|
|
|
|
|