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 8 years ago.
Improve this question
I am fairly new to Java and I ran into this problem once or twice in one of my books. This is a super simple program and I just don't understand why it's not working. I know when you use return, anything after it in the method is meaningless. So does this mean after you perform an for statement or an if statement that is a return?
I am using Java 8 on Windows 8 in the latest version of Eclipse.
This is my simple program:
// Find the sum of 1 through 50 and the average.
class SumAndAverage
{
public static void main(String args[])
{
int sum = 0;
double average = 0;
for(int i = 1; 1 <= 50; i++)
{
sum += i;
}
// the following code is "unreachable"
average = sum / 100;
System.out.println("The sum is: " + sum);
System.out.println("The average is " + average);
}
}
1 is always less than or equal to 50, isn't it? You probably meant to compare i with 50:
for(int i = 1; i <= 50; i++)
{
sum += i;
}
for(int i = 1; 1 <= 50; i++)
1 is always less than or equal to 50.
Related
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 4 months ago.
Improve this question
Loop
how to loop this without using array? I'm clearly confuse in this in java module I don't know the solution ;-; I'm new to java pls helppp mee
Try this.
for (int i = 5; i <= 25; i += 5)
System.out.println(i / 2);
Or
for (int i = 1; i <= 5; i++)
System.out.println(i * 5 / 2);
output:
2
5
7
10
12
Think for yourself why this produces the correct result.
public class Main {
public static void main(String[] args) {
int outputNum = 0;
for(int i = 0; i < 5; i++){
if (i % 2 == 0){
outputNum += 2;
}
else {
outputNum += 3;
}
System.out.print(outputNum + (i == 4 ? "" : " "));
}
}
}
This does exactly what you need.
Explanation:
In the loop if the i variable is even we add 2 to the output number (since you need to add 2 then 3 then 2...) and if i is uneven then we add three. At the end of the for loop we print the number without the newline using System.out.print and we separate them by a space if we aren't printing the last number using a ternary operator (i == 4 ? "" : " ") which returns a space if i is not equal to 4 and an empty string if it is, we do this to avoid having a space on the end of our output, so we get this 2 5 7 10 12 instead of this 2 5 7 10 12
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 1 year ago.
Improve this question
I have the following code fragment, which yields a sum of 16.
int sum = 1;
for (int i = 0; i <= 5; sum = sum + i++)
System.out.println(sum);
From my understanding of sum = sum + i++ and for loops in general, my thought process for the loop would be like this:
0th loop: sum = 1 (do nothing)
1st loop: sum = 1 + 1 (uses sum = 1 from previous iteration )
2nd loop: sum = 2 + 2
3rd loop: sum = 4 + 3
4th loop: sum = 7 + 4
5th loop: sum = 11 + 5
Obviously, there is an error in my logic. Could you please explain why the sum would be 16?
Change System.out.println(sum); to include the loop number as well. Also, don't forget to print the values after your loop.
int sum = 1;
for (int i = 0; i <= 5; sum = sum + i++) {
System.out.printf("loop=%d, sum=%d%n", i, sum);
}
System.out.printf("after loop, sum=%d%n", sum);
Output
loop=0, sum=1
loop=1, sum=1
loop=2, sum=2
loop=3, sum=4
loop=4, sum=7
loop=5, sum=11
after loop, sum=16
Which I trust explains both how and why sum is 16 after the loop.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Im having trouble only displaying the correct numbers, I know how to get the formula is have count-1 have 10/2 = 5 but now I want to count up from the start making it 10 15 20.
//Static void dispalySpace(double start, double end, int count){
// Display numbers between start and end inclusively
//The numbers are spaced equally
//Assume start < end and count is at least 2.
//displaySpace(10,20,3)
//The answer to the question is 10.0 15.0 20.0
while(min<max && count>=2)
for(int i=min; i<=max; i++) {
for(double j = count-1; j>=2; j++) {
System.out.print(j + " ");
}
System.out.print(i + " ");
}
System.out.println();
}
I keep just getting 10-20 displaying on a loop.
You first need to calculate the steep size, ie what do you have to add to min so that you end up with count numbers. For the first output, you won't have to add anything, so it's one less than count , ie
double step = (max-min) / count - 1;
That's what you have to add each step of the loop, starting with min , so
for (double i = 0; i <= max ; i = i + step) {
System.out.print(i) ;
}
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 started learning java yesterday and this is the first time i'm using array. This is the code
import java.util.Scanner;
public class array
{
public static void main(String[] args)
{
int num[];
num = new int[5];
Scanner input = new Scanner(System.in);
int i;
System.out.println("Insert 5 numbers:");
for(i = 0; i < 5; i = i + 1);
{
System.out.print("Insert the " + i + "° number: ");
num[i] = input.nextInt();
}
System.out.print("The numbers you entered are: ");
for(i = 0; i < 5 ; i = i + 1)
{
System.out.println(num[i] + " ");
}
}
}
When i try to run it i get this problem:
Insert 5 numbers:
Insert the 5° number: 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at array.main(array.java:14)
Two things.
You have a semicolon at the end of your loop. That will cause the loop to run until i = 5 and you're kind of stuck with that value now. Remove it.
i will remain 5 after the first loop for the same reason as above. Declare and initialize i inside of your for statement.
for(int i = 0; i < 5; i = i + 1) {
// the rest of your block
}
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 5 years ago.
Improve this question
If I am not mistaken, this bit of code should print out all even numbers smaller than or equal to 100. When I run this code, nothing happens. No error message or anything. I'm using Eclipse.
public class Even {
public static void main(String args[]) {
int number = 1;
int remainder = number % 2;
while(number <= 100) {
number++;
if(remainder == 0) {
System.out.println(number);
}
}
}
}
As others stated before, value of remainder is set only once. However, you check it a hundred times expecting it to tell you something else every time. I'd suggest putting it inside a loop, so you get a "fresh" value for every number.
public class Even {
public static void main(String args[]) {
int number = 1;
int remainder;
while(number <= 100) {
number++;
remainder = number % 2;
if(remainder == 0) {
System.out.println(number);
}
}
}
}
The reminder doesn't change as the assignment is prior to while loop. The statement int remainder = number % 2; has to be inside while loop to see the expected result.
for(int i = 0;i<=100; i= i+2)
System.out.println(i);
You do not need remainder :)