不是阅读用户输入

时间:2014-11-17 23:59:16

标签: java

我试图编写一个要求输入1,2或q的程序。 1将要求输入,将打印输入,然后打印调整后的输入。 2将与1完全相反,要求输入然后打印解码版本。 q将退出该计划:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class StringyString {
    public static void main (String[] args) throws java.io.IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        //int n;
        boolean whatever  = true;

        while(whatever){
                System.out.println("If you would like to type something w/o using the formula, type 1");
                System.out.println("If you would like to type something using the formula, type 2");
                System.out.println("If you would like to quit, type q");

                str = br.readLine();


                if(str == "1"){
                    System.out.println("Type something below and press enter to end your input");
                    String str1 = br.readLine();
                    System.out.println("Your word is " + str1);
                    System.out.println("Your altered word is: ");

                    String str2 = str1.replace('A', 'Q');
                    String str3 = str2.replace('E', 'W');
                    String str4 = str3.replace('I', 'L');
                    String str5 = str4.replace('O', 'P');
                    String str6 = str5.replace('U', 'G');
                    String str7 = str6.replace('1', 'J');
            //str = s7r.replace(‘J uuuuu’, ‘1’);
                    String str8 = str7.replace('5', 'S');
                    String str9 = str8.replace('S', '5');
                    String str10 = str9.replace('8', 'B');
                    String str11 = str10.replace('B', '8');

                    System.out.println(str11);

                }
                if(str == "2"){
                    System.out.println("Type something below and type '/.' to end your input");
                    str = br.readLine();
            //n = Integer.parseInt(str);
                    System.out.println("Your word is " + str);
                    System.out.println("Your decoded word is: ");

                    str = str.replace('Q', 'A');
                    str = str.replace('W', 'E');
                    str = str.replace('L', 'I');
                    str = str.replace('P', 'O');
                    str = str.replace('G', 'U');
                    str = str.replace('1', 'J');
                    str = str.replace('J', '1');
                    str = str.replace('5', 'S');
                    str = str.replace('S', '5');
                    str = str.replace('8', 'B');
                    str = str.replace('B', '8');

                    System.out.println(str);

        }
                while(str != "q"){
                    whatever = true;

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

                }
    }
}

但是,每次运行程序时,除了我的输入外,它都没有。 输出: 如果您想使用公式键入某些内容,请键入1 如果要使用公式键入内容,请键入2 如果要退出,请键入q [输入] [没有任何反应,但你可以继续投入]

任何人都可以为我解决这个问题吗? TIA并对不起,如果这是格式化代码的错误方法

2 个答案:

答案 0 :(得分:0)

你的问题就在这一部分:

while(str != "q")
{
    whatever = true;
}

在这里,您进入了一个无限循环,系统没有读取您的任何值 - 只是永远循环。另一个问题是,即使str是" q",您也永远不会将str设置为false来打破while(whatever)循环。请尝试使用此代码:

if(str.equals("q"))
{
    whatever = false;
}

正如John3136所指出的那样,你不应该使用==运算符来比较Java中的字符串,而应该使用str.equals(value)代替。

如需进一步更改,请不要命名变量whatever;这个名称并没有告诉你变量在做什么,这将使你的程序调试变得更加困难。

答案 1 :(得分:0)

你的代码很长很糟糕......但是你可以试试这个,我做了最小的修改......

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class test {
public static void main (String[] args) throws java.io.IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str;
    //int n;
    boolean whatever  = true;

    while(whatever){
            System.out.println("If you would like to type something w/o using the formula, type 1");
            System.out.println("If you would like to type something using the formula, type 2");
            System.out.println("If you would like to quit, type 0");

            str = br.readLine();


            if(Integer.parseInt(str)==1){
                System.out.println("Type something below and press enter to end your input");
                String str1 = br.readLine();
                System.out.println("Your word is " + str1);
                System.out.println("Your altered word is: ");

              str1 = str1.replace('A', 'Q');
                str1 = str1.replace('E', 'W');
                 str1 = str1.replace('I', 'L');
                 str1 = str1.replace('O', 'P');
                str1 = str1.replace('U', 'G');
                str1 = str1.replace('1', 'J');
        //str = s7r.replace(‘J uuuuu’, ‘1’);
                 str1 = str1.replace('5', 'S');
                 str1 = str1.replace('S', '5');
                str1 = str1.replace('8', 'B');
                str1 = str1.replace('B', '8');

                System.out.println(str1);

            }

            else if(Integer.parseInt(str)==2){
                System.out.println("Type something below and type '/.' to end your input");
                str = br.readLine();

                System.out.println("Your word is " + str);
                System.out.println("Your decoded word is: ");

                str= str.replace('Q', 'A');
                 str= str.replace('W', 'E');
                 str= str.replace('L', 'I');
                 str= str.replace('P', 'O');
                 str= str.replace('G', 'U');
                 str= str.replace('1', 'J');
                 str= str.replace('J', '1');
                 str= str.replace('5', 'S');
                 str= str.replace('S', '5');
                 str= str.replace('8', 'B');
                 str= str.replace('B', '8');

                System.out.println(str);
                }
            else if(Integer.parseInt(str)==0){
            System.out.println("Quit");
             whatever = true;
             break;
             }
             else{
             System.out.println("Wrong Input Try Again!");
             }


    }



  }          
}
相关问题