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



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

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

你会得到这个:

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

请看-

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

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

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

你问的叫做串联。

作为基线,串联是指对字符串进行"添加"。

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变量已经是字符串,因此它只会将它们插入预先类型的字符串之间。不要忘记在字符串中包含空格(在"之间"之后,在"and"的两侧,在"is"之前。希望这有帮助!

添加以下行:

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

} }

最新更新