Wyskakuje komunikat ze za długo kompiluje. jestem świadomy ze musze napisać inaczej kod ale jestem ciekaw czy ma sens takie rozwiązanie.
ackage pl.codegym.task.task07.task0706;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Ulice i domy
*/
public class Solution {
public static void main(String[] args) throws Exception {
//tutaj wpisz swój kod
int[] tablica = inicjalizujTablice();
int a = parz(tablica);
int b = nieParz(tablica);
sprawdz(a, b);
}
public static int[] inicjalizujTablice() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int[] tablica = new int[15];
for(int i = 0; i<15; i++) {
tablica[i] = Integer.parseInt(reader.readLine());
}
return tablica;
}
public static int parz(int[] tablica) {
int sumaParz = 0;
for(int i=0; i<15; i++) {
while(i%2 == 0)
sumaParz += tablica[i];
}
return sumaParz;
}
public static int nieParz(int[] tablica) {
int sumanieParz = 0;
for(int i=0; i<15; i++) {
while(i%2 != 0)
sumanieParz += tablica[i];
}
return sumanieParz;
}
public static void sprawdz(int sumaParz, int sumanieParz) {
if(sumaParz>sumanieParz) {
System.out.println("Więcej ludzi mieszka w domach o parzystych numerach.");
} else if (sumaParz<sumanieParz) {
System.out.println("Więcej ludzi mieszka w domach o nieparzystych numerach."); }
}
}
goral
Poziom 13
Prośba o sprawdzenie
Dyskutowane
Komentarze (2)
- Popularne
- Najnowsze
- Najstarsze
Musisz się zalogować, aby dodać komentarz
Mikołaj
31 stycznia 2022, 21:53
Instead of these two lines:
while(i%2 == 0)
while(i%2 != 0)
you should use if:
if(i%2 == 0)
if(i%2 != 0)
Rest of your code looks correct.
0
Guadalupe Gagnon
21 stycznia 2022, 21:59
Your problem is this line:
That is an infinite loop. Other than that (in both the methods), the code looks good enough to pass. 0