Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 确定名称是短名称、平均长度名称还是长名称_Java_String_System.out - Fatal编程技术网

Java 确定名称是短名称、平均长度名称还是长名称

Java 确定名称是短名称、平均长度名称还是长名称,java,string,system.out,Java,String,System.out,我正在尝试写一个if/else语句,因为我认为这是我最好的方式。我需要它做的是确定名称是短名称、平均长度名称还是长名称。现在,13是平均长度,这意味着我需要如下编码,但我似乎无法让它工作 现在可以用了 System.out.println("Please Enter your first and last name"); String str = input.nextLine(); // here the code wil display the string's length an

我正在尝试写一个if/else语句,因为我认为这是我最好的方式。我需要它做的是确定名称是短名称、平均长度名称还是长名称。现在,13是平均长度,这意味着我需要如下编码,但我似乎无法让它工作

现在可以用了

      System.out.println("Please Enter your first and last name");
String str = input.nextLine();
// here the code wil display the string's length and its first character
System.out.println("The number of characters in your name is " + 
str.length());

  if(str.length() == 13)
System.out.println("Your name is average length.");
else if (str.length() > 13)
System.out.println("Your name is long length.");
else if (str.length() < 13)
System.out.println("Your name is short length.");

您对整体概念的想法是正确的,但正如Andreas指出的,您的代码中存在一些语法错误

Java还需要导入扫描仪来读取用户输入。我还调整了一行,从用户输入的总长度中减去一行,以补偿用户名中的空格

每个人都从某个地方开始。。。这是我第一次回复帖子!坚持下去

package javaapplicationName;
import java.util.Scanner;
public class JavaApplication41 {

    public static void main(String[] args) {
        // You'd want to grab the user's
        // input so we need to initialize a scanner object.
        Scanner input = new Scanner(System.in);
        System.out.println("Please Enter your first and last name");
        String str = input.nextLine();
        System.out.println("The number of characters in your name is " + (str.length() - 1));
        if (str.length() - 1 == 13) {
            System.out.println("Your name is average length.");
        } else if (str.length() - 1 > 13) {
            System.out.println("Your name is long length.");
        } else if (str.length() - 1 < 13) {
            System.out.println("Your name is short length.");
        }
    }
}

您忘了在最后一行编写System.out.println,我希望您的代码中单词character不在它自己的行中。另外,length是一种方法,所以它应该是str.lengthm,就像您前面几行所做的那样。我强烈建议使用IDE,这将极大地帮助您找出并修复语法错误。嗯,我使用Netbeans作为我的IDE,这只是我的第二个应用程序,我在这方面有点困难。我已经按照您的建议编辑了我的代码,但仍然无法正常工作NVM,非常感谢您的帮助!!!!你对代码的想法也奏效了。疯狂的是,编码中有这么多东西可以如此不同,但仍然可以工作。