Reputation: 3
import java.util.Scanner;
public class TomAndJerry {
Scanner sc = new Scanner(System.in);
public String catAndMouse(int x, int y, int z) {
String result = null;
int q = 0;
System.out.println("enter the number of queries");
q = sc.nextInt();
for (int i = 0; i < q; i++) {
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();
}
if (Math.abs(x - z) < Math.abs(y - z)) {
result = "Cat A";
} else if (Math.abs(x - z) > Math.abs(y - z)) {
result = "Cat B";
} else {
result = "Mouse C";
}
return result;
}
public static void main(String[] args) {
int x = 0, y = 0, z = 0;
String result = null;
TomAndJerry tj = new TomAndJerry();
result = tj.catAndMouse(x, y, z);
System.out.println(result);
}
}
OUTPUT:-
enter the number of queries
2
1 2 3
1 3 2
Mouse C
I end up getting the output only for the last query/input provided using the scanner. How do i get the output for the remaining queries/input. Thanks for your time in advance! :)
Upvotes: 0
Views: 580
Reputation: 603
You need to use List or String[] instead of simple String object to return multiple Strings out of a function , also the if condition needs to be part of the for loop and you need to add the result in the List or the array , below is the expected output using List
import java.util.*;
public class Main {
Scanner sc = new Scanner(System.in);
public List<String> catAndMouse(int x, int y, int z) {
List<String> results = new ArrayList<String>();
int q = 0;
System.out.println("enter the number of queries");
q = sc.nextInt();
for (int i = 0; i < q; i++) {
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();
if (Math.abs(x - z) < Math.abs(y - z)) {
results.add("Cat A");
} else if (Math.abs(x - z) > Math.abs(y - z)) {
results.add("Cat B");
} else {
results.add("Mouse C");
}
}
return results;
}
public static void main(String[] args) {
int x = 0, y = 0, z = 0;
Main tj = new Main();
List<String> results = tj.catAndMouse(x, y, z);
System.out.println(results.toString());
}
}
enter the number of queries 2 1 2 3 1 3 2 [Cat B, Mouse C]
Upvotes: 2
Reputation: 2228
It is because of this piece of code -
System.out.println("enter the number of queries");
q = sc.nextInt();
for (int i = 0; i < q; i++) {
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();
}
You are asking user for a number of queries and then asking for x,y and z that many times in the loop and not processing the values inside the loop.
The sole responsibility of your catAndMouse
method should be to give a result given the input parameters. So, leave the input taking part to the main class.
You can simplify the code like below:
public class TomAndJerry {
private static Scanner sc = new Scanner(System.in);
public String catAndMouse(int x, int y, int z) {
String result;
if (Math.abs(x - z) < Math.abs(y - z)) {
result = "Cat A";
} else if (Math.abs(x - z) > Math.abs(y - z)) {
result = "Cat B";
} else {
result = "Mouse C";
}
return result;
}
public static void main(String[] args) {
System.out.println("enter the number of queries");
int q = sc.nextInt();
int x , y , z ;
String result;
TomAndJerry tj = new TomAndJerry();
for (int i = 0; i < q; i++) {
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();
result = tj.catAndMouse(x, y, z);
System.out.println(result);
}
}
}
Upvotes: 0