简体   繁体   English

程序不会总是打印出最长的字符串

[英]Program won't always print out the longest string

What i'm trying to do below in the code, is get the user to enter two sepearate strings i know i have used first name and last name but just ignore that. 我在下面的代码中尝试做的事情是让用户输入两个独立的字符串,我知道我已经使用了名字和姓氏,但是忽略了它。 So the user enters two strings and it should print the longest string. 因此,用户输入两个字符串,它应该打印最长的字符串。 my program does not always do this. 我的程序并不总是这样做。 what do i need to change to make it work? 我需要进行哪些更改才能使其正常工作?

import java.util.Scanner;

 public class Q2
  {

    public static void main(String args [])
    {
    Scanner keyboardIn = new Scanner(System.in);
    String Fname;
    String Lname;

    System.out.print("Please enter first name: ");
    Fname=keyboardIn.nextLine();

    System.out.print("Please enter last name: ");
    Lname=keyboardIn.nextLine();

  if(Fname.compareTo(Lname) < 0)
  {
     System.out.println(Lname + " Is longest ");
  }
  else if(Fname.compareTo(Lname) > 0)
  {
     System.out.println(Fname + " Is longest ");
  }
 }
}

You should use the length 您应该使用长度

import java.util.Scanner;

public class Q2 {

    public static void main(String args[]) {
        Scanner keyboardIn = new Scanner(System.in);
        String Fname;
        String Lname;

        System.out.print("Please enter first name: ");
        Fname = keyboardIn.nextLine();

        System.out.print("Please enter last name: ");
        Lname = keyboardIn.nextLine();

        if (Lname.length() > Fname.length()) {
            System.out.println(Lname + " Is longest ");
        } else if (Fname.length() > Lname.length()) {
            System.out.println(Fname + " Is longest ");
        } else {
            // Both are of same length
        }
    }
}

this is wrong 这是错的

if(Fname.compareTo(Lname) < 0)
  {
     System.out.println(Lname + " Is longest ");
  }

strings have a length and that is what you need to use in order to compare first and lastname by checking the numbers of chars that are defining it 字符串的长度 ,这是你需要通过检查字符的被定义它的数字,以用来比较第一什么

why is this wrong 为什么这是错误的

Fname.compareTo(Lname)

because the result is not considering the number of chars on the string 因为结果没有考虑字符串上的字符数

You are comparing the Strings, not their lengths. 您正在比较字符串,而不是它们的长度。 Your code should be changed to: 您的代码应更改为:

  if(Fname.length() < LName.length())
  {
     System.out.println(Lname + " Is longest ");
  }
  else if(Fname.length() > LName.length())
  {
     System.out.println(Fname + " Is longest ");
  }

I would suggest to also add a case for equality, check if Fname.length() == LName.length() 我建议还添加一个相等的情况,检查Fname.length() == LName.length()

Java compareTo() method compares string lexicographically. Java compareTo()方法按字典顺序比较字符串。 I think a good way would be to use the length() method. 我认为一种好方法是使用length()方法。 See the code below 见下面的代码

if(Fname.length()<Lname.length())
{
   System.out.println(Lname + " Is longest ");
}
else 
{
   System.out.println(Fname + " Is longest ");
 }

I am not sure what you want to do when strings are equal length. 我不确定当字符串长度相等时您要做什么。 So, please make sure you check that case. 因此,请确保您检查该情况。

Instead of using compareTo method, I suggest using the length method. 建议不要使用compareTo方法,而建议使用length方法。 This allows you to compare string lengths directly and more easily. 这使您可以直接轻松地比较字符串长度。

Use the following statement as an example: 使用以下语句作为示例:

if (Fname.length() > Lname.length()){
  System.out.println(Fname + " Is longest ");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM