如何让代码在用户未输入任何内容,只是按下回车键时作出响应?

huangapple go评论62阅读模式
英文:

How do get the code to respond if user dose not type anything and just presses enter?

问题

以下是您的代码的翻译部分:

import java.util.Scanner;

class Main {

    static Scanner name = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("请输入您的名字:");
        String a = name.nextLine();
        String b = a.substring(0, 1).toUpperCase() + a.substring(1);
        String c = a.trim();
        if (c.length() != 0) {
            if (a.contains(" ")) {
                System.out.println("您在名字中输入了空格。 : (");
            } else {
                System.out.println(b + " 是一个很好的名字! : )");
            }
        } else {
            System.out.println("这不是一个名字! : (");
        }
    }
}
英文:

I want my code to respond if the user only presses enter and doesn't type anything else. I use .trim to react to only putting in spaces but, it gives me an error when I just hit enter as the input. Here is what I have so far:


import java.util.Scanner;

class Main {

    static Scanner name = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("What is your first name?");
        String a = name.nextLine();
        String b = a.substring(0, 1).toUpperCase() + a.substring(1);
        String c = a.trim();
        if (c.length() != 0) {
            if (a.contains(" ")) {
                System.out.println("You put space in your name. : (");
            } else {
                System.out.println(b + " is a nice name! : )");
            }
        } else {
            System.out.println("Thats not a name! : (");
        }
    }
}

答案1

得分: 1

try (Scanner scan = new Scanner(System.in)) {
    System.out.println("What is your first name?");

    String str = scan.nextLine().trim();

    if (str.isEmpty())
        System.err.println("You entered an empty name");
    else if (str.contains(" "))
        System.err.println("You put space in your name.");
    else {
        String name = Character.toUpperCase(str.charAt(0)) + str.substring(1);
        System.out.println('\'' + name + "' is a nice name!");
    }
}
英文:
try (Scanner scan = new Scanner(System.in)) {
    System.out.println("What is your first name?");

    String str = scan.nextLine().trim();

    if (str.isEmpty())
        System.err.println("You entered an empty name");
    else if (str.contains(" "))
        System.err.println("You put space in your name.");
    else {
        String name = Character.toUpperCase(str.charAt(0)) + str.substring(1);
        System.out.println('\'' + name + "' is a nice name!");
    }
}

答案2

得分: 0

以下是翻译的内容:

我认为这可能是你想要的。此外,我还突出了一些最佳实践。

class Main {

    private static Scanner scanner = new Scanner(System.in); // 点0 - 使用访问修饰符!

    public static void main(String[] args) {

        System.out.println("What is your first name?");
        String name = ""; // 点1 - 初始化变量以避免空指针异常

        // 点2 - 始终检查是否有内容可从扫描仪读取
        if (scanner.hasNextLine()) {
            name = scanner.nextLine();
        }

        // 点3 - 您可以在原地使用方法,而不是使用堆栈变量
        if (!name.isEmpty()) {
            if (name.contains(" ")) {
                System.out.println("You put space in your name. : (");
            } else {
                // 点4 - 您可以省去额外的变量
                System.out.println(capitalize(name) + " is a nice name! : )");
            }
        } else {
            System.out.println("That's not a name! : (");
        }

        scanner.close(); // 点5 - 清理您的资源
    }

    // 点6 - 简洁的方法使代码更易读
    private static String capitalize(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
}

你可以查阅更多关于 Scanner 的信息。

英文:

I think this is probably what you want. Furthermore, I've also highlighted a few best practices.

class Main {

    private static Scanner scanner = new Scanner(System.in); // Point 0 - Use access modifiers!

    public static void main(String[] args) {

        System.out.println("What is your first name?");
        String name = ""; // Point 1 - Initialize your variables to avoid NullPointerException

        // Point 2 - Always check if there is something to read from scanner
        if (scanner.hasNextLine()) {
            name = scanner.nextLine();
        }

        // Point 3 - You can methods in place instead of stack variables
        if (!name.isEmpty()) {
            if(name.contains(" ")) {
                System.out.println("You put space in your name. : (");
            } else {
                // Point 4 - You can do away with extra variables
                System.out.println(capitalize(name) + " is a nice name! : )");
            }
        } else {
            System.out.println("That's not a name! : (");
        }

        scanner.close(); // Point 5 - Clean up your resources
    }

    // Point 6 - Concise methods makes code more readable
    private static String capitalize(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
}

You can look up more about Scanner.

huangapple
  • 本文由 发表于 2020年9月2日 02:03:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63693133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定