为什么这不能打印所需的输出?

时间:2016-02-22 18:34:05

标签: java

这个java代码没有打印我想要打印的内容。它正在打印最后一行“上面的命令只能接受”

import java.util.Scanner;

public class Recap1 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Hi ! I am your dumb assistant, Dumbo");
        System.out.println("Tell me your name");
        String YourName = input.nextLine();
        System.out.println("these are a list of commands which can tell me what to do :-");
        String CurrentAffairs = "Tell me the current affairs";
        String Dinner = "Cook my dinner" ; 
        String Marriage ="Will you marry me ?";
        String Name = "What is my name ?";
        String gift = "Buy me a gift";
        System.out.println("Tell me the current affairs ");
        System.out.println("Cook my dinner ");
        System.out.println("Will you marry me ?");
        System.out.println("What is my name ?");
        System.out.println("Buy me a gift");
        System.out.println("now write a command !!!");
        String FirstCommand = input.nextLine();

        if (FirstCommand == CurrentAffairs)
            System.out.println("The Year is 2016" + "You are reading this" + "You are smiling " + "You are stupid");
        else if
            (FirstCommand == Dinner)
            System.out.println("I can only cook roasted human brain covered with melted intestines sprinkled with blood sauce.I need the nearest human availible to me.Will you volunter? ");
        else if
            (FirstCommand == Marriage)
            System.out.println("You are fine but I am afraid you are not of my type");
        else if 
            (FirstCommand == Name)
            System.out.println("Your name must be " + "  " + YourName);
        else if
            (FirstCommand == gift)
            System.out.println(" Give me some money and I will buy a gift for you.Deal ?");
        else
            System.out.println("Only the above commands are acceptable !!!!!!!!!!!!!!");
    }
}

4 个答案:

答案 0 :(得分:1)

是的,你必须使用.equals()

来比较字符串
if (FirstCommand.equals(CurrentAffairs))
{
    System.out.println( "The Year is 2016" + "You are reading this" + "You are smiling " + "You are stupid" );
}

并且你可能最好将整个if语句作为switch语句。

switch ( FirstCommand )
{
    case "Tell me the current affairs":
        System.out.println(The Year is 2016" + "You are reading this" + "You are smiling " + "You are stupid);
        break;
    default:
        System.out.println( "Only the above commands are acceptable !!!!!!!!!!!!!!" );
        break;
}

答案 1 :(得分:0)

你应该使用

 if (FirstCommand.equals( CurrentAffairs))

注意:比较两个字符串值需要使用equals()而不是==因为==比较引用值而不是内容

答案 2 :(得分:0)

试试这个工作正常。

public class Recap1 {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Hi ! I am your dumb assistant, Dumbo");
    System.out.println("Tell me your name");
    String YourName = input.nextLine();
    System.out.println("these are a list of commands which can tell me what to do :-");
    String CurrentAffairs = "Tell me the current affairs";
    String Dinner = "Cook my dinner" ; 
    String Marriage ="Will you marry me ?";
    String Name = "What is my name ?";
    String gift = "Buy me a gift";
    System.out.println("Tell me the current affairs ");
    System.out.println("Cook my dinner ");
    System.out.println("Will you marry me ?");
    System.out.println("What is my name ?");
    System.out.println("Buy me a gift");
    System.out.println("now write a command !!!");
    String FirstCommand = input.nextLine();
    if (FirstCommand.equalsIgnoreCase(CurrentAffairs))
        System.out.println("The Year is 2016" + "You are reading this" + "You are smiling " + "You are stupid");
    else if (FirstCommand.equalsIgnoreCase(Dinner))
        System.out.println("I can only cook roasted human brain covered with melted intestines sprinkled with blood sauce.I need the nearest human availible to me.Will you volunter? ");
    else if (FirstCommand.equalsIgnoreCase(Marriage))
        System.out.println("You are fine but I am afraid you are not of my type");
    else if(FirstCommand.equalsIgnoreCase(Name))
        System.out.println("Your name must be " + "  " + YourName);
    else if (FirstCommand.equalsIgnoreCase(gift))
        System.out.println(" Give me some money and I will buy a gift for you.Deal ?");
    else
        System.out.println("Only the above commands are acceptable !!!!!!!!!!!!!!");
}

}

答案 3 :(得分:0)

这是完成您要做的事情的有效方式。不使用字符串作为输入,而是取整数。使这简单,没有错误。

import java.util.Scanner;

public class Recap1 {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Hi ! I am your dumb assistant, Dumbo");
    System.out.println("Tell me your name");
    String YourName = input.nextLine();
    System.out.println("these are a list of commands which can tell me what to do :-");
    String[] replies = {"The Year is 2016" + "You are reading this" + "You are smiling " + "You are stupid"
    ,"I can only cook roasted human brain covered with melted intestines sprinkled with blood sauce.I need the nearest human availible to me.Will you volunter? ",
    "You are fine but I am afraid you are not of my type","Your name must be ",
    " Give me some money and I will buy a gift for you.Deal ?"};


    System.out.println("1.Tell me the current affairs ");
    System.out.println("2.Cook my dinner ");
    System.out.println("3.Will you marry me ?");
    System.out.println("4.What is my name ?");
    System.out.println("5.Buy me a gift");
    System.out.println("now write a command !!!");
    int FirstCommand = input.nextInt();

   switch(FirstCommand)
   {
    case 1:
    case 2:
    case 3:
    case 5: System.out.println(replies[FirstCommand-1]);
    break;
    case 4 : System.out.println(replies[FirstCommand-1]+" "+YourName);
    break;
    default:
        System.out.println("Only the above commands are acceptable !!!!!!!!!!!!!!");
        break;
   }
}
}