I would like to know how to do this with another way except of userinput? I don’t want to write the values I want to do this the user
Calculate calculation = new Calculate(); int sum = calculation.sum(2, 5); int testSum = 7; @Test public void testSum() { System.out.println("@Test sum(): " + sum + " = " + testSum); assertEquals(sum, testSum); } }
Advertisement
Answer
I guess the following might help:
public void test() { int number1 = 0; int number2 = 0; int expected = 0; System.out.println("Enter first number"); int state = 0; Scanner scanner = new Scanner(System.in); String input = ""; while(!input.equals("E")) { input = scanner.nextLine(); input = input.toUpperCase(); if (!input.equals("") && Character.isDigit(input.charAt(0))){ switch(state) { case 0: number1 = Integer.parseInt(input); System.out.println("Enter second number"); break; case 1: number2 = Integer.parseInt(input); System.out.println("Enter expected result"); break; case 2: expected = Integer.parseInt(input); System.out.println("Result: " + (number1 + number2) + " | Expected: " + expected + System.lineSeparator()); System.out.println("Enter first number"); state = -1; break; default: break; } state++; } } scanner.close(); System.out.println("Exiting"); }