Skip to content
Advertisement

Tag: for-loop

how to print a single output in for loop?

How can I print only one output in for loop? If the number is in array then it will print “Present” but if the number is not in the array it will print “Nope”. And I need to search the number the user inputed. Input: output: Expected Output: Present Answer You need to maintain a variable outside the loop and

How to Flip the triangle?

How to flip this triangle? So i was making aritmethic sequance triangle. It was upside down. How do I turn it 180 degree? for example: 1=1 1+2=3 1+2+3=6 etc… my code: Answer You can do it for any n, by getting input from the user

Program ignoring the last row in the file in calculation

I have a text file with data that looks like this (TestData.txt): My code parses the file and does some calculations with it. However, in the method arrangeList() within which calls another method called getTestAvg() (calculates column means), the program ignores Tyler Perez’s scores. I noticed that the results I am getting were inaccurate so I went and printed the

Calculate product in Java, term vs. a ‘for’ loop, different results

Consider: Why are the results different although the calculation is even? Is there an explanation? Answer 49*48*47*46*45*44 is a multiplication of int literals, and therefore performs int multiplications resulting in an int value. It overflows in this case (since the result is larger than Integer.MAX_VALUE) before you assign the result to the long variable. Hence the result is incorrect. Change

Can we use semicolon independently at the beginning of a for-loop?

What is the meaning of this type of syntax in the for-loop? Answer A for statement is composed of 3 parts for(statement A; statement B; statement C): Statement A is the initializer statement. It is executed only one time. Usually, you create your variables you want to use in the for loop Statement B is the stop condition. It is

Skipping To The Next if Statement from Inside a for Loop

I’ve been learning some Java in my spare time and I’m a beginner, so I’m very sorry if I don’t understand some simple concepts. I’ve been trying to make a “Robot” move around an already made map. I’ve been trying to make lines of a text file into multiple arrays (ie. moving forward, turning left, turning right). Then, I’ve been

Advertisement