Java中的基本输入和输出
我使用的是NetBeans IDE 7.4,我对Java非常陌生,我试图让用户输入两个数字(都是双精度的),然后将它们存储在另一个变量中。我去过几十个辅导网站,但都不好。如何获得基本的输入和输出 基本示例:Java中的基本输入和输出,java,io,Java,Io,我使用的是NetBeans IDE 7.4,我对Java非常陌生,我试图让用户输入两个数字(都是双精度的),然后将它们存储在另一个变量中。我去过几十个辅导网站,但都不好。如何获得基本的输入和输出 基本示例: import java.util.Scanner; public class TestIO { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
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);
}
}
建议阅读:
- 官方-它的信息量很大,很容易理解
- 课程:(来自上述教程)
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);
}
}
建议阅读:
- 官方-它的信息量很大,很容易理解
- 课程:(来自上述教程)
System.out.println(input1); // This outputs the value of input1 on the screen.
不太确定这是否是您的问题,因为输出值也可能与
System.out.println(input1); // This outputs the value of input1 on the screen.
double d1=double.parseDouble(文本)@是的,有很多变化。我刚刚抛出了一些基本示例)double d1=double.parseDouble(文本)@是的,有很多变化。我只是举了一些基本的例子