【问题标题】:Printing specific user input in java在java中打印特定的用户输入
【发布时间】:2015-12-28 20:30:14
【问题描述】:

我必须设计一个程序,在表单中获得 3 个用户输入:

Name1   (any number of spaces)  age1
Name2   (any number of spaces)  age3
Name3   (any number of spaces)  age3

然后打印年龄最大的行(假设 Name3 age3 的年龄最大,我会打印他的行)。

我的代码:

import java.util.*;
public class RankAge{
    public static void main(String[] args){
    System.out.println("Enter 3 different names and the age of each person respectively:");
    Scanner sc = new Scanner(System.in);
    String n1 = sc.nextLine();
    String n2 = sc.nextLine();
    String n3 = sc.nextLine();

    }
}

我知道如何扫描用户输入,但我不知道如何比较获取的字符串中的数字以打印特定的数字(也因为可以有任意数量的空格,所以看起来更多对我来说很复杂)。

【问题讨论】:

  • 您可以使用n1.split("\\s+")将字符串拆分为任意数量的空格。

标签: java string input integer compare


【解决方案1】:

您可以使用split 获取此人的年龄:

String age = "Jon       57".split("\\s+")[1]; // contains "57"

然后您可以使用Integer.parseInt(age) 以数字形式获取此人的年龄。


如果您需要允许用户输入带有空格的名称,您可以调整方括号中的数字([])。例如,[2] 将要求用户输入名字和姓氏。

【讨论】:

    【解决方案2】:

    在 frenchDolphin 的建议下设法做到了。这是我使用的代码(非常适合初学者):

    import java.util.*;
    public class RankAge{
        public static void main(String[] args){
        System.out.println("Enter 3 different names and the age of each person respectively:");
        Scanner sc = new Scanner(System.in);
        String n1 = sc.nextLine();
        String a1 = n1.split("\\s+")[1];
        String n2 = sc.nextLine();
        String a2 = n2.split("\\s+")[1];
        String n3 = sc.nextLine();
        String a3 = n3.split("\\s+")[1];
    
        if(Integer.parseInt(a1) > Integer.parseInt(a2)){
        } if(Integer.parseInt(a1) > Integer.parseInt(a3)){
                System.out.println(n1);
        }else if(Integer.parseInt(a2) > Integer.parseInt(a3)){
            System.out.println(n2);
        }else{
            System.out.println(n3);
        }
        }
    }
    

    【讨论】:

      【解决方案3】:

      +1 因为乍一看它看起来很简单,但是当我开始实施时,复杂性开始显现。这是您问题的完整解决方案,

      import java.util.*;
      
      public class RankAge {
      
         public static void main(String s[]){
              System.out.println("Enter 3 different names and the age of each person respectively:");
              Scanner sc = new Scanner(System.in);
              String n[] = new String[3];
              for(int i=0;i<3;i++){
                  n[i] = sc.nextLine();
              }
      
              int age[] = new int[3];
      
              age[0] = Integer.parseInt(n[0].split("\\s+")[1]);
              age[1] = Integer.parseInt(n[1].split("\\s+")[1]);
              age[2] = Integer.parseInt(n[2].split("\\s+")[1]);
              int ageTemp[] = age;
              for(int i=0;i<age.length;i++){
                  for(int j=i+1;j<age.length;j++){
                      int tempAge = 0;
                      String tempN = "";
                      if(age[i]<ageTemp[j]){
                          tempAge = age[i];
                          tempN = n[i];
                          age[i] = age[j];
                          n[i] = n[j];
                          age[j] = tempAge;
                          n[j] = tempN;
                      }
                  }
              }
      
              for(int i=0;i<3;i++){
                  System.out.println(n[i]);
              }
      
         }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-26
        • 2018-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多