首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在输出中获取输入词?开始Java

如何在输出中获取输入词?开始Java
EN

Stack Overflow用户
提问于 2018-09-20 03:02:18
回答 4查看 1.2K关注 0票数 -1

我试着在我的输出中找到出发的地方,但我不知道该怎么做。我必须在顶部的String方法中声明它吗?

代码语言:javascript
复制
    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);
  }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-09-20 03:11:15

你会得到这样的结果:

代码语言:javascript
复制
System.out.println("The time to travel between " + departure + " and " + arrival + " is ");
票数 0
EN

Stack Overflow用户

发布于 2018-09-20 03:10:12

请看一下-

代码语言:javascript
复制
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);
    }
}
票数 0
EN

Stack Overflow用户

发布于 2018-09-20 03:10:27

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

代码语言:javascript
复制
System.out.println("The time to travel  between"+departure +" and"+arrival +"  is ");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52412628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档