关于循环的Java帮助

时间:2014-03-14 15:18:22

标签: java loops while-loop try-catch

我对这个编程的东西基本上是新手。有一部分代码我无法理解,也不知道该怎么做。可能是我们的教练没有讨论的内容。我想要发生的是当我输入“Y”时,它会重试整个程序。如果我输入“N”,它将退出。这是代码

import java.io.*;
import javax.swing.JOptionPane;

public class Doge {

    public static void main(String[] args) 
    {
        int start = 0;
        int end = 0;
        int step = 0;
        String input = "";
        BufferedReader in = new BufferedReader (
                new InputStreamReader (System.in));

        try 
        {
            System.out.println("Input starting number: ");
            input = in.readLine();
            start = Integer.parseInt(input);

            System.out.println("Input ending number: ");
            input = in.readLine();
            end = Integer.parseInt(input);

            System.out.println("Input step number: ");
            input = in.readLine();
            step = Integer.parseInt(input);

        }
        catch (IOException e)
        {
            System.out.println("Error");
        }

        System.out.println("");
        if (start>=end)
        {
            System.out.println("Starting number should be lesser than the ending number.");
        }
        while (start<=end)
        {
            System.out.println(start);
            start = start+step;
        }

        String result = "";
        char y = 'Y';
        char n = 'N';

        BufferedReader it = new BufferedReader (
                new InputStreamReader(System.in));
        try 
        {
            System.out.print("Do you want to retry (Y/N)? ");
            result = it.readLine();
        }
        catch(IOException e)
        {
            System.out.println("Error");
        }
        start = 0;
        end = 0;
        step = 0;
        input = "";
        BufferedReader in2 = new BufferedReader (
                new InputStreamReader (System.in));
        while ("y".equals(result))
        {
            try 
            {
                System.out.println("Input starting number: ");
                input = in2.readLine();
                start = Integer.parseInt(input);

                System.out.println("Input ending number: ");
                input = in2.readLine();
                end = Integer.parseInt(input);

                System.out.println("Input step number: ");
                input = in2.readLine();
                step = Integer.parseInt(input);
            }
            catch (IOException e)
            {
                System.out.println("Error");
            }

            System.out.println("");

            while (start<=end)
            {
                System.out.println(start);
                start = start+step;
            }
        }

        if ("n".equals(result))
            System.exit(0);
    }
}

我很困惑。我在考虑编写相同的代码,但输入起始编号行后,为步骤编号设置了一个值。

1 个答案:

答案 0 :(得分:1)

了解Java中的do..while循环.... here

我在您的代码中发现的问题

  1. BufferedReader 不重用
  2. 您的重试结果仅检查一次,但不是每次完成一个流程循环
  3. 字符串与“y”比较,但不是您定义的char y = 'Y';char y = 'Y';应为String y = "Y";(与.equals()的比较区分大小写。因此,如果您想在考虑中加入'y',则可以使用.equalsIgnoreCase()
  4. {和}不能很好地配合显示相同范围内的语句(易于阅读)
  5. 我重写了你的代码,希望可以帮助你理解更多

    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class Doge {
    
        public static void main(String[] args) 
        {
            int start = 0;
            int end = 0;
            int step = 0;
            String input = "";
            String result = "";
            boolean invalidInput = false;
            //String y = "Y";
            //String n = "N";
    
            //reuse same BufferedReader
            BufferedReader in2 = new BufferedReader (
                    new InputStreamReader (System.in));
    
            //use do while to have statement run once then only check retry 
            do
            {
                try 
                {
                    do
                    {
                        System.out.println("Input starting number: ");
                        input = in2.readLine();
                        start = Integer.parseInt(input);
    
                        System.out.println("Input ending number: ");
                        input = in2.readLine();
                        end = Integer.parseInt(input);
    
                        if (start >= end)
                        {
                            System.out.println("Starting number should be lesser than the ending number.");
                            invalidInput = true;
                        }
                        else
                            invalidInput = false;
                    } while (invalidInput);  //loop as long as the start >= end
    
                    System.out.println("Input step number: ");
                    input = in2.readLine();
                    step = Integer.parseInt(input);
                }
                catch (IOException e)
                {
                    System.out.println("Error");
                }
    
                System.out.println("");
    
                while (start<=end)
                {
                    System.out.println(start);
                    start = start+step;
                }
    
                try 
                {
                    System.out.print("Do you want to retry (Y/N)? ");
                    result = in2.readLine();
                    //it will exit if answer is "N" or "n"
                    //check whether result is String n (ignore case)
                    //if (n.equalsIgnoreCase(result))
                    //only accept "n"
                    if ("n".equals(result))
                        System.exit(0);
                }
                catch(IOException e)
                {
                    System.out.println("Error");
                }
            //} while (y.equalsIgnoreCase(result)); //loop if "y" or "Y"
            } while ("y".equals(result));
    
            //if here have other process, then your result that is neither 'Y' nor 'N' will run here    
        }
    }
    

    运行后,我只知道.equals().equalsIgnoreCase()只能用于String而不能用char