Loops 循环概念程序
我一直在尝试学习Java,并在短时间内尝试了一些不同的概念。我决定做一个循环程序,以进一步巩固我对不同类型循环的了解 下面的程序旨在接受用户输入,在我创建的每个循环类中创建一个不同的变量,并循环将整数增加1的次数。 e、 g 输入 二, foreach 1 foreach 2 而1 而2 一 两个 然而,我得到的输出是完全不同的程序制作后。对于用户输入的每一个高/低数字,以及每重复0 49次,我得到最多49+-1的For循环。While循环不运行。在谷歌搜索和查看StackOverflow之后,我一辈子都搞不清楚为什么我的程序不起作用 } }Loops 循环概念程序,loops,for-loop,foreach,while-loop,Loops,For Loop,Foreach,While Loop,我一直在尝试学习Java,并在短时间内尝试了一些不同的概念。我决定做一个循环程序,以进一步巩固我对不同类型循环的了解 下面的程序旨在接受用户输入,在我创建的每个循环类中创建一个不同的变量,并循环将整数增加1的次数。 e、 g 输入 二, foreach 1 foreach 2 而1 而2 一 两个 然而,我得到的输出是完全不同的程序制作后。对于用户输入的每一个高/低数字,以及每重复0 49次,我得到最多49+-1的For循环。While循环不运行。在谷歌搜索和查看StackOverflow之后,
}每次运行后都没有重置loopNumber,这会导致while循环立即退出。我会避免在您声明的几乎每个变量中使用static。虽然我猜在这种情况下是必需的。您正在将I和循环初始化为与WhileLoop中LoopPlay.loopNumber的当前值相同的值。因此,条件i
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);
}
}
}
--------------------------------------------------------------------------------------
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
}