需要帮助检查Java中的if/else语句中的空格。

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

Need help checking for white-space in If/else statements in Java

问题

我刚开始学习Java,正在为我的在线课程项目而努力,并且目前卡在其中的一部分。

编写一个程序来检查给定变量名的合适性。更具体地说,您的程序应指示用户输入的变量名是否为:

  • 非法的(不允许有空格,必须以字母开头)
  • 合法,但使用不良风格(只能使用字母或数字)
  • 良好的

您不需要检查第一个字母是否为大写字母,例如在第二个单词、第三个单词等中。

所以我的问题是,由于变量名中有空格是非法的,我需要检查用户的输入是否含有空格,如果有空格,需要打印出非法。我还需要检查特殊符号(如$%#),如果它出现在除第一个字符以外的任何位置,需要打印出合法但不恰当。

我觉得这非常简单,我只是想不出来。

import java.util.Scanner;

public class IdentiferCheck {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        String variableName = "";
        char ch = ' '; // 临时变量

        // 获取用户输入
        System.out.println("此程序检查建议的Java变量名的适当性。");
        System.out.println("请输入一个变量名(输入 q 退出):");
        variableName = in.nextLine();

        // 检查变量名是否合适
        do {
            // 检查第一个字符是否为小写字母
            ch = variableName.charAt(0);

            if (Character.isLetter(ch) && Character.isLowerCase(ch)) {
                System.out.println("良好!");
            } else if (Character.isDigit(ch) && variableName.contains(" ")) {
                System.out.println("非法!");
            } else if (!Character.isLetterOrDigit(ch) || variableName.substring(1).contains("$") ||
                       variableName.substring(1).contains("%") || variableName.substring(1).contains("#")) {
                System.out.println("合法但不恰当!");
            }

            // 允许用户输入另一个变量名或退出
            System.out.println("输入另一个变量名或按 q 退出:");
            variableName = in.nextLine();
        } while (!variableName.equalsIgnoreCase("q"));
    }
}
英文:

I am new to learning java and I'm doing a project for my online class and currently stuck on part of it.

> Write a program that checks the properness of a given variable name. More specifically, your program should specify whether a user-entered variable name is:
> * illegal (no spaces allowed, must begin with a letter)
> * legal, but uses poor style (should only use letters or digits)
> * good
>
> You don’t need to check for an uppercase letter for the first letter in the second word, third word, etc.

So my problem is that since having space in a variable name is illegal, I need to check the user's input for space, and if there is one it needs to print that it is illegal. I also need to check for special symbols (like $%#) and if it is anywhere but the first character, have it print that it is legal but improper.

I feel like this is super simple I just can't figure it out.

import java.util.Scanner;
public class IdentiferCheck
{
public static void main (String[] args)
{
Scanner in = new Scanner (System.in);
String variableName = "";
char ch = ' '; //temp holder
//Get User Input
System.out.println("This program checks the properness of a proposed Java variable name.");
System.out.println("Please enter a variable name (q to quit):");
variableName = in.nextLine();
//Check if variable name is proper
do
{
//Check if first char is lowercase
ch = variableName.charAt(0);
if (Character.isLetter(ch) && Character.isLowerCase(ch))
{
System.out.println("Good!");
}
else if (Character.isDigit(ch) && Character.isUpperCase(ch) && variableName.contains(" "))
{
System.out.println("Illegal!");
}
//Allow user to input another name or quit
System.out.println("Enter another name or press q to quit: ");
variableName = in.nextLine();
} while (!variableName.equalsIgnoreCase("q"));
}
}

答案1

得分: 0

请确保阅读以下文档以完成您的任务。值得注意的是,这些要求实际上与Java变量名称的真实需求不匹配。然而,这个任务中最困难的部分是逻辑,而不是脚本。我已经为您编写了一个示例脚本:

后续字符可以是字母、数字、美元符号或下划线字符。 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

import java.util.Scanner;

public class identifierCheck {
    public static void main(String[] args) {
        String input = " ";
        System.out.printf("输入 q 退出...\n");

        while (input.charAt(0) != 'q' && input.length() == 1) // 注意长度,这样您可以使用 q 作为变量名称的开头
        {
            input = requestLine();

            if (checkValid(input)) {
                System.out.printf("%s 是合法的...\n", input);
            }
        }
    }

    public static String requestLine() {
        Scanner cmd = new Scanner(System.in);
        System.out.printf("输入一个变量名 > ");
        return cmd.nextLine();
    }

    public static boolean startsWithLetter(String input) {
        if (123 > (int) input.toLowerCase().charAt(0) && (int) input.toLowerCase().charAt(0) > 60) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean containsInvalid(String input) {
        if ((input.indexOf('$') != -1 || input.indexOf('_') != -1) && input.indexOf(' ') == -1) {
            System.out.printf("在变量名中使用 $ 和/或 _ 风格较差...\n");
        }
        if (input.indexOf(' ') != -1) // 字符串中有空格
        {
            return true;
        } else {
            return false;
        }
    }

    public static boolean checkValid(String input) {
        if (!startsWithLetter(input)) {
            System.out.printf("%s 非法,必须以字母开头(注意:$ 和 _ 可以作为变量名的合法开头)...\n", input);
            return false;
        } else if (containsInvalid(input)) {
            System.out.printf("%s 非法,不能包含空格...\n", input);
            return false;
        } else {
            return true;
        }
    }
}

祝您在课程中好运!Java 是学习的绝佳语言。

英文:

make sure to read the following documentation for your task. It's worth noting that the requirements don't actually match the real world requirements of Java variable names. However, the most difficult part of this task is the logic rather than the script. I've written an example script for you below:

> Subsequent characters may be letters, digits, dollar signs, or underscore characters. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

import java.util.Scanner;
public class identifierCheck
{
public static void main(String[] args) {
String input = " ";
System.out.printf("type q to exit...\n");
while (input.charAt(0) != 'q' && input.length() == 1) // Note the length so that you can use q for starting variable names
{
input = requestLine();
if (checkValid(input))
{
System.out.printf("%s is legal...\n", input);
}
}
}
public static String requestLine()
{
Scanner cmd = new Scanner(System.in);
System.out.printf("Enter a variable name > ");
return cmd.nextLine();
}
public static boolean startsWithLetter(String input)
{
if (123 > (int)input.toLowerCase().charAt(0) && (int)input.toLowerCase().charAt(0) > 60)
{
return true;
}
else
{
return false;
}
}
public static boolean containsInvalid(String input)
{
if ((input.indexOf('$') != -1 || input.indexOf('_') != -1) && input.indexOf(' ') == -1)
{
System.out.printf("Using $ and/or _ in variable names is poor style...\n");
}
if (input.indexOf(' ') != -1) // Has a space in the string
{
return true;
}
else
{
return false;
}
}
public static boolean checkValid(String input)
{
if (!startsWithLetter(input))
{
System.out.printf("%s is illegal, must start with a letter (note: $ and _ are 'valid' to start variable names)...\n", input);
return false;
}
else if (containsInvalid(input))
{
System.out.printf("%s is illegal, it must not contain spaces...\n", input);
return false;
}
else
{
return true;
}
}
}

Good luck with your course! Java's an excellent language to learn.

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

发表评论

匿名网友

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

确定