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
Why I try to use the loop to display the numbers I had put into the ArrayList, it says Unreachable code what am I doing wrong.
private void nOther1() {
ArrayList<Integer> multiples = new ArrayList<Integer>();
int n = 1;
while (1 <= 100) {
multiples.add(n);
n++;
}
// UNREACHABLE CODE
for (int num : multiples) {
System.out.println(num);
}
}
Because
while (1 <= 100)
will never terminate since 1 is always <= 100, and the compiler knows that.
See Chapter 14.21. Unreachable Statements for detailed explanation.
This condition while(1 <= 100){ is always returning true
it would be the same as you do while(true){ so all behind that point is code that NEVER will be executed..
I think you wanted trying instead:
while(n <= 100){
multiples.add(n);
n++;
}
Related
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
Not sure what is wrong here. This line if (ocean[row][col]==null && ocean2[row][col] = null). I tried ((ocean[row][col])==null && (ocean2[row][col]) = null). Still it's wrong. What is the issue?
public static void deployCompShips(String[][] ocean) {
Random rand = new Random();
System.out.println("Computer is deploying ships");
while (compShips < 5) {
int col = rand.nextInt(10);
int row = rand.nextInt(10);
if (ocean[row][col]==null && ocean2[row][col] = null) {
System.out.println((compShips+1)+". ship DEPLOYED");
ocean2[row][col] = "#";
compShips++;
}
}
}
You can't use assignment operator to compare two values.
if (ocean[row][col]==null && ocean2[row][col] = null)
^
|
using assignment operator
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 years ago.
Improve this question
I just started learning java, and I created a support code (called HELP) to help me track some variables in another code im writing. But when I try to run HELP I get this exception in return, can someone help me?
Im using INTELIJ
public static void main(String [] args){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int T = Integer.parseInt(args[2]);
for (int i = 0; i < T; i++) {
//bob vĂȘ a carta
int see;
int unseen;
if (Math.random() > .5) {
see = a;
} else see = b;
System.out.println(see);
}
}
the output is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at HELP.main(HELP.java:4)
You aren't specifying any arguments when you run the program so args[0], args[1], args[2] isn't a valid index.
In one old post founded this
// to use 10 when there aren't args...
int trials = (args.length > 0) ? Integer.parseInt(args[0]) : 10;
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
Every time I try and run this code currentCost returns as 0.
I am trying to calculate depreciation # 20% using loops with the value and age of the asset (here a car) from the user. Thank you.
float cost = Integer.parseInt(jTextField2.getText());
int vehicalAge = Integer.parseInt(jTextField3.getText());
float currentCost = 0;
for (int a = 1; a == vehicalAge; a++) {
cost = (float) (cost - (cost * 0.2));
currentCost = cost;
}
JOptionPane.showMessageDialog(this, Float.toString(currentCost));
Problem is in your for loop:
for (int a=1; a==vehicalAge;a++)
it should be
for (int a=1; a<=vehicalAge;a++)
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
public class Main {
public static void main(String [] args) {
int i = 0;
do {
System.out.println(i);
i++;
} while (i==3);
}
}
// Outputs 0
Why do iterations stop at zero? while(i==3) is a condition, which tests for equality of i to 3. But then, even after incrementing the value of i as i++ why the output is only 0?
Because it'll print i, which is 0, then will increment it, and won't loop since 3 != 1:
do {
System.out.println(i); //i is 0, will print 0
i++; //i is now 1
} while (i==3); //false
Read The while and do-while Statements to better understand how the do-while loop works.
I'm carefully assuming that you meant to write while(i != 3);, if that's the case, your program will loop and will print 0 1 2.
Your condition would be i!=3. Change to while(i!=3). You have mistakenly put the wrong condition in the while loop. You have said it to loop the while only when i==3 otherwise to exit the loop. So you need to change it to
do {
System.out.println(i);
i++;
} while (i!=3);
Hope this clarifies!
Because you increment after printing.
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 9 years ago.
Improve this question
I've just started working with some programming in java. I'm experimenting with loops and I have a problem with a for-loop where I'm having a hard time finding the mistake. Its saying that "i" is not a variable, even though i made it one in just above. Hope you guys can help!
import java.util.Scanner;
public class Loops {
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Skriv et tal");
int a = sc.nextInt();
for(int i = 0; i <= 9; i++);
{
System.out.println(a + i);
}
}
}
for(int i=0; i<=9;i++);
// ^ get rid of this
should be
for(int i=0; i<=9;i++)
Because of that the for statement ends there and the new block had been started there.
Beware of the ; behind your loop definition. It is an empty statement.