混合使用if语句生成回复

时间:2015-10-06 03:47:53

标签: java eclipse

我正在做一些编程(自学),我一直在努力处理其他声明,但我修复了它。现在,当我运行它时,它运行错误的答复..如果这是有道理的。我希望我很清楚,你可以帮忙!w

rotate_deg()

2 个答案:

答案 0 :(得分:0)

您为if-else阻止提供了错误的大括号。请检查以下代码。

您可以使用equalsIgnoreCase()来区分用户输入的灵敏度。

import java.util.Scanner;
public class Test {

  private static Scanner scanner;

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    String answer;
    scanner = new Scanner(System.in);
    System.out.println("Hello Human");
    System.out.println("Do you want to build a snowman?");
    answer = scanner.next();
    if (answer.equalsIgnoreCase("Yes")) {
      System.out.println("Yay, Now you must think hard of what Olaf Looks like Okay? (say okay to coninue)");
    }
    else {
      System.out.println("Go away! I hate you"); //This happens when I try to say Yes.. This is meant for no.
    }
  }
}

PS:这里的if and else`块只有一个语句,如果你愿意,你可以完全删除它们。但是你有多个,那么你必须使用括号。

答案 1 :(得分:0)

大括号的位置:{是错误的。该消息不会落入if块内,并且始终会打印出来。它应该如下,

import java.util.Scanner;
public class Test {

private static Scanner scanner;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String answer;
    scanner = new Scanner(System.in); 

    System.out.println("Hello Human");
    System.out.println("Do you want to build a snowman?");
    answer = scanner.next();

    if (answer.equals("Yes")) {
    System.out.println("Yay, Now you must think hard of what Olaf Looks like Okay? (say okay to coninue)");
    { else
        System.out.println("Go away! I hate you"); //This happens when I try to say Yes.. This is meant for no.

    }
   }
}