任何人都知道为什么这段代码不断陷入无限循环 [英] anyone know why this code keeps going in an infinite loop

查看:76
本文介绍了任何人都知道为什么这段代码不断陷入无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Scanner;


public class Lab5
{
	
	 public static void main (String[]args)
         {
		Scanner in = new Scanner(System.in);
            System.out.println("How big is the multiplication");
		int choice = in.nextInt();
                while(choice>=1 && choice<=20)
                {
                System.out.print("\nX\t");
            for(int x = 1; x <= choice; x++)
            {   
                    System.out.print(x+"\t"); 
                     System.out.print("\n\t");  
            }
            for( int l=choice; l<=choice; l++)
            {
                System.out.print("-------");
                System.out.println();
            }
                
            for (int j = 1; j <= choice; j++)
{
System.out.print("row"+choice+"|\t");
for (int i = 1; i <= choice; i++)
{
    System.out.print(i * j + "\t");
}

System.out.println();
}
         
            }
                }
         }

推荐答案

int choice = in.nextInt();
while(choice>=1 && choice<=20)
{
    ...


您的循环基于变量choice中返回的值.但是,在循环结束时,您不会获得新值,也没有任何退出循环的方法.更好的方法是:


Your loop is based in the value returned in the variable choice. However, at the end of the loop you do not get a new value or have any way of breaking out of the loop. A better way would be something like:

int choice;
do
{
    System.out.println("How big is the multiplication");
    choice = in.nextInt();
    if (choice < 1 || choice > 20)
        break;  // exit the loop if choice out of range

    // all other code here

} while(choice > 0);


变量"choice"的值在任何地方都不会增加.因此解决方法是增加它.我的解决方案.告诉我我是否正确....

The value of the variable ''choice'' is not getting incremented anywhere. So the fix will be to increment it. My solution for the issue. Tell me if I am correct....

 while(choice>=1 && choice<=20)
                {
                System.out.print("\nX\t");
            for(int x = 1; x <= choice; x++)
            {   
                    System.out.print(x+"\t"); 
                     System.out.print("\n\t");  
            }
            for( int l=choice; l<=choice; l++)
            {
                System.out.print("-------");
                System.out.println();
            }
                
            for (int j = 1; j <= choice; j++)
{
System.out.print("row"+choice+"|\t");
for (int i = 1; i <= choice; i++)
{
    System.out.print(i * j + "\t");
}
 
System.out.println();
}
  choice++; // Added this line of code as fix        
}


我认为您没有完全做到:
I think you did not do fully:
/*---------------------------------------------------------------------------
// AUTHOR:          (Put your name here)
// FILENAME:        Lab5.java
// SPECIFICATION:   This program is for practicing nested loops.
//                    The first task is to print a multiplication table
//                      whose size is specified by the user.
//                    The second (optional, no extra credit, just for fun)
//                      task is to encode a string entered by the user.
//                      The encryption you will use is as follows: in the ith
//                      word, shift every letter up by i (so if the sentence
//                      is "abc abc abc", it becomes "bcd cde def").
// INSTRUCTIONS:    Read the following code skeleton and add your own code
//                  according to the comments.  Ask your TA or your class-
//                  mates for help and/or clarification.  When you see
//                  //--> or ??? that is where you need to add code.
// LAB LETTER:      (Put your Lab Letter here)
//-------------------------------------------------------------------------*/

//  Import required package (Scanner)
//-->

//Declare class (Lab5)
//-->
//-->
    //Declare the main method
    //-->
    //-->
        //Declare some variables: a Scanner (and initialize) and an integer that holds
        //the user's choice of how big the multiplication table is.
        //-->
        //-->

        //For the second task (optional) declare the following:
        //an integer variable to keep track of what word we're currently operating on,
        //a String array that holds each word int he user's sentence separately
        //a String that holds the encrypted text,
        //Give the strings initial values of "" (empty string but not null),
        //and give the first integer variable an initial value of 0
        //-->
        //-->
        //-->

        //make a do while loop that will repeat until the user enters a valid
        //input (an integer from 1 to 20).

        ???
        {

            //Ask the user how large to make the multiplication table
            //-->
            //Read in the user's input
            //-->

            //output the multiplication table if the user's input is within the
            //appropriate range (1-20)
            ???(???)
            {
                //Print out the following string used for formatting the table "\nX\t"
                //use print, not println
                //-->

                //make a for loop that prints each of the numbers from 1 to the user-specified number
                //putting a tab '\t' character in between each number (all on the same line)
                ???(???; ???; ???)
                        //-->

                //Print out the following string used for formatting the table "\n\t"
                //use print, not println
                        //-->

                //make a for loop that prints out the string "--------"  (on the same line)
                //a number of times equal to the user specified size of the multiplication
                //table (more formatting)
                ???(???; ???; ???)
                        //-->

                //Print a blank line
                //-->


                //make a for loop that will range a variable i from 1 to the user-specified
                //size of the multiplication table. At the start of each row (the outer loop),
                //you should print the current row number and the string "|\t" for formatting
                //Inside of that for loop, do the same for
                //a variable j. Each table entry will be i*j and you should print this as well
                //as tab character '\t' after each entry. Also, after each row is done, you
                //should print a blank line.
                ???(???; ???; ???)
                {
                    System.out.print(??? + "|\t");
                    ???(???; ???; ???)
                    {
                        System.out.print(??? + "\t");
                    }
                    //Print a blank line
                    //-->
                }
            }
            //otherwise print out an error telling the user the input is out of range
            ???
            {
                //-->
            }
        }???(???);
        /*



请参阅最后一部分



See this last part


这篇关于任何人都知道为什么这段代码不断陷入无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