java程序printIn_Java 在printin输入int

我正在尝试修复我写的脚本:

import java.util.Scanner;

public class Line2

{

public static void main (String [] args)

{

Scanner scan = new Scanner (System.in);

System.out.println ("Please enter 4 integers");

int x1 = scan.nextInt();

int y1 = scan.nextInt();

int x2 = scan.nextInt ();

int y2 = scan.nextInt ();

double distance;

//Asking the user to insert coordinates of both points and setting double

// on distance in order to properly calculate the square root

distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

System.out.print( "the length of the line between the points" (int x1, int x2) "and" (int y1, int y2) "is" distance);

//Telling the program to calculate the distance of two points given by user

}//end of method main

}

我试图让x1 x2 y1和y2出现在里面,但它不允许我 – 给Y(有点)预期……

无论int是什么,我能做什么才能使它出现? (除了那个程序运行得很好,我认为..)

谢谢

试试这个:

System.out.printf(

"The length of the line between the points (%d, %d) and (%d, %d) is %f%n",

x1, x2, y1, y2, distance);

这些情况最简单的解决方案是使用格式化字符串,如上面的代码所示.查看documentation中字符串等语法的更多详细信息.

在上面的代码片段中,%之后的每个字符表示必须相应地格式化该位置中的相应参数(在字符串之后,从左到右的顺序).特别是:

>%d:这将是一个整数.前四个参数是整数x1,y1,x2,y2

>%f:这将是一个十进制数字.第五个参数是双倍称为距离

>%n:这将是一个特定于平台的新行字符

printf方法负责用相应的参数值替换每个格式字符,按预期创建和打印字符串.这比将字符串部分与运算符连接起来更容易,更不容易出错,散布所需的变量.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值