import java.util.Scanner; public class Main { public static final int MAX_NUMS=40; public static void main(String[] args) { int[] nums = new int[MAX_NUMS]; Scanner scan = new Scanner(System.in); int n = 0; int num; do { System.out.print("Enter a number (-1 to quit): "); num = scan.nextInt(); if (num != -1) { nums[n] = num; n++; } } while (n < MAX_NUMS && num != -1); for (int i=n-1; i>=0; i--) { System.out.print(nums[i] + " "); } System.out.println(); } }
It's outputting:
80 70 60 50 40 30 20 10
But instead I want it to output:
10 30 50 70 20 40 60 80
Can anybody link me a YouTube tutorial or help me out with this?
If you can't figure this out you deserve to fail which ever class you're taking.
David Cook
reverse the for loop at the bottom, lmao
Mason Lee
Nah, that's not gonna fix it. This is Replace the for loop with System.out.println("10 30 50 70 20 40 60 80”);
Christopher Cox
Post this in the DPT you fool. Also, that is NOT how you use the Scanner. Look up Scanner.hasNextInt()
Robert Hernandez
not OP but does the scanner.hasnextint pause execution? if not isnt it useless as he is scanning from system.in?
Daniel Miller
Yes it pauses. You can then press CTRL+D on the terminal to send an EOF and make it continue, or just any other legal char.
Carter Flores
You fool, that's a very poor implementation. Clearly the most intelligent thing to do is to echo "10 30 50 70 20 40 60 80". OP's dumb teacher won't be able to tell!
Caleb Cruz
>do >{ Stopped reading right there
Gabriel Perez
I doubt op has learned that yet. Don't want him to get in trouble now.
Elijah Ward
for (int i=0; i
Levi Phillips
What is the purpose of the code? Also, make sure to comment stuff out, and have proper spacing and the like. The do loop is completely pointless too.
Owen Nelson
>YouTube tutorial The fact that you can't learn anything without watching some drooling retard going on and on about nothing for 15 minutes is more telling than the fact that you can't solve this problem.
Owen Lee
>> Scanner scan = new Scanner(System.in);
Every fucking time, so glad i dropped this pajeet fucking language for C
Not explaining the task/goal or your inputs makes things bit harder
Hunter Nelson
He's a retard who is uncomfortable seeing the name of the class on both sides of the assignment.
Carson Bell
inputs numbers, storing them in an array as they are read, until either the user enters the sentinel value -1 or the array is full. The code then outputs the numbers in reverse order, from last entered to first. Show how to modify the code so that instead of outputting the numbers in reverse order it outputs the first entry, then the third entry, then the fifth, and so on, and then outputs the second entry followed by the fourth, the sixth, and so on, all on one line.
Change the conditions of the loop, user. If you want every second element - increment by two. If you want to start from the first element - start from the first element.
Ian Collins
flip loop direction change incrementer to adjust step size
Jaxon Adams
Okay, got it working (kind of)
public static final int MAX_NUMS=40; public static void main(String[] args) { int[] nums = new int[MAX_NUMS]; Scanner scan = new Scanner(System.in); int n = 0; int num; do { System.out.print("Enter a number (-1 to quit): "); num = scan.nextInt(); if (num != -1) { nums[n] = num; n++; } } while (n < MAX_NUMS && num != -1); for (int i=0; i
Robert Long
Two loops, user. One for odd and one for even.
Eli Clark
And rather than printing it directly from the loop, concatenate into a string, then print the said string after both loops are finished. If you absolutely have to print during the loop, there are multiple ways of adding extra conditions inside its body. For example, you can start from zero, do incrementation by two and just before the loop hits the end condition restart it from the position 1.