Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

WAR (card game)

JavNewbieJavNewbie Member Posts: 5
I have to create a program which will print out integers from 1-100. The trick is, I have to use an arraylist and I must begin from the middle of the program. The output should look like
13,11,9,7,5 ,3,1,2,4,6,8,10,12
and so on until all 100 ints are on the screen.
When the program finishes running int 1 should be in the middle.

Does anyone have any suggestions.

Comments

  • KyonKyon Member Posts: 113
    Hi.

    Some way could be this:
    [code]
    public class Test3 {
    public static void main(String[] argv) {

    // create an intarray of 101 elements
    // the first one won't be needed though but I take this one
    // because then the indices are the same as the contained numbers
    // intarray[0] will never be used here.
    int[] intarray = new int[101];
    // fill it ascending with ints
    for (int i = 1; i <= 100; i++) {
    intarray[i] = i;
    }
    // now has the form: intarray[1]=1, intarray[2]=2 etc.

    // first take all the odd numbers
    // (that's every second one so we subtract 2 from i)
    for (int i = 98; i >= 0; i -= 2) {
    System.out.print(" " + (intarray[i]+1));
    }

    // then take the even numbers:
    for (int i = 1; i < 100; i += 2) {
    System.out.print(" " + (intarray[i]+1));
    }

    }
    }
    [/code]

    But I'm sure there are simpler ways to accomplish what you want. You could do all this without the use of the array by just taking the for-loops' counter (i).

    Nevertheless, this one does what you've been talking about. Output looks like: 99 97 95 ... 5 3 1 2 4 6 ... 96 98 100 with the 1 in the middle.

    Cheers,
    Kyon
    -- planning is the substitution of hazard by error --

  • JavNewbieJavNewbie Member Posts: 5
    [b][red]This message was edited by JavNewbie at 2002-9-14 12:40:27[/red][/b][hr]
    : Hi.
    :
    : Some way could be this:
    : [code]
    : public class Test3 {
    : public static void main(String[] argv) {
    :
    : // create an intarray of 101 elements
    : // the first one won't be needed though but I take this one
    : // because then the indices are the same as the contained numbers
    : // intarray[0] will never be used here.
    : int[] intarray = new int[101];
    : // fill it ascending with ints
    : for (int i = 1; i <= 100; i++) {
    : intarray[i] = i;
    : }
    : // now has the form: intarray[1]=1, intarray[2]=2 etc.
    :
    : // first take all the odd numbers
    : // (that's every second one so we subtract 2 from i)
    : for (int i = 98; i >= 0; i -= 2) {
    : System.out.print(" " + (intarray[i]+1));
    : }
    :
    : // then take the even numbers:
    : for (int i = 1; i < 100; i += 2) {
    : System.out.print(" " + (intarray[i]+1));
    : }
    :
    : }
    : }
    : [/code]
    :
    : But I'm sure there are simpler ways to accomplish what you want. You could do all this without the use of the array by just taking the for-loops' counter (i).
    :
    : Nevertheless, this one does what you've been talking about. Output looks like: 99 97 95 ... 5 3 1 2 4 6 ... 96 98 100 with the 1 in the middle.
    :
    : Cheers,
    : Kyon
    : -- planning is the substitution of hazard by error --
    :
    :



    Thanks Kyon....Hopefully I can help you in the future.

  • Shawn CarterShawn Carter Member Posts: 0

    _________ \ http://forcoder.org \ free ebooks and video tutorials about [ Scratch, R, C, Perl, MATLAB, Objective-C, Ruby, Java, Python, PHP, C++, Swift, Assembly, PL/SQL, Delphi, Go, C#, Visual Basic, JavaScript, Visual Basic .NET Bash, Scala, LabVIEW, ABAP, Erlang, Ada, Hack, F#, D, FoxPro, Apex, Fortran, Awk, Transact-SQL, ML, Rust, Julia, Lua, SAS, Alice, Prolog, Dart, COBOL, Scheme, Crystal, Lisp, Clojure, VBScript, Logo, Kotlin ]

Sign In or Register to comment.