循环概念程序

时间:2014-09-22 16:13:11

标签: loops for-loop foreach while-loop

我一直在努力学习Java,并在短时间内使用不同的概念。我决定制作一个循环程序,以进一步巩固我对不同类型循环的了解。

下面的程序旨在接受用户输入,在我创建的每个循环类中创建一个不同的变量,并循环该次数,将整数增加1。 e.g

“输入输入” 2

#p> foreach 1 foreach 2 而1 而2 为1 为2

然而,在制作程序后,我得到的输出完全不同。我得到For循环高达49 + - 1,每个数字高/低用户输入,每个重复0 49次。 While循环不运行。在谷歌上搜索并浏览StackOverflow后,我无法解释为什么我的程序无效。


import java.io.IOException;

public class LoopPlay {
    static int loopNumber = 0;
    public static void main (String [] args){
        System.out.println ("Please enter a number to loop by");
        try {
            loopNumber = System.in.read();
            ForLoop.DoLoop();
            WhileLoop.DoLoop();
            EnhancedForLoop.DoLoop();
        } catch (IOException e) {
            System.out.println("Did you enter an integer?");
            main(args);
            e.printStackTrace();
        }       
    }
}



public class ForLoop extends LoopPlay{
    static int loops = LoopPlay.loopNumber;
    public static void DoLoop (){
        int i;
        for (i=0; i<loops; i++){
            System.out.println ("Forloop = " + i);
        }
    }
}



public class WhileLoop extends LoopPlay {
    static int loops = LoopPlay.loopNumber;
    public static void DoLoop(){
        int i = LoopPlay.loopNumber;
        while (i < loops){
            System.out.println("Whileloop =" + loops);
            loops++;
        }
    }
}



public class EnhancedForLoop extends LoopPlay {

    static int [] loop = new int [LoopPlay.loopNumber];
    static int loops = LoopPlay.loopNumber;

    public static void ForLoop (String [] args){
        int i;
        for (i=0; i<loops; i++){
            loop[i] = i;
        }
    }
    public static void DoLoop () {
        for (int x : loop){
            System.out.println("EnhancedForLoop = " + x);
        }
    }
}

--------------------------------------------------------------------------------------

我继续使用循环程序,现在我的for和while循环方法正如我想要的那样运行,但是我无法使用增强的for循环方法来工作。

import java.util.Scanner; // Import scanner class for reading user input

public class LoopPlay { // Sets the loopNumber and allows user to select loop type

    static int loopNumber = 0; // Instantiates loopNumber for calculating num of times to loop
static Scanner scan = new Scanner(System.in); // Creates a scanner object called scan

public static void main (String [] args){ // Primary method 
    System.out.println ("Please enter a number to loop by"); // Asks user for input

    loopNumber = scan.nextInt(); // Sets loopNumber to user input

    System.out.println("\n\n\n\n\n\n\n\n\n\n\n"); // Clears the console
    System.out.println("What would you like to do?"); // Menu for user selections 
    System.out.println("1. For Loop");
    System.out.println("2. While Loop");
    System.out.println("3. Enhanced For Loop");
    System.out.println("4. Do all");

    int choice = scan.nextInt(); // Sets user choice to choice variable
    System.out.println("\n\n\n\n\n\n\n");

    switch (choice){ // Creates a switch statement to call loops based on choice
    case 1: choice = 1;
    ForLoop.DoLoop();
    break;

    case 2: choice = 2;
    WhileLoop.DoLoop();
    break;

    case 3: choice = 3;
    EnhancedForLoop.DoLoop();
    break;

    case 4: choice = 4;
    ForLoop.DoLoop();
    WhileLoop.DoLoop();
    EnhancedForLoop.DoLoop();
    }
}
}





public class WhileLoop extends LoopPlay { // This class runs a while loop using       loopNumber
public static void DoLoop(){
    int i = loopNumber; // Sets i to loop variable for this method
    while (i > 0 ){ // Outputs the value of i for loopNumber amount of times
        System.out.println("Whileloop = " + i);
        i--;
    }
    LoopPlay.main(null); // Calls the main method to restart process
}

}

public class ForLoop extends LoopPlay{ // Class for While Loop
public static void DoLoop (){
    int i; // Creates a looping variable local to DoLoop method
    for (i=loopNumber; i > 0; i--){ // Sets i to loop variable and loops that many times
        System.out.println ("Forloop = " + i); // Shows where the loop is up to
    }
    LoopPlay.main(null); // Restarts the program
}

}

import java.lang.reflect.Array;


public class EnhancedForLoop extends LoopPlay { // Class to run through using an enhanced for loop

static int enhancedLoop = (loopNumber); // creates an array with loopNumber amount of slots

public static void ForLoop (String [] args){ // Populates the enhancedLoop array
    int i;                                   // with values of i
    for (i=loopNumber; i > 0; i--){
        enhancedLoop(i) = i;
    }
}
public static void DoLoop () { // Runs the enhanced loop to display all values of enhancedLoop
    for (int x: enhancedLoop){
        System.out.println("EnhancedForLoop = " + x);
    }
    LoopPlay.main(null); // Restarts the program
}

}

0 个答案:

没有答案