Как получить входное слово в выходной?Начало Java - PullRequest
0 голосов
/ 19 сентября 2018

Я пытаюсь найти место отправления в моем аут-ауте, но я не совсем понимаю, как это сделать.Должен ли я объявить это в методе 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);
  }
}

Ответы [ 5 ]

0 голосов
/ 19 сентября 2018

То, о чем вы спрашиваете, называется конкатенацией.

В качестве базовой линии конкатенация - это когда вы добавляете строки.

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, ваши departure и arrival переменные уже являются строками, поэтому он просто вставит их между предварительно набранными строками.Не забудьте включить в строки пробелы (после «между», по обе стороны от «и» и до «есть». Надеюсь, это поможет!

0 голосов
/ 19 сентября 2018

добавить эту строку:

   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);  


} }
0 голосов
/ 19 сентября 2018

Вы можете указать переменные departure и arrival в System.out.println(), как показано в приведенном ниже коде:

System.out.println("The time to travel  between"+departure +" and"+arrival +"  is ");
0 голосов
/ 19 сентября 2018

У вас будет это:

System.out.println("The time to travel between " + departure + " and " + arrival + " is ");
0 голосов
/ 19 сентября 2018

Пожалуйста, посмотрите -

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);
    }
}
...