扫描10个数,存入数组,然后反转数组
Scan 10 numbers, store them in an array and then reverse the array
我应该在 java 中编写一个程序,扫描 10 个双精度数字,然后将它们存储在一个数组中。然后应该将数字反转并打印出来。这就是我写的。程序以正确的顺序打印数组而不是颠倒顺序,我该如何解决?
public class ReverseNumbers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Fyll i egen kod
double[] a = new double[10];
for(int i = a.length - 1; i >=0; i--){
a[i] = scan.nextDouble();
System.out.print(" " + a[i]);
}
}
}
作为初学者,我不懂 Java 语言,但我可以使用 Qbasic 轻松解决您的问题。
CLS
N = 1
DO
INPUT "ENTER THE NUMBER"; A(N)
N = N + 1
LOOP WHILE N <= 10
CLS
DO
PRINT A(N)
N = N - 1
LOOP WHILE N > 0
END
您需要使用 Arrays.toString
或其他循环来打印数组。
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double[] a = new double[10];
System.out.println("Enter " + a.length + " numbers: ");
for (int i = a.length - 1; i >= 0; i--) {
a[i] = scan.nextDouble();
}
// Either print it like this
System.out.println(Arrays.toString(a));
// Or like this
for (double d : a) {
System.out.print(d + " ");
}
System.out.println();
// Or like this
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
样本运行:
Enter 10 numbers:
10 20 30 5 15 25 12 22 32 42
[42.0, 32.0, 22.0, 12.0, 25.0, 15.0, 5.0, 30.0, 20.0, 10.0]
42.0 32.0 22.0 12.0 25.0 15.0 5.0 30.0 20.0 10.0
42.0 32.0 22.0 12.0 25.0 15.0 5.0 30.0 20.0 10.0
这行代码有效:),谢谢大家!
public class ReverseNumbers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Fyll i egen kod
double[] a = new double[10];
for(int i=0; i<10; i++){
a[i] = scan.nextDouble();
}
for(int i = a.length - 1; i >=0; i--){
System.out.print(a[i] + " ");
}
}
}
我应该在 java 中编写一个程序,扫描 10 个双精度数字,然后将它们存储在一个数组中。然后应该将数字反转并打印出来。这就是我写的。程序以正确的顺序打印数组而不是颠倒顺序,我该如何解决?
public class ReverseNumbers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Fyll i egen kod
double[] a = new double[10];
for(int i = a.length - 1; i >=0; i--){
a[i] = scan.nextDouble();
System.out.print(" " + a[i]);
}
}
}
作为初学者,我不懂 Java 语言,但我可以使用 Qbasic 轻松解决您的问题。
CLS
N = 1
DO
INPUT "ENTER THE NUMBER"; A(N)
N = N + 1
LOOP WHILE N <= 10
CLS
DO
PRINT A(N)
N = N - 1
LOOP WHILE N > 0
END
您需要使用 Arrays.toString
或其他循环来打印数组。
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double[] a = new double[10];
System.out.println("Enter " + a.length + " numbers: ");
for (int i = a.length - 1; i >= 0; i--) {
a[i] = scan.nextDouble();
}
// Either print it like this
System.out.println(Arrays.toString(a));
// Or like this
for (double d : a) {
System.out.print(d + " ");
}
System.out.println();
// Or like this
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
样本运行:
Enter 10 numbers:
10 20 30 5 15 25 12 22 32 42
[42.0, 32.0, 22.0, 12.0, 25.0, 15.0, 5.0, 30.0, 20.0, 10.0]
42.0 32.0 22.0 12.0 25.0 15.0 5.0 30.0 20.0 10.0
42.0 32.0 22.0 12.0 25.0 15.0 5.0 30.0 20.0 10.0
这行代码有效:),谢谢大家!
public class ReverseNumbers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Fyll i egen kod
double[] a = new double[10];
for(int i=0; i<10; i++){
a[i] = scan.nextDouble();
}
for(int i = a.length - 1; i >=0; i--){
System.out.print(a[i] + " ");
}
}
}