How come this loop doesn't print out six numbers? [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 5 months ago.
Improve this question
package p22_09_24;
public class Lotto2 {
public static void main(String[] args) {
for (int i=0; i <6; i++) {
i = (int)(Math.random()*45)+1;
System.out.println(i);
}
}
}
As you can see, the exit is under the 'for' which means it should loop six times, but the answer is only one num.

public class Random {
public static void main(String[] args) {
for (int i = 0; i < 6; i++)
{
int randomNumber = (int) (Math.random()*45)+1;
System.out.println("Random number is: " + randomNumber);
}
}
}
use different name to store the random number.

Related

Confused about assigning integer as value of element [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
I am currently working on a project for my java class and I am having a lot of trouble with this specific part. How do I "assign randomNumber as the value of the current element within ageArray"? thank you
public class SortAndSearch {
public static void main(String args[]) {
String[] ageArray = new String[10];
ArrayList<Integer> ageArrayList = new ArrayList<Integer>();
int randomNumber;
///////////////////// PART A /////////////////////
for (int i = 0; i < 10; i++) {
randomNumber = (int)(Math.random() * 20) + 1;
/* #3: Assign "randomNumber" as the value of the current element within ageArray */
}
You should convert int to String
ageArray[i]=randomNumber+"";
or
ageArray[i]=Integer.toString(randomNumber);
or
ageArray[i]=String.valueOf(randomNumber);

What is wrong with this small for/if loop logic? [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
This code is asking for how many numbers the user will input and then it takes each of those. In the end it should return the smallest value. I know about Math.min methods, I'm just struggling with why the logic below doesn't work, it always prints the last input instead the smallest one.
import java.util.Scanner;
public class Ch5_smallestValue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Input how many numbers and then input each one");
int hMany = sc.nextInt();
int firstNum = sc.nextInt();
int smallest = firstNum;
for (int i = hMany; i > 1; i--){
int input = sc.nextInt();
if (smallest < input){
smallest = input;
}
}
System.out.println("smallest = " + smallest);
}
}
Change (smallest < input) to (smallest > input).

Java Sorting Error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
public class Main {
public static void main(String[] args) {
int z;
int [] a = new int[5];
a[0]=4;
a[1]=8;
a[2]=5;
a[3]=1;
a[4]=3;
for(;;){
z=0;
for(int i=1;i<a.length;i++){
if(a[i-1]>a[i]){
int tmp = a[i];
a[i-1]=a[i];
a[i]=tmp;
z++;
}
}
if(z==0){
break;
}
}
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}}
Hi. I have this problem. I want to sort array items, but result of this code is 1 1 1 1 3. I can't understand where is problem.
Thanks you very much!
You overwrite the value of a[i-1] thus you lose it
So you have just to change this int tmp = a[i]; with this int tmp = a[i-1];
The whole code is :
public class MailClient {
public static void main(String[] args) {
int z;
int [] a = new int[5];
a[0]=4;
a[1]=8;
a[2]=5;
a[3]=1;
a[4]=3;
for(;;){
z=0;
for(int i=1;i<a.length;i++){
if(a[i-1]>a[i]){
int tmp = a[i-1];
a[i-1]=a[i];
a[i]=tmp;
z++;
}
}
if(z==0){
break;
}
}
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}

x cannot be resolved as a variable [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 7 years ago.
Improve this question
What is wrong with this loop?
int index = 0;
for(int x = 0; x < winDate.length;x++);
{
if(userDate == winDate[x])
{
index = x;
break;
}
}
I've used x several times before in comparing values.
You have got a ";" behind the loop!
In fact this loop does nothing instead of counting.
The part below the loop is getting to initialized statically.
int index = 0;
for(int x = 0; x < winDate.length;x++)-->;<---
{
if(userDate == winDate[x])
{
index = x;
break;
}
}

How to go through the deepest elements in an array of arrays? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
public static void main(String[] args) {
int [][] a={{1,2,3},{4,5,6}} ;
for(int x:a)
System.out.print(" "+x);
}
}
Could someone please tell me how would I print each value in the array?
You have to use for each loop twice. In first loop you iterate over all the array and in the inner loop you iterate over all the integers in the outer array.
int [][] a={{1,2,3},{4,5,6}} ;
for(int []x:a){
for(int y : x){
System.out.println(y);
}
}
As you're having a dual-dimension array, you need two loops:
public static void main(String[] args) {
int [][] a={{1,2,3},{4,5,6}} ;
for(int[] r:a) {
for (int x:r) {
System.out.print(" "+x);
}
}
}

Categories

Resources