扫描用户输入文本

时间:2014-01-17 19:02:51

标签: java string text user-input

我一直试图将用户输入作为文本,然后根据输入输出2个答案中的一个。这是我在Google的帮助下能够获得的目标:

import java.util.*;
public class kt_3_1 {
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String t;
    System.out.println("Enter the time of year");
    t = scanner.nextLine();
    if ( t.equals("summer") ) {
        System.out.printf("%n%s", t);
    }   else {
    System.out.println("FUUUUUUUu");
    }
}

}     

这个程序编译但总是返回“FUUUUUUUU”,即使我输入“夏天”。我做错了什么?

2 个答案:

答案 0 :(得分:1)

由于vaheaeg未在程序中的任何位置定义为变量,因此该程序甚至无法编译。尝试使用这个程序,它适合我。

import java.util.Scanner;

public class kt_3_1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String t;
        System.out.println("Enter the time of year");
        t = scanner.nextLine();
        if ( t.equals("summer") ) {
            System.out.printf("%n%s", t);
        }
        else {
            System.out.println("FUUUUUUUu");
        }
    }
}

答案 1 :(得分:0)

感谢您的帮助,对不起,我忘记更改1个变量。该计划现在有效。根据我的理解,即使我的原始程序编写正确,也可能无法正常工作,因为我将其保存为UTF-8。

相关问题