Wrote some code:

Wrote some code:


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?

Attached: 1476322883991.png (383x301, 112K)

If you can't figure this out you deserve to fail which ever class you're taking.

reverse the for loop at the bottom, lmao

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”);

Post this in the DPT you fool.
Also, that is NOT how you use the Scanner. Look up Scanner.hasNextInt()

not OP but does the scanner.hasnextint pause execution? if not isnt it useless as he is scanning from system.in?

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.

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!

>do
>{
Stopped reading right there

I doubt op has learned that yet. Don't want him to get in trouble now.

for (int i=0; i

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.

>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.

>> Scanner scan = new Scanner(System.in);

Every fucking time, so glad i dropped this pajeet fucking language for C

Attached: 1520488203113.jpg (720x960, 58K)

What's wrong with new Scanner(System.in)?

Not explaining the task/goal or your inputs makes things bit harder

He's a retard who is uncomfortable seeing the name of the class on both sides of the assignment.

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.

Attached: 416x416.png (416x416, 247K)

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.

flip loop direction
change incrementer to adjust step size

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

Two loops, user. One for odd and one for even.

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.

Could you show me how this would look?

what do u mean

String result = "";
for (int i=0; i