Commit 35ec2d32 by Patryk Czarnik

kalk 1-metodowy 1

parent 5ab198b6
package kalkulatory;
import java.util.Scanner;
public class ProgramJednometodowy {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("Podaj działanie jako + 2 3 lub Q, aby zakończyć");
String operacja = sc.next();
if("Q".equalsIgnoreCase(operacja)) {
break;
}
int liczba1 = sc.nextInt();
int liczba2 = sc.nextInt();
int wynik = oblicz(liczba1, liczba2, operacja);
System.out.printf("%d %s %d = %d\n", liczba1, operacja, liczba2, wynik);
}
System.out.println("nara");
}
public static int oblicz(int liczba1, int liczba2, String operacja) {
switch(operacja) {
case "+": return liczba1 + liczba2;
case "-": return liczba1 - liczba2;
case "*": return liczba1 * liczba2;
case "/": return liczba1 / liczba2;
default: throw new IllegalArgumentException("Nieznane " + operacja);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment