Do while loop nested in for loop wont iterate [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I need to enter N which is a number of numbers for which I need to see if they can be divided by 3. Afterwards I need to display what % of numbers from the N I can divide. Numbers need to go from 15 to 62 and they need to loop until I enter the right value each time, but they don't. Instead, they just repeat the for loop regardless of my input. Here is the code:
System.out.println("Enter N number of numbers");
int N = TextIO.getlnInt();
int number;
int counterOfDivisible = 0;
for(int i = 0; i < N ; i++) {
do {
System.out.println("Please enter a number from the 15-62 span");
number = TextIO.getlnInt();
} while (number<15 && number>62);
if(number%3==0)
counterOfDivisible++;
}
System.out.println("% of numbers from the N that can be divided by 3 is " + (counterOfDivisible*100.0)/N + "%");

number can never be <15 and >62 at the same time. Think about your condition.

Thank you all for the quick reply, first time here. Quite silly mistake on my behalf.
This seems to do the trick i was aiming for.
do {
System.out.println("Please enter a number from the 15-62 span");
number = TextIO.getlnInt();
} while (number < 15 || number > 62);

Related

I don't know how to describe this well but it's something to do with integer [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
if (Weight <6)
{
Price = 5;
System.out.println("The price of cleaning will be" + Price);
}
else if(Weight > 5 < 10)
{
Price = 8;
System.out.println("The price of cleaning will be" + Price);
}
else if(Weight >=10)
{
Price = 12;
System.out.println("The price of cleaning will be" + Price);
}
on the else if (weight > 5 < 10). I'm getting a boolean error how do I fix that part?
I expected it to know the difference. Please, I need help on this part, this work I'm doing is due in 3 hours
It is not a Java syntax.
You need to change the Weight > 5 < 10 to Weight > 5 && Weight < 10.
And by the way, we recommend using lowercase letters as initial characters for variables in Java.
Provided that the weight should be larger than five and less than ten for the condition in question, try:
if (weight > 5 && weight < 10)
brother you are are not using "&&" between two statements
so it should be something like
else if(weight > 5 && weight < 10)
Happy Coding!!!!

Remove random index from an arrayList [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have an ArrayList of 4 items. I need to remove one item randomly and display the updated ArrayList. However my random keeps targeting the second and third element on the array list. As far as I understand my random would count like this: 0 1 2 3. Shouldn't that be enough to cover my 4 elements? Why does it keep targeting the same indexes? I have tried increasing the random number (4) + 1, but that puts me out of bounds.
Random rand = new Random();
Scanner input = new Scanner(System.in);
int numberOfGuests = 4;
ArrayList<String> guestList = new ArrayList<>(4);
System.out.println("Enter 4 guests:");
for(int i = 1; i <=numberOfGuests; i++){
System.out.printf("guest%d: ", i);
guestList.add(input.nextLine());
}
System.out.println("Guest List: " + guestList);
String remove = guestList.remove(rand.nextInt(4));
System.out.printf("%s can't come%n" , remove);
System.out.println("Guest List: " + guestList);
yoy check when you user random number generation the rage is set to length of array minus one such as in your case
String remove = guestList.remove(rand.nextInt(3));

Java program that gets 10 int number and calculates the average with a loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to write a java program that gets 10 int numbers and calculates the average.
My requirements are have 3 loops in the same method, while loop, do-while, and for.
For each version, use a loop to input 10 int numbers from the user and calculate the sum. Then display the average.
I would appreciate that help on this. I am having trouble getting the 10 inputs in a loop.
Thanks!
public static void main(String[] args)
{
int sum = 0;
double average = 0;
// for-loop for 10 integers
for(int x = 1; x <= 10; x++)
{
System.out.println("Enter 10 integers ");
Scanner input=new Scanner(System.in);
}
}
}
First you need to import java.util.Scanner;
second create an instance of the class scanner like Scanner input = new Scanner(System.in);
The loop step:
"The main concept is the same for all the three loops".
You need a loop from 0 to < 10 or from 1 to <=10. Then, you need a variable to store the sum of all the variables entered. Inside the loop you will use the instance you created above to get the 10 numbers such as sum += input.nextInt().
After the loop is finish you divide the sum by 10 and return the outcome.
I hope this is clear.

Program is supposed to determine if the input number is prime or not [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
//I am supposed to do it with loops and decision statements, but it's not working. Help!
import java.util.Scanner;
public class main {
/**
* #param args
*/
public static void main(String[] args) {
//Declare variables
Scanner abc;
abc = new Scanner (System.in);
int input;
int divide = 2;
int count=0;
//Ask for input
System.out.println("Please enter an integer to determine if it is prime");
input = abc.nextInt();
//Do math
for (int x=1; x < input; x++) {
if ((input%divide) == 0)
count += 1;
divide = divide + 1;
}
if (count == 0)
System.out.println("It is a prime number");
else
System.out.println("It is not a prime number");
}
}
In your for loop, for the last iteration, x = input - 1, but that means divide = input (since divide was one greater in the beginning, and you increment both once per iteration of the loop), so count will actually be equal to 1 if the number is prime, not 0.
You're counting the number of divisors; all you need to do is determine if there is at least one divisor. Here's a better algorithm:
function isPrime(n)
if n is even
return n == 2
d := 3
while d * d <= n
if n % d == 0
return False
d := d + 2
return True
I discuss this algorithm, among others in the essay Programming with Prime Numbers at my blog, which includes implementations in Java.
It looks like count is supposed to count the number of factors of input not counting 1 and input. For readability, I'd recommend using a name like numOfFactors instead of count.
Given that, now look at your loop and answer these questions. I'm not going to give you the answer. (Yes, you can get the answer by looking at others' comments, but I think you will learn more by answering these questions anyway.)
(1) What are x and divide the first time you go through the loop, at the beginning of the loop?
(2) If you look at what happens to x and divide, there's a simple relationship between x and divide at the beginning of each time through the loop. What is it?
(3) What is x the last time you go through the loop?
(4) Based on the answers to #2 and #3, what is divide at the beginning of the last time through the loop? What will input%divide be equal to?
That's why it isn't working. Figure that out first. Then we can talk about how to can make it work more efficiently.
MORE: OK, I'll say one more thing. If all you care about is whether count is zero or not, you can quit your loop as soon as you find a factor. Like this:
if ((input%divide) == 0)
{
count += 1;
break;
}
(And if you do it that way, then instead of count you should use a boolean foundAFactor because all it says is whether you found a factor, not how many there are.)
But if you really want to know the exact number of factors, don't do that.
Hello do it like this:
for(int i = input-1; i > 0; i--) {
if((input % i) == 0) {
if(i == 1)
System.out.println("is a prime");
else
System.out.println("is not a prime");
break;
}
}
for (int x=2; x < input; x++) (change x=1 to x=2)
otherwise you end up trying to divide 5 by 5 to test if 5 is prime

Java for every 6 remove 1 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm making a system.
What I want is, for every 6 items you have to buy 5 (so when the price is 5 each item, 6 items is not 30 but 25, same with 12, 18, 24 etc...)
How would I do that?
I thought it would be something like this:
if (amount % 6 == 0) {
}</code>
But that would get it one time if I'm correct.
The modulus operator won't work in this situation.
So for an efficient solution.
int numberOfItems = 17; //however many there are
int discount = numberOfItems / 6;
double priceToPay = price * (numOfItems - discount);
By having the discount as an int you won't get any rounding or decimal part after the division.
Using modulus will only give you the discount if you have 6, 12, etc. items. What about if you have 7 items? You won't get any discount (it is not divisible by 6)! So, it would be something like this:
int numOfItems = 6; //this will be different every time
//copy numOfItems because it will be modified
int temp = numOfItems;
double price = 5;
int discount = 0;
//While there are at least 6 items
while (temp >= 6) {
discount++; //discount one item
temp -= 6; //take away 6
}
//you have to pay for the number of items, but not the discounted items
double amountToPay = price * (numOfItems - discount);
This way, every time you take away 6, you don't have to pay for 1 item.

Categories