Kiedyś już miałem taki problem. Mianowicie mam coś w rodzaju kalkulatora ale potrzebuje trzech wprowadzonych danych. Czy ktoś mugłby mi powiedzieć jak to zrobić.
Proszę o pomoc.
Proszę o pomoc.
Czy wolisz polską wersję strony elektroda?
Nie, dziękuję Przekieruj mnie tamimport java.io.*;
public class Calculate {
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
/**
* Gets two numbers from user, adds them, and prints out the result.
*/
void add() {
float n1 = getNumber();
float n2 = getNumber();
float sum = n1 + n2;
putNumber(sum);
}
/**
* Outputs a number to the screen.
*/
void putNumber(float result) {
System.out.println(result);
}
/**
* Asks the user to input a number and returns it.
*/
float getNumber() {
while (true) {
String s = "";
try {
System.out.print("Enter a number: ");
s = in.readLine();
return Float.parseFloat(s);
} catch (NumberFormatException e) {
System.err.println("'" + s
+ "' is not a valid decimal number.");
} catch (IOException e) {
System.err.println("Could not read a number from user input!");
}
System.err.println("Try again.");
}
}
/**
* Entry point for the program. Simply calls add method.
*/
public static void main(String[] args) {
Calculate cal = new Calculate();
cal.add();
}
}