简体   繁体   English

为什么 Java 在让用户输入字符串的值之前强制我初始化我的字符串?

[英]Why is Java forcing me to initialize my String before letting the user input the value of the String?

I'm essentially making a small, simply "twenty questions" style program, using nested if-statements to try to guess what object the user is thinking based on clarifying questions.我基本上是在制作一个小型的、简单的“二十个问题”风格的程序,使用嵌套的 if 语句来尝试猜测用户在澄清问题的基础上在想什么 object。 I'm using the if statements to eventually give the user a "result" at the end, using a String variable called "result".我使用 if 语句最终在最后给用户一个“结果”,使用一个名为“result”的字符串变量。

My ultimate confusion lies in which the compiler is stating that " variable response may have not been initialized ".我最终的困惑在于编译器声明“ variable response may have not been initialized ”。 To which, based on the code, I would think it is after the if statements.根据代码,我认为在 if 语句之后。

import java.util.Scanner;

public class TwoQuestions {
  public static void main (String args[] ) {

    Scanner kb = new Scanner(System.in);

    String answer1, answer2, response;

    System.out.println("\n[Two Questions]\nThink of an object, and I'll try to guess it.");
    System.out.println("Is it an \"animal\", a \"vegetable\", or a \"mineral\"? (Type an answer exactly as quoted)");
    answer1 = kb.nextLine();
    System.out.println("Is it bigger than a breadbox? (yes/no)");
    answer2 = kb.nextLine();

    // example "response" based on user decisions
    if (answer1 == "animal" || answer1 == "Animal") {
      response = "yes";
      if (answer2 == "yes" || answer2 == "Yes") {
        response = "squirrel";
      }
    }
    // more if statements...

    // final machine "response" to user"
    // TODO: Figure out why *this* "response" requires initialization before if statements.
    System.out.println("My guess is that you are thinking of a " + response + ".\nI would ask you if I'm right, but I don't actually care.");

  }
}
  1. Like mad programmer said use String equals function to compare string.就像疯狂的程序员说的那样,使用 String 等于 function 来比较字符串。
  2. Yes it will need to be initialize before compilation can take place.是的,它需要在编译之前进行初始化。 If Im not wrong you can initialize with Null, or " ", empty string.如果我没记错,您可以使用 Null 或“”空字符串进行初始化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java控制台程序不让我输入字符串 - Java console program not letting me input string 为什么我的Java程序不能正常工作? 它不允许我输入字符串语句小数位数。 - Why isn't my java program working?? It doesn't allow me to input my string statement scale. 在Java中将用户输入存储为字符串 - Storing user input as a string in Java 我试图只从用户那里获取一个字符串,任何人都可以指导我为什么我的代码不起作用。 它甚至接受 integer 作为字符串值 - I am trying to get only a string from a user, can any one please guide me why my code doesn't work. it even accepts the integer as String value 为什么Java中的parseInt无法识别输入字符串中的整数? - Why is parseInt in java unable to identify integers in my input string? Java:如何初始化String []? - Java: how to initialize String[]? 在java中动态初始化字符串 - initialize string dynamically in java Java Scanner在字符串之前不接受整数输入 - Java Scanner not accepting integer input before string 尝试实现我的Tilemap时,Java向我抛出错误'对于输入字符串:“” - When trying to implement my Tilemap, Java is throwing me an error ' For input string: “” ' 我的扫描仪在运行几次后停止工作,并给我 java.util.InputMismatchException: For input string: "7642874781" - my scanner stopped working after a couple of runs and gives me the java.util.InputMismatchException: For input string: "7642874781"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM