• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to make this number point?  RSS feed

 
javac java
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
========
public class TestTikus {
public static void main (String[] args) {
Tikus t = new Tikus();

t.setWeight(10);
System.out.println("Tikus ni berat:"+ t.getWeight());


}
}
=========
public class Tikus {
private float weight;
public float getWeight() {
return weight;
}

public void setWeight(float newWeight) {
if (newWeight > 0) {
weight = newWeight;
}
}
}
=======
output:10.0


how can i get the output....let say..i want 10.3
how can i do that?

and..how can i get input from user?
in others words,the programme prompt and ask the user to give the weight

thanks for helping me~
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand the first part of your question. The program is just printing whatever value you send to the method.

In order to get input from the user, you can use java.util.Scanner.
[ October 09, 2006: Message edited by: Keith Lynn ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"javac java",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
MV Krishna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by javac java:
========
public class TestTikus {
public static void main (String[] args) {
Tikus t = new Tikus();

t.setWeight(10);
System.out.println("Tikus ni berat:"+ t.getWeight());


}
}
=========
public class Tikus {
private float weight;
public float getWeight() {
return weight;
}

public void setWeight(float newWeight) {
if (newWeight > 0) {
weight = newWeight;
}
}
}
=======
output:10.0


how can i get the output....let say..i want 10.3
how can i do that?

and..how can i get input from user?
in others words,the programme prompt and ask the user to give the weight

thanks for helping me~
 
MV Krishna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use BufferedReader to read the input from the command line.

Example:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.readLine();
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!