从 myScanner 抓取多个数据
我试图同时抓取和存储 (coordinate1,x1,y1) 。
这是当有人输入 2,3 时我试图发生的情况:
坐标 = (2,3)
x1=2
y1=3
import java.util.Scanner;
public class LinearSlopeFinder {
public static void main(String[]args){
double x1, y1 ;
Scanner myScanner = new Scanner(System.in);
System.out.print(" What is the first set of cordinants? ... ");
String coordinate1 = myScanner.next();//this is the only thing
//it stores
x1= myScanner.nextInt();//need this from myScanner
y1= myScanner.nextInt();//need this from myScanner
System.out.println("your coordinates are " + coordinate1);
System.out.println("x1 is "+ x1 );
}
}
现在尝试分割但仍然收到错误
import java.util.Scanner;
public class LinearSlopeFinder {
public static void main(String[]args){
int x1, y1, x2, y2, n1, equation, slope ;
Scanner myScanner = new Scanner(System.in);
System.out.print(" What is the first set of cordinants? ... ");
String coordinate1 = myScanner.nextLine();
String coordinates[] = coordinate1.split(",");
x1 = coordinates[0];
y1 = coordinates[1];
System.out.println("your cordinants are " + cordinant1);
System.out.println("x1 is "+ x1 );
}
}
I am trying to grab and store (coordinate1,x1,y1) at the same time .
Here is what I am trying to have happen when someone enteres 2,3:
coordinate = (2,3)
x1=2
y1=3
import java.util.Scanner;
public class LinearSlopeFinder {
public static void main(String[]args){
double x1, y1 ;
Scanner myScanner = new Scanner(System.in);
System.out.print(" What is the first set of cordinants? ... ");
String coordinate1 = myScanner.next();//this is the only thing
//it stores
x1= myScanner.nextInt();//need this from myScanner
y1= myScanner.nextInt();//need this from myScanner
System.out.println("your coordinates are " + coordinate1);
System.out.println("x1 is "+ x1 );
}
}
now trying to split but still getting an error
import java.util.Scanner;
public class LinearSlopeFinder {
public static void main(String[]args){
int x1, y1, x2, y2, n1, equation, slope ;
Scanner myScanner = new Scanner(System.in);
System.out.print(" What is the first set of cordinants? ... ");
String coordinate1 = myScanner.nextLine();
String coordinates[] = coordinate1.split(",");
x1 = coordinates[0];
y1 = coordinates[1];
System.out.println("your cordinants are " + cordinant1);
System.out.println("x1 is "+ x1 );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获取
myScanner.nextLine()
行并将其保存到coordiante1
。使用
,
作为分隔符 (String#split
) 拆分coordiante1
。 Cast(Integer#parseInt
) &将第 0 个索引分配给x1
,将第 1 个索引分配给y1
。Get the line
myScanner.nextLine()
and save it tocoordiante1
.Split the
coordiante1
using,
as delimiter (String#split
). Cast(Integer#parseInt
) & Assign 0th index tox1
and 1st index toy1
.我不太确定您希望输入是什么样的,但是有多种方法可以使用 Scanner 类。查看 API 文档以更好地了解此类的工作原理: J2SE6 扫描仪 API 文档
I'm not exactly sure what you want your input to look like, but there are all kinds of ways to use the Scanner class. Check out the API documentation to get a better idea about how this class works: J2SE6 Scanner API Documentation