Pessoal acabei de entrar em um curso de Engenharia da Computacao aqui em Varsovia / Polonia. estou tendo bastante dificuldade de acompanhar o ritimo do curse de java. Estou tentando resolver o meu segundo exercicio mas so esta dando erro! alguem pode me dar uma luz onde estou errando?
o que o professor pede…
Write a program reading integers until zero is entered and printing the length of
the longest sequence of consecutive numbers of the same value (and this value). For
example, for
2 2 2 11 11 11 5 5 2 3 3 2 2 2 2
the result should be Longest sequence: 4 times number 2.
Do not use arrays, strings or any other kind of collections.
eu escrevi isso aqui…
// Boolean for checking if there's a character in the sequence
// Scan all numbers typed until 0 is typed
while (scanner.hasNext()) {
// Check for integer input
if (!scanner.hasNextInt()) {
// Check for finishing program
if (scanner.hasNext("q") || scanner.hasNext("Q"))
System.exit(0);
// Checks if we do have a char in the sequence and just raises instruction once
if(!hasChar) {
// Instruction raise
System.out.println("Please give an integer sequence");
hasChar = true;
}
// Scan next element, prevents infinite looping
scanner.next();
}else {
// Breaks outside loop if "0" is typed
if (scanner.hasNext("0"))
break;
// Scans integer and assign to an int
sequence = scanner.nextInt();
// Increases counting for reading number
if (number == sequence) {
counter++;
// Checks if have found longer sequence
if (longestCount < counter) {
longestCount = counter;
longestNumber = number;
}
// If is a new number sequence, make this the reading number and start counting from 0
} else {
number = sequence;
counter = 0;
}
}
}
// If just one number was typed then the longest number of a sequence is the first
if (longestCount == 0)
longestNumber = sequence;
// Results print if there is only integers
if (!hasChar)
System.out.println("Longest sequence: " + (longestCount + 1) + " times " + "number " + longestNumber);
// Next line
scanner.next();
}
}
// Thank you!
}