BlueJ魔术技巧错误

时间:2013-12-05 13:22:52

标签: java bluej

我的节目是:

// By Rachit Bhargava
// Using BlueJ 3.1.0
import java.io.*;
public class Magic_Trick_1
{
    public static void main(String args[])throws IOException
    {
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader read=new BufferedReader(in);
        System.out.println("Look carefully at the follwing numbers and think of one of them!");
        System.out.println("1  8 15");
        System.out.println("2  9 16");
        System.out.println("3 10 17");
        System.out.println("4 11 18");
        System.out.println("5 12 19");
        System.out.println("6 13 20");
        System.out.println("7 14 21");
        System.out.println("Now enter the row number starting from left in which your number is");
        char ch1, ch2, ch3, ch;
        ch1=(char)(read.read());
        if(ch1=='1')
        {
            System.out.println("21 20 19");
            System.out.println("18 17 16");
            System.out.println("15  7  6");
            System.out.println(" 5  4  3");
            System.out.println(" 2  1 14");
            System.out.println("13 12 11");
            System.out.println("10  9  8");
            System.out.println("Now enter the row number starting from left in which your number is");
            ch2=(char)(read.read());
            if(ch2=='1')
            {
                System.out.println(" 9 12  1");
                System.out.println(" 4  7 17");
                System.out.println("20 10 13");
                System.out.println(" 2  5 15");
                System.out.println("18 21  8");
                System.out.println("11 14  3");
                System.out.println(" 6 16 19");
                System.out.println("Now enter the row number starting from left in which your number is");
                ch3=(char)(read.read());
                if(ch3=='1')
                    System.out.println("Your number is 2");
                else if(ch3=='2')
                    System.out.println("Your number is 5");
                else if(ch3=='3')
                    System.out.println("Your number is 15");
                else
                    System.out.println("Invalid Input! :(");
            }
            else if(ch2=='2')
            {
                System.out.println("10 13  2");
                System.out.println(" 5 15 18");
                System.out.println("21  9 12");
                System.out.println(" 1  4  7");
                System.out.println("17 20  8");
                System.out.println("11 14  3");
                System.out.println(" 6 16 19");
                System.out.println("Now enter the row number starting from left in which your number is");
                ch3=(char)(read.read());
                if(ch3=='1')
                    System.out.println("Your number is 1");
                else if(ch3=='2')
                    System.out.println("Your number is 4");
                else if(ch3=='3')
                    System.out.println("Your number is 7");
                else
                    System.out.println("Invalid Input! :(");
            }
            else if(ch2=='3')
            {
                System.out.println(" 9 12  1");
                System.out.println(" 4  7 17");
                System.out.println("20  8 11");
                System.out.println("14  3  6");
                System.out.println("16 19 10");
                System.out.println("13  2  5");
                System.out.println("15 18 21");
                System.out.println("Now enter the row number starting from left in which your number is");
                ch3=(char)(read.read());
                if(ch3=='1')
                    System.out.println("Your number is 14");
                else if(ch3=='2')
                    System.out.println("Your number is 3");
                else if(ch3=='3')
                    System.out.println("Your number is 6");
                else
                    System.out.println("Invalid Input! :(");
            }
            else
                System.out.println("Invalid Input :(");
        }
        else if(ch1=='2')
        {
            System.out.println(" 7  6  5");
            System.out.println(" 4  3  2");
            System.out.println(" 1 14 13");
            System.out.println("12 11 10");
            System.out.println(" 9  8 21");
            System.out.println("20 19 18");
            System.out.println("17 16 15");
            System.out.println("Now enter the row number starting from left in which your number is");
            ch2=(char)(read.read());
            if(ch2=='1')
            {
                System.out.println("16 19  8");
                System.out.println("11 14  3");
                System.out.println(" 6 17 20");
                System.out.println(" 9 12  1");
                System.out.println(" 4  7 15");
                System.out.println("18 21 10");
                System.out.println("13  2  5");
                System.out.println("Now enter the row number starting from left in which your number is");
                ch3=(char)(read.read());
                if(ch3=='1')
                    System.out.println("Your number is 9");
                else if(ch3=='2')
                    System.out.println("Your number is 12");
                else if(ch3=='3')
                    System.out.println("Your number is 1");
                else
                    System.out.println("Invalid Input! :(");
            }
            else if(ch2=='2')
            {
                System.out.println("17 20  9");
                System.out.println("12  1  4");
                System.out.println(" 7 16 19");
                System.out.println(" 8 11 14");
                System.out.println(" 3  6 15");
                System.out.println("18 21 10");
                System.out.println("13  2  5");
                System.out.println("Now enter the row number starting from left in which your number is");
                ch3=(char)(read.read());
                if(ch3=='1')
                    System.out.println("Your number is 8");
                else if(ch3=='2')
                    System.out.println("Your number is 11");
                else if(ch3=='3')
                    System.out.println("Your number is 14");
                else
                    System.out.println("Invalid Input! :(");
            }
            else if(ch2=='3')
            {
                System.out.println("16 19  8");
                System.out.println("11 14  3");
                System.out.println(" 6 15 18");
                System.out.println("21 10 13");
                System.out.println(" 2  5 17");
                System.out.println("20  9 12");
                System.out.println(" 1  4  7");
                System.out.println("Now enter the row number starting from left in which your number is");
                ch3=(char)(read.read());
                if(ch3=='1')
                    System.out.println("Your number is 21");
                else if(ch3=='2')
                    System.out.println("Your number is 10");
                else if(ch3=='3')
                    System.out.println("Your number is 13");
                else
                    System.out.println("Invalid Input! :(");
            }
            else
                System.out.println("Invalid Input :(");
        }
        else if(ch1=='3')
        {
            System.out.println("14 13 12");
            System.out.println("11 10  9");
            System.out.println(" 8 21 20");
            System.out.println("19 18 17");
            System.out.println("16 15  7");
            System.out.println(" 6  5  4");
            System.out.println(" 3  2  1");
            System.out.println("Now enter the row number starting from left in which your number is");
            ch2=(char)(read.read());
            if(ch2=='1')
            {
                System.out.println(" 2  5 15");
                System.out.println("18 21 10");
                System.out.println("13  3  6");
                System.out.println("16 19  8");
                System.out.println("11 14  1");
                System.out.println(" 4  7 17");
                System.out.println("20  9 12");
                System.out.println("Now enter the row number starting from left in which your number is");
                ch3=(char)(read.read());
                if(ch3=='1')
                    System.out.println("Your number is 16");
                else if(ch3=='2')
                    System.out.println("Your number is 19");
                else if(ch3=='3')
                    System.out.println("Your number is 8");
                else
                    System.out.println("Invalid Input! :(");
            }
            else if(ch2=='2')
            {
                System.out.println(" 1  4  7");
                System.out.println("17 20  9");
                System.out.println("12  2  5");
                System.out.println("15 18 21");
                System.out.println("10 13  3");
                System.out.println(" 6 16 19");
                System.out.println(" 8 11 14");
                System.out.println("Now enter the row number starting from left in which your number is");
                ch3=(char)(read.read());
                if(ch3=='1')
                    System.out.println("Your number is 15");
                else if(ch3=='2')
                    System.out.println("Your number is 18");
                else if(ch3=='3')
                    System.out.println("Your number is 21");
                else
                    System.out.println("Invalid Input! :(");
            }
            else if(ch2=='3')
            {
                System.out.println(" 2  5 15");
                System.out.println("18 21 10");
                System.out.println("13  1  4");
                System.out.println(" 7 17 20");
                System.out.println(" 9 12  3");
                System.out.println(" 6 16 19");
                System.out.println(" 8 11 14");
                System.out.println("Now enter the row number starting from left in which your number is");
                ch3=(char)(read.read());
                if(ch3=='1')
                    System.out.println("Your number is 7");
                else if(ch3=='2')
                    System.out.println("Your number is 17");
                else if(ch3=='3')
                    System.out.println("Your number is 20");
                else
                    System.out.println("Invalid Input! :(");
            }
            else
                System.out.println("Invalid Input :(");
        }
        else
        System.out.println("Invalid Input :(");
        System.out.println("Did this program worked correctly? Enter 'Y' for yes and 'N' for no");
        ch=(char)(read.read());
        if(ch=='Y')
        {
            System.out.println("Thanks for using my program!");
            System.out.println("All credits go to Rachit Bhargava!");
        }
        else if(ch=='N')
        System.out.println("Please contact Rachit Bhargava in case of any error(s)");
        else
        System.out.println("Invalid Input :(");
    }
}

