首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BlueJ魔术错误

BlueJ魔术错误
EN

Stack Overflow用户
提问于 2013-12-05 21:22:52
回答 1查看 545关注 0票数 0

我的程序是:

代码语言:javascript
复制
// 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 :(");
    }
}

执行此程序的结果为:

代码语言:javascript
复制
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上添加了这个问题,但有人告诉我在这里发布这个问题是为了纠正错误!

EN

回答 1

Stack Overflow用户

发布于 2013-12-06 03:43:56

这里的问题是,您输入了2并按了enter键。这个'enter‘也是一个字符。所以基本上你一次输入两个字符。试试这个:

声明另一个字符,比如'temp‘。每次你读一个字符时,插入这个:

代码语言:javascript
复制
    temp=(char)(read.read());

例如,当你阅读ch1时,你可以这样写:

代码语言:javascript
复制
    ch1=(char)(read.read());
    temp=(char)(read.read());

在你想读一个字符的任何地方都要这样做。

因为发生了这个问题,所以建议在字符串中接受字符输入,然后转换为字符。

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20401164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档