首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java中的基本输入和输出

Java中的基本输入和输出
EN

Stack Overflow用户
提问于 2013-11-03 23:31:48
回答 2查看 993关注 0票数 2

我使用的是JavaIDE7.4,我对NetBeans非常陌生,我试图让用户输入两个数字(它们都是双数),然后将它们存储在另一个变量中。我已经去了几十个教程网站,他们是不好的。如何获得基本的输入和输出?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-03 23:38:05

原始示例:

代码语言:javascript
复制
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);
    }

}

更新

抱歉,没说到重点,你想要打双打。给你:

代码语言:javascript
复制
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);
    }
}

建议阅读:

  • 官方的Oracle Java Tutorials --它信息丰富,易于理解
  • 课程:Basic I/O (来自上述教程)
票数 4
EN

Stack Overflow用户

发布于 2013-11-03 23:41:10

代码语言:javascript
复制
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.
}
}

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

代码语言:javascript
复制
System.out.println(input1); // This outputs the value of input1 on the screen.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19759535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档