如何在Java中显示这样的输出(条件:可输入的元素最多10个)(图像)

问题描述 投票:0回答:1

“在此处输入图像描述”

我可以使用inputmany()进行输入,但是当我为atomicNum输入0时,要让程序停止并运行outputmany()。但事实并非如此。

这是我的代码。某处是错误的,但我不知道。


package workshop3thu;

import java.util.Scanner;

public class Workshop3thu {

    public static void inputmany(Workshop3thu[] a, int n) {//n<= 10
        Scanner sc = new Scanner(System.in);
        int xatomicNum;
        String xsymbol;
        String xfullname;
        float xweight;
        int i = 0;
        while (i < 10) {
            System.out.print("Enter atomic number : ");
            xatomicNum = sc.nextInt();
            if (xatomicNum == 0) break;
            else {
                System.out.print("Enter symbol : ");
                xsymbol = sc.next();
                System.out.print("Enter fullname : ");
                xfullname = sc.next();
                System.out.print("Enter weight : ");
                xweight = sc.nextFloat();
            }


        }
    }

    public static void outputmany(Workshop3thu[] a, int n) {
        for(int i = 0; i <= n - 1; i++) {
            System.out.println(a[i]);
        }            
    }

    public static void main(String[] args) {
        Workshop3thu[] arr;
        Scanner sc = new Scanner(System.in);
        System.out.println("Atomic Information");
        System.out.println("================== ");
        arr = new Workshop3thu[10];
        Workshop3thu ob = new Workshop3thu();
        inputmany(arr, 10);
        outputmany(arr, 10);

    }

}
java output
1个回答
0
投票

我认为outputmany功能不起作用,因为参数arr从未设置为任何值。

© www.soinside.com 2019 - 2024. All rights reserved.