Java Codelab新行

时间:2016-02-10 19:57:51

标签: java

如何使输出像预期的那样?为了使它更清楚,我该如何使它输出“它·将·采取9.090909090909091E-4·秒”与其他所有内容在同一行而不是新行?

预期产出:

输入·一个··的所述·以下:·空气,水·,·或·钢:·输入··的距离··该声音·波·将·行程:··它··将采取·9.090909090909091 E-4·seconds.↵

实际输出:

输入·一个··的所述·以下:·空气,水·,·或·钢:·输入··的距离··该声音·波·将·行程:↵ ·它·意愿·take9.090909090909091E-4·秒。

特殊标志的传说: 符号含义 ·太空炭 ↵换行符(\ n) →标签(\ t) ←回车(\ r)

import java.util.Scanner; //Scanner for input
//Class name
public class TESTTEST
{
public static void main(String[] args)//main section
{
    //varibles
    String speed;
    double distance;
    double time;
    //set scanner
    Scanner keyboard = new Scanner(System.in);
    //ask for the type of speed input
    System.out.println("Enter one of the following: air, water, or steel: "
            +"Enter the distance the sound wave will travel:");
    speed = keyboard.nextLine();
    distance = keyboard.nextDouble();
    if (speed.equals("air") || speed.equals("water") || speed.equals("steel"))

     //set switch
     switch (speed)
     {

        case "air":
           time = distance/1100;
           System.out.print(" It will take" + time + " seconds.");
           break;
        case "water":
           time = distance/4900;
           System.out.print(" It will take" + time + " seconds.");
           break;
        case "steel":
           time = distance/16400;
           System.out.print(" It will take" + time + " seconds.");
           break;
         default:System.out.print("Sorry, you must enter air,water,or steel.");

}
}
}   

1 个答案:

答案 0 :(得分:0)

在这种情况下,即使System.out.print("Enter one of the following ...);也不起作用,因为您正在使用Scanner并期望来自用户的输入。输入输入后,用户需要按键盘的ENTER键将控制权移交给程序,这将在控制台中生成new line