执行该程序的结果是:

Look carefully at the follwing numbers and think of one of them!
1  8 15
2  9 16
3 10 17
4 11 18
5 12 19
6 13 20
7 14 21
Now enter the row number starting from left in which your number is
2
 7  6  5
 4  3  2
 1 14 13
12 11 10
 9  8 21
20 19 18
17 16 15
Now enter the row number starting from left in which your number is
Invalid Input :(
Did this program worked correctly? Enter 'Y' for yes and 'N' for no
N
Please contact Rachit Bhargava in case of any error(s)

它不允许我输入ch2和ch3的值,而是直接进入最后一个选项!请帮我调试程序!我最近把这个问题添加到了codereview.stackexchange.com,但是我被告知在这里发布这个问题以纠正错误!

1 个答案:

答案 0 :(得分:0)

这里的问题是,你输入2并按下回车键。这个'enter'也是一个角色。所以基本上你一次输入2个字符。试试这个:

声明另一个角色说'temp'。 每次读到一个字符时,请插入:

    temp=(char)(read.read());

例如,当您读取ch1时,请写:

    ch1=(char)(read.read());
    temp=(char)(read.read());

在想要阅读角色的任何地方都这样做。

由于出现此问题,建议在字符串中输入字符,然后转换为字符。

希望这有帮助!