从 myScanner 抓取多个数据

发布于 2024-12-11 04:00:33 字数 1334 浏览 0 评论 0原文

我试图同时抓取和存储 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救星 2024-12-18 04:00:33

获取 myScanner.nextLine() 行并将其保存到 coordiante1
使用 , 作为分隔符 (String#split) 拆分 coordiante1。 Cast(Integer#parseInt) &将第 0 个索引分配给 x1,将第 1 个索引分配给 y1

Get the line myScanner.nextLine() and save it to coordiante1.
Split the coordiante1 using , as delimiter (String#split). Cast(Integer#parseInt) & Assign 0th index to x1 and 1st index to y1.

听风吹 2024-12-18 04:00:33

我不太确定您希望输入是什么样的,但是有多种方法可以使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文