fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. String[] msg0 = {"数字を入力してください ",
  6. "入力された数字の%s結果は%dです。\n"};
  7. String[] msg1 = {"足し算", "引き算", "掛け算", "割り算"};
  8. int var;
  9. Scanner scan = new Scanner(System.in);
  10. for (int i = 0; i < msg1.length; i++) {
  11. System.out.print(msg0[0]);
  12. Foo foo = new Foo(scan.nextInt());
  13. System.out.print(msg0[0]);
  14. var = scan.nextInt();
  15. System.out.printf(msg0[1], msg1[i], i == 0 ? foo.add(var): (i == 1 ? foo.sub(var): (i == 2 ? foo.mul(var): foo.div(var))));
  16. }
  17.  
  18. }
  19. }
  20.  
  21. class Foo {
  22. static int x;
  23.  
  24. public Foo(int y) {
  25. x = y;
  26. }
  27. public static int add(int y) {
  28. return x + y;
  29. }
  30. public static int sub(int y) {
  31. return x - y;
  32. }
  33. public static int mul(int y) {
  34. return x * y;
  35. }
  36. public static int div(int y) {
  37. return x / y;
  38. }
  39. }
  40.  
Runtime error #stdin #stdout #stderr 0.14s 52784KB
stdin
Standard input is empty
stdout
数字を入力してください 
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.Scanner.throwFor(Scanner.java:937)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:12)