【问题标题】:Basic input and output in JavaJava中的基本输入和输出
【发布时间】:2013-11-03 23:31:48
【问题描述】:

我正在使用 NetBeans IDE 7.4,我对 Java 非常陌生,我试图让用户输入两个数字(它们都是双精度数),然后将它们存储在另一个变量中。我去过几十个教程网站,但它们都不好。如何获得基本的输入和输出?

【问题讨论】:

    标签: java io


    【解决方案1】:

    原始示例:

    import java.util.Scanner;
    
    public class TestIO {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("Print something:");    // printing output
            String text = scanner.nextLine();          // taking input
    
            System.out.println("You have printed the following text: " + text);
        }
    
    }
    


    更新
    对不起,错过了你想要双打的重点。给你:

    import java.util.Scanner;
    import java.util.InputMismatchException;
    
    public class TestIO {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            double firstDouble = 0L;
            double secondDouble = 0L;
            while (true) {  // take input (two doubles) until successful
                System.out.println("Print two doubles (press enter after each input):");
                try {
                    firstDouble = scanner.nextDouble();
                    secondDouble = scanner.nextDouble();
                } catch (InputMismatchException e) {
                    System.out.println("Wrong input. You must print doubles.\n" +
                                       "Depending on your locale, as a decimal separator " +
                                       "use either a comma or a dot.\n");
                    scanner.nextLine(); // clearing the buffer with the wrong input
                                        // to avoid infinite loop
                    continue;
                }
                break; 
            }
            // Printing the user input (limiting doubles to 3 decimal places)
            System.out.format("You have printed %.3f and %.3f %n", 
                              firstDouble, secondDouble);
        }
    }
    

    推荐阅读:

    【讨论】:

    • double d1 = Double.parseDouble(text);
    • @sgmart 是的,有很多变化。我只是抛出了一些基本的例子))
    【解决方案2】:
    import java.util.*;
    
    public class Test
    {
    static Scanner console = new Scanner (System.in)
    public static void main (String[] args)
    {
    double input1, input2; // Declares the double value of input1 and input2
    input1 = console.nextDouble(); // User inputs value into input1
    input2 = console.nextDouble();
    
    String value1 = "" + input1; // Storing value into a String
    String value2 = "" + input2;
    // If you want to store it in let's say string.
    // Or else i think / int value1 = Integer.parseInt(input1);
    // This changes the double value into an Int value
    // Something like that.
    }
    }
    

    不太确定这是否是您的问题,因为输出值也可能与

    System.out.println(input1); // This outputs the value of input1 on the screen.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 2022-09-30
      • 2015-01-06
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多