Asked by anonymous
1)Your program will get numbers (int) from the user. Do this in a while loop so that you keep getting more numbers.
If the number is a multiple of 3 or 5, do nothing (use the "continue" command for this)
If the number is -99 then exit the loop
All other numbers should be added to the total (yes, you're making a total of all of the numbers)
After you exit the while loop print out the total
my code
package postMarchBreak;
import java.util.Scanner;
public class While99 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("pick a few numbers and type -99 to stop");
while (true){
int num = sc.nextInt();
int sum = 0;
if (num==-99)
break;
if(num%3 == 0 || num%5 == 0){
continue;
}
sc.close();
}
}
}
how can I add up all the numbers that aren't multiples of 3 or 5
If the number is a multiple of 3 or 5, do nothing (use the "continue" command for this)
If the number is -99 then exit the loop
All other numbers should be added to the total (yes, you're making a total of all of the numbers)
After you exit the while loop print out the total
my code
package postMarchBreak;
import java.util.Scanner;
public class While99 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("pick a few numbers and type -99 to stop");
while (true){
int num = sc.nextInt();
int sum = 0;
if (num==-99)
break;
if(num%3 == 0 || num%5 == 0){
continue;
}
sc.close();
}
}
}
how can I add up all the numbers that aren't multiples of 3 or 5
Answers
Answered by
oobleck
if(num%3 == 0 || num%5 == 0){
continue;
} else {
sum += num
}
continue;
} else {
sum += num
}
Answered by
anonymous
i need to add the sum of all the numbers but what you suggested is just printing what I typed
Answered by
oobleck
clearly you do not know what the += operator does. It means sum = sum + num. That is, it adds up all the numbers that failed the divisibility test.
At the end of the loop, just print the value of sum.
At the end of the loop, just print the value of sum.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.