java – 在printin输入int
发布时间:2020-12-15 08:48:09 所属栏目:Java 来源:网络整理
导读:我正在尝试修复我写的脚本: 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 = sc
我正在尝试修复我写的脚本:
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(有点)预期…… 解决方法
试试这个:
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,y2
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Java TreeSet实现学生按年龄大小和姓名排序的方法示例
- Java Garbage Collection Basics--转载
- 我可以使用我的开源工具分发Azul Zulu JVM吗?
- Google App Engine如何预编译Java?
- java – On Event Dispatch Thread —想要离开它
- java – 尝试将XLSX数据导入R时出现IllegalArgumentExcepti
- SpringMVC架构的项目 js,css等静态文件导入有问题的解决方法
- Java 理论与实践: 非阻塞算法简介--转载
- java – stream和parallelStream
- 使用Java对象作为Clojure映射