gpt4 book ai didi

Java Codelab 新行

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:52 24 4
gpt4 key购买 nike

如何使我的输出符合预期?为了更清楚地说明,如何使输出“It·will·take9.090909090909091E-4·秒”与其他所有内容在同一行而不是新行?

预期输出:

输入以下其中一项:·空气、·水或·钢:·输入声波传播的距离:··将花费·9.090909090909091E-4·秒。↵

实际输出:

输入下列之一:·空气、·水或·钢:·输入·距离·声波·意志·旅行:↵·它将·需要9.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.");

}
}
}

最佳答案

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

关于Java Codelab 新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35324715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com