Cohen Cohen - 28 days ago 19
Java Question

Print message condition every 10 values

I'm trying to print a message, every 10 values if a condition is true in an array.

Example: user have the opportunity to log how many rows of 10 the user wanna type.

ex: 4.

then, user can type 20 values into array.

1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
so on...

now i need that under every 10 values, verify: if some of the following condition are satisfied:

if 2 == "mack".
if 3 == "rack".
if 3 && 2 == "both".
if aren't nay of the values = "none".


ex:

user log:

1 2 4 5 6 7 8 9 0 5
1 2 3 5 6 7 8 9 0 9
1 2 3 4 5 6 7 8 9 0
1 2 0 0 5 6 7 8 9 0


Console show:

1 2 4 5 6 7 8 9 0 5
mack
1 2 3 5 6 7 8 9 0 9
rack
1 2 3 4 5 6 7 8 9 0
both
1 2 0 0 5 6 7 8 9 0
none


how can i make this works>?

this i what i have till now:

public class Main {

public static void main(String[] args)
{

Scanner sc = new Scanner(System.in);
int num = sc.nextInt();

int total = num * 10;

int[] val = new int[total];

for(int i = 0; i < val.length; i++)
{
val[i] = sc.nextInt();
}
int zack = 17;
int mack = 18;

int count = 0;
String m = null;
String r = null;

String ans;

for(int i = 0; i < val.length; i++)
{
System.out.print(val[i] + " ");

count++;

if(contains(val, 17))
{
ans = "zack";
}
else if(contains(val, 18))
{
ans = "mack";
}
else {
ans = "none";
}

if(count == 10)
{
System.out.println();
System.out.println(ans);


count = 0;
}
}

}
static boolean contains(int[] arr, int item) {
for (int n : arr) {
if (item == n) {
return true;
}
}
return false;
}

}

Answer

Here's pseudocode for what you want to do:

index = 0
twoSeen = false
threeSeen = false
for each item {
   if (item == 2) {
      twoSeen = true
   }
   if (item == 3) {
      threeSeen = true
   }
   print(item)
   index = index + 1
   if (index == 10) {
      printReport(twoSeen,threeSeen)
      index = 0
      twoSeen = false
      threeSeen = false
   }
}

All you need to do is translate this into Java, and write the printReport method (which should be trivial).

 private void printReport(boolean seen2, boolean seen3) {
      if(seen2 && !seen3) {
           System.out.println("Mack");
      } else if(...

... you can fill in the rest.

There is a smarter way to detect every tenth item using the % operator, but leave that until you have working code.

Note I have provided pseudocode and a partial answer because this looks like a homework question, so what you need is nudges in the right direction.