Java变量!初始化

时间:2018-05-16 16:35:31

标签: java android

应初始化变量a,b,c和d的代码,!确实初始化

public class BufferedReaderFromUserAnd
{
  private static final String FILENAME = "F:/Android.txt";

  public static void main(String args[]) throws IOException
  {
    BufferedWriter bw = null;
    FileWriter fw = null;

    try
    {
      fw = new FileWriter(FILENAME);
      bw = new BufferedWriter(fw);

      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Word1: ");
      String Word1 = reader.readLine();
      System.out.print("Word2: ");
      String Word2 = reader.readLine();
      System.out.print("Word3: ");
      String Word3 = reader.readLine();
      System.out.print("Word4: ");
      String Word4 = reader.readLine();
      System.out.print("Y: ");
      String y = reader.readLine();
      String a = reader.readLine();
      String b = reader.readLine();
      String c = reader.readLine();
      String d = reader.readLine();      

      if(y.compareTo(a)==0)
      {                
        b = "not" ;       
        c = "not" ;                       
        d = "not" ;       
      }                

      if(y.compareTo(B)/>/>/>==0)
      {                
        a = "not" ;       
        c = "not" ;                       
        d = "not" ;       
      }

      if(y.compareTo(c)==0)
      {                
        b = "not" ;       
        a = "not" ;                       
        d = "not" ;       
      }                

      if(y.compareTo(d)==0)
      {                
        a = "not" ;       
        c = "not" ;                       
        d = "not" ;       
      }    


      String n = reader.readLine();

      bw.write("<input type='radio' name='rbnNumber' value='You selected (a) " + Word1 + "  which is " + a +  " the correct answer' />(a) "  + Word1 + "<br/>");
      bw.write("<input type='radio' name='rbnNumber' value='You selected (a) " + Word2 + "  which is " + b +  " the correct answer' />(a) "  + Word2 + "<br/>");
      bw.write("<input type='radio' name='rbnNumber' value='You selected (a) " + Word3 + "  which is " + c +  " the correct answer' />(a) "  + Word3 + "<br/>");
      bw.write("<input type='radio' name='rbnNumber' value='You selected (a) " + Word4 + "  which is " + d +  " the correct answer' />(a) "  + Word4 + "<br/>");
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally
    {
      try {
        if (bw != null)
          bw.close();
        if (fw != null)
          fw.close();
      }
      catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }
}

在提示输入a时,结果为

  

输入类型=&#39;广播&#39;命名=&#39; rbnNumber&#39; value =&#39;您选择了(a)Android   这是正确答案&#39; /&gt;(a)Android
输入类型=&#39; radio&#39;   命名=&#39; rbnNumber&#39; value =&#39;您选择了(a)CS是正确的   回答&#39; /&gt;(a)CS
输入类型=&#39; radio&#39;命名=&#39; rbnNumber&#39;值=&#39;您   选择(a)OS是正确答案&#39; /&gt;(a)OS
输入   类型=&#39;无线电&#39;命名=&#39; rbnNumber&#39; value =&#39;您选择了(a)代码   正确答案&#39; /&gt;(a)守则

     

变量应该动态初始化,结果是

     

输入类型=&#39;广播&#39;命名=&#39; rbnNumber&#39; value =&#39;您选择了(a)Android   这是正确答案&#39; /&gt;(a)Android
输入类型=&#39; radio&#39;   命名=&#39; rbnNumber&#39; value =&#39;您选择了(a)不正确的CS   回答&#39; /&gt;(a)CS
输入类型=&#39; radio&#39;命名=&#39; rbnNumber&#39;值=&#39;您   选择(a)不正确答案的操作系统&#39; /&gt;(a)OS
输入   类型=&#39;无线电&#39;命名=&#39; rbnNumber&#39; value =&#39;您选择了(a)代码   不正确答案&#39; /&gt;(a)守则

1 个答案:

答案 0 :(得分:1)

我已经对您的代码进行了折射和简化,并为您解决了问题。基本上,如果您只是阅读和书写行,您应该考虑使用Scanner / PrintStream而不是Reader / Writer,因为它们更方便。此外,当您发现自己重复代码时,应将其移动到单独的方法中,然后使用不同的参数调用该方法。

public class ReadOptionsFromUser {
    private static final Scanner INPUT = new Scanner(System.in);
    private static final String FILENAME = "F:/Android.txt";

    public static void main(String[] args) throws FileNotFoundException {
        try (PrintStream output = new PrintStream(FILENAME)) {
            readFromUser(output);
        }
    }

    public static void readFromUser(PrintStream output) {
        String wordA = readLine("Word (a)");
        String wordB = readLine("Word (b)");
        String wordC = readLine("Word (c)");
        String wordD = readLine("Word (d)");
        String answer = readLine("Correct letter");

        output.println(radio("a", wordA, answer));
        output.println(radio("b", wordB, answer));
        output.println(radio("c", wordC, answer));
        output.println(radio("d", wordD, answer));
    }

    private static String readLine(String prompt) {
        System.out.print(prompt + ": ");
        return INPUT.nextLine();
    }

    private static String radio(String letter, String word, String answer) {
        String option = "(" + letter + ") " + word;
        String is = letter.equals(answer) ? "is" : "is not";

        return "<input type='radio' name='rbnNumber' value='You selected " 
                + option + " which " + is +  " the correct answer' />"
                + option + "<br/>";
    }
}