for loop in java not recognizing variable "i" [duplicate] - java

This question already has answers here:
Having issue with the concept of extra semi-colon at the end of for loop in Java
(5 answers)
Misplaced semicolon in for loop [duplicate]
(5 answers)
For loops and semicolons on java, the code is running just once [duplicate]
(3 answers)
Closed 29 days ago.
//Get target amount
System.out.print("\\nEnter target amount: ");
int target = scan.nextInt();
int count = 0;
for( int i = 0; i < sales.length; i++ );
{
if( sales[i] >= target )
{
count++;
System.out.println("Salesperson " + i + "hit or exceeded the target with sales of $" + sales[i] );
}
}
System.out.println("\nNumber of people who met or exceeded the target: " + count);
}
declared varible i , then started loop but does not recongize variable "i"

Related

Index is out of bounds exception [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 2 years ago.
I am trying to write a guessing game where we are not allowed to use the same card twice and we need to use an array of 15 card (objects) I have created everything (probably not in an ideal fashion) but I keep getting the error java.land.ArrayIndexOutOfBoundException: Index 15 is out of bounds for length 15 when I try to answer all questions correctly it won't complete the deck of cards. I don't understand why. Here is the relevant code:
while (CountryCard.instances < 30) {
System.out.println("Would you like to guess the capital of a country?"
+ " Hit 1 for yes, or 2 for no (Case sensitive)");
yesNo = input.nextInt();
if (yesNo == 2) {
return;
}
int randomNumber = 0 + (int) (Math.random() * ((game.length)+1 ));
while(game[randomNumber].used==true){
randomNumber = 0 + (int) (Math.random() * ((game.length)+1 ));
}
System.out.println("What is the Capital of "
+ game[randomNumber].getName() + "?");
game[randomNumber].usedCard(1);
guess = input.next();
if (guess.equals(game[randomNumber].getCapital())) {
System.out.println("Correct! :)");
} else {
System.out.println("Incorrect! :(");
}
}//end of while
System.out.println("Thanks for playing :)");
Array indices are 0 based that means the 15th element has an index of 14
The index for any Array/List starts with 0, so a object with length of 9, will have the max object index of 8
char[] a = "Apple".toCharArray();
so the length of a is 5, but a[4] = 'e'

i++ and ++i in Java [duplicate]

This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
Closed 2 years ago.
int i = 1;
i = i++;
int j = i++;
int k = i + ++i * i++;
System.out.println("i==" + i);
System.out.println("j==" + j);
System.out.println("k==" + k);
Why the result of k is 11? I'm a learner of java. Please help me and explain what's going on under the hood or give me some pointers about where I can find related learning resources if possible.
int i = 1; // i == 1
i = i++; // i is incremented, but the original value of i is assigned back to i, so i == 1
int j = i++; // i == 2
int k = i + ++i * i++; // k = 2 (the original value of i before this statement) +
// 3 (the result of pre-incrementing i) *
// 3 (the result of post-incrementing i is still 3) == 11
System.out.println("i==" + i);
System.out.println("j==" + j);
System.out.println("k==" + k);
Note that 2 + 3 * 3 is evaluated as 2 + (3 * 3).

Displaying 500 random integers [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 4 years ago.
I want to generate 500 random numbers and display them next to their indexes. I want the numbers to be 1 though 10. But when I run my code it only generates 13 random numbers and the numbers are greater than 10. Please help.
public class RandomStats {
public static void main(String[] args) {
int [] stats = new int [501];
int numRolls;
int outcome;
for (int i = 0; i <=500; i++ ){
outcome = (int) ( 10 * Math.random() + 1)
+ (int) ( 10 * Math.random () + 1);
stats[outcome] += 1;
}
for ( int i = 2; i <=500; i++) {
System.out.println(i + ": " + stats[i]);
}
}
}
While saving in the array, you are doing stats[outcome] += 1;, where your array is updated with same index again and again and thats the reason you only see ~13 values and rest as 0. I believe it should be stats[i] =outcome+ 1;

Java - Expected output after a loop printing array [duplicate]

This question already has answers here:
Java Increment / Decrement Operators - How they behave, what's the functionality?
(2 answers)
Closed 7 years ago.
I've seen this question (from a multiple choice) "what is the output of the following program" :
class array_output {
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = i/2;
array_variable[i]++;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
The expected output is:
1 2 3 4 5
It is clear to me that the value i is incremented twice, first in the body of the loop and at the last line.
But I don't really get what the line array_variable[i]++; is doing.
Any suggestions?
Thanks in advance to answer to this newbie question!
The post-fix increment and decrement operators return the value of a variable before altering its value. Consider the following:
int anInt = 0;
System.out.println("anInt: " + anInt);
// anInt: 0
System.out.println("anInt: " + anInt++);
// anInt: 0
System.out.println("anInt: " + anInt);
// anInt: 1
System.out.println("anInt: " + ++anInt);
// anInt: 2
System.out.println("anInt: " + anInt);
// anInt: 2
So basically, anInt++ returns the value of anInt before incrementing it. ++anInt increments the value of anInt before returning the (freshly-incremented) value.
array_variable[i]++; increments the value that is stored in array_variable[i] by one.

How Do I Use The Variable "i" As Part Of A String in a For Loop in Java [duplicate]

This question already has answers here:
How do I concatenate two strings in Java?
(23 answers)
Closed 7 years ago.
basically I want to do this in java
for (int i = 1; i < 10; i++) {
System.out.println("Hello, I am "i" years old");
}
So the for loop would print out
Hello, I am 1 years old
Hello, I am 2 years old
Hello, I am 3 years old
...
And so on. How do I do this?
for (int i = 1; i < 10; i++) {
System.out.println("Hello, I am " + i + " years old");
}

Categories

Resources