【问题标题】:How to get an input word in the output? Beginning Java如何在输出中获取输入单词?开始 Java
【发布时间】:2025-10-25 09:50:01
【问题描述】:

我正试图在我的输出中找到出发地,但我对如何做到这一点感到困惑。我必须在顶部的 String 方法中声明它吗?

    int seconds1;
    int seconds2;
    int minutes;
    int hours;

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter departure city: ");
    String departure = sc.nextLine();

    System.out.println("Enter arrival city ");
    String arrival = sc.nextLine();

    System.out.println("Enter time (in seconds) it takes to travel between: ");
    seconds1 = sc.nextInt();

    hours = (seconds1 % 86400) / 3600;
    minutes = ((seconds1 % 86400) % 3600) / 60;
    seconds2 = ((seconds1 % 86400) % 3600) % 60;

    // I am trying to get this to say "The time between + departure + and + arrival+is"
    System.out.println("The time to travel  between and  is "); 

    System.out.println(hours + " : " + minutes + " : " + seconds2);
  }
}

【问题讨论】:

  • 几乎和你在下一行所做的时间一样(小时、分钟、秒2)

标签: java string output


【解决方案1】:

你会得到这个:

System.out.println("The time to travel between " + departure + " and " + arrival + " is ");

【讨论】:

    【解决方案2】:

    请看-

    package com.jap.falcon;
    
    import java.util.Scanner;
    
    public class Problem2 {
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
    
            int seconds1;
            int seconds2;
            int minutes;
            int hours;
    
            System.out.println("Enter departure city: ");
            String departure = sc.nextLine();
    
            System.out.println("Enter arrival city ");
            String arrival = sc.nextLine();
    
            System.out.println("Enter time (in seconds) it takes to travel between: ");
            seconds1 = sc.nextInt();
    
            hours = (seconds1 % 86400) / 3600;
            minutes = ((seconds1 % 86400) % 3600) / 60;
            seconds2 = ((seconds1 % 86400) % 3600) % 60;
    
            System.out.println("The time to travel  between "+ departure +" and "+arrival+" is "); // I am trying to get this to say "The time between
                                                                        // + departure + and + arrival + is"
            System.out.println(hours + " : " + minutes + " : " + seconds2);
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以在System.out.println() 中指定departurearrival 变量,如下代码所示:

      System.out.println("The time to travel  between"+departure +" and"+arrival +"  is ");
      

      【讨论】:

        【解决方案4】:

        您要问的是所谓的串联。

        作为基线,连接是指对字符串进行“添加”。

        String a = "Hello, " + "World!";
        System.out.println(a);            // Prints "Hello, World!"
        

        这也可以在打印语句中完成。

        System.out.println("Hello, " + "World!");
        

        由于您的出发地点和目的地都已经是字符串的形式,它可能看起来像这样:

        System.out.println("The time between " + departure + " and " + arrival + " is:");
        

        对于 Java,您的 departurearrival 变量已经是字符串,因此它只会将它们插入到您预先输入的字符串之间。不要忘记在字符串中包含空格(在“between”之后、“and”的两侧以及“is”之前。希望这会有所帮助!

        【讨论】:

          【解决方案5】:

          添加这一行:

             System.out.println("The time between " + departure + " and " + arrival + " is");
          

          整个代码:

          import java.util.Scanner;
          public class Problem2 {
          public static void main( String [] args) {
          
          Scanner sc = new Scanner(System.in);
          
          int seconds1;
          int seconds2;
            int minutes;
            int hours;
          
          System.out.println("Enter departure city: ");
          String departure = sc.nextLine();
          
          System.out.println("Enter arrival city ");
          String arrival = sc.nextLine();
          
          System.out.println("Enter time (in seconds) it takes to travel between: ");
          seconds1 = sc.nextInt();
          
          hours = (seconds1 % 86400) / 3600;
          minutes = ((seconds1 % 86400) % 3600) / 60;
          seconds2 = ((seconds1 % 86400) % 3600) % 60;
          
               System.out.println("The time between " + departure + " and " + arrival + " is"); // I am trying to get this to say "The time between + departure + and + arrival + is"
          System.out.println(hours + " : " + minutes + " : " + seconds2);  
          
          
          } }
          

          【讨论】: