如何创建java代码以在(x,y)形式中输入x和y的值。

编程入门 行业动态 更新时间:2024-10-06 14:38:20
本文介绍了如何创建java代码以在(x,y)形式中输入x和y的值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在java中创建代码以输入(x,y)形式的坐标时遇到问题。 输出应该放在一个 - 维数组,如下所示: 强制扩展 __________ __________ X1 Y1 X2 Y2 X3 Y3 ect。 这是我的代码: 扫描仪扫描=新扫描仪(System.in); double [] [] arr = {}; //变量 String [] variables = new String [6]; System.out.println(输入因变量的名称:); 变量[0] = scan.nextLine(); System.out.println(输入独立变量的名称:); 变量[1] = scan.nextLine(); //没有。数据 System.out.println(输入数据点数:); int DataPoints = Integer.parseInt(scan.nextLine()); //创建一个字符串数组来存储元素 String Elements [] = new String [DataPoints]; System.out.println(输入+ DataPoints +元素来存储在数组中:); for(int i = 0; i< Elements.length; i ++){ System.out.print(输入元素+(i + 1)+格式为(x,y):); Elements [i] = scan.nextLine(); } //如果没有。数据点数为0或-ve for(int x = 0; x< 0; x ++){ System.out.println(输入无效,请再试一次); } //输出 System.out.println(); System.out.println(数组中的元素是:); System.out.println(); System.out.println(变量[1] +\ t\t\t+ variables [0]); System.out.println(__________+\t\t+ __________); for(int x = 0; x< 3; x ++){ System.out.println(Elements [x] +\t\t\t+ Elements [x]); } 我尝试了什么: 我已经尝试了上面提到的代码,但找不到将X和Y坐标分开的方法一维数组。

I am having trouble creating a code in java to input coordinates in the form (x, y). The output should then be put in a one-dimensional array, like below: Force Extension __________ __________ X1 Y1 X2 Y2 X3 Y3 ect. This is my code: Scanner scan = new Scanner(System.in); double [][] arr = {}; //Variables String[] variables = new String[6]; System.out.println("Enter the name of the dependant variable: "); variables[0] = scan.nextLine(); System.out.println("Enter the name of the independant variable: "); variables[1] = scan.nextLine(); //No. of Data System.out.println("Enter the number of data points: "); int DataPoints = Integer.parseInt(scan.nextLine()); //Create a string array to store elements String Elements[] = new String[DataPoints]; System.out.println("Enter the " + DataPoints + " elements to store in Array:"); for (int i = 0; i < Elements.length; i++) { System.out.print("Enter element " + (i+1) + " in the form (x,y): "); Elements[i] = scan.nextLine(); } // if no. of data points is 0 or -ve for (int x = 0; x < 0; x++){ System.out.println("Invalid input, please try again"); } //OUTPUT System.out.println(); System.out.println("Elements in Array are: "); System.out.println(); System.out.println( variables[1] + "\t\t\t" + variables[0]); System.out.println( "__________" + "\t\t" + "__________"); for (int x = 0; x < 3; x++){ System.out.println(Elements[x] + " \t\t\t" + Elements[x]); } What I have tried: I have tried the code as mentioned above but cannot find a way to separate the X and Y coordinates into separate one-dimensional arrays.

推荐答案

如果用户以x,y或(x,y)的形式输入两个值,则可以使用String.split或 String.split(正则表达式) [ ^ ]将两个元素分开。然后,您可以使用 Integer.parseInt(String s) [ ^ ]获取每个值。 If the user enters two values in the form x,y or (x,y) you can use String.split, or String.split(Regex)[^] to separate the two elements. You can then use Integer.parseInt(String s)[^] to get each value.

更多推荐

如何创建java代码以在(x,y)形式中输入x和y的值。

本文发布于:2023-10-10 18:15:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:形式   代码   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!