【问题标题】:my code cannot run because of this a[j]=sc.nextInt();我的代码因此无法运行 a[j]=sc.nextInt();
【发布时间】:2020-10-08 06:05:37
【问题描述】:

类 test10{

    static double displaySalesObject(String id , double amount) {
    return amount;
    }
    
     public static void main (String[] args) {

        sales s1 = new sales ();
        sales s2 = new sales ();
        
        System.out.print(s1.getsalesid() + "," + s1.getamount());
        System.out.print(s2.getsalesid() + "," + s2.getamount());
        s1.determineDiscount();
        s2.determineDiscount();
    //}

    //public static void main(String args[]) {
//  public void displaySalesArray(){        
    //Scanner sc=new Scanner(System.in);
    int[] a=new int[6];
    System.out.println("Please enter elements...");
    for(int j=0;j<6;j++)
    //a[j]=sc.nextInt();
    System.out.println("Array elements are : ");
    for (int i=0;i<a.length;i++)
        System.out.println(a[i]);
}

}

** 我尝试使用方法过去数组。这是正确的使用方法吗?并且在 a[j]=sc.nextInt(); 处也有错误**

【问题讨论】:

  • 你得到什么错误?

标签: java arrays methods


【解决方案1】:

这就是你要找的吗?

public class Test {

    static double displaySalesObject(String id, double amount) {
        return amount;
    }

    public static void main(String[] args) {

        sales s1 = new sales();
        sales s2 = new sales();

        System.out.print(s1.getSalesid() + "," + s1.getAmount());
        System.out.print(s2.getSalesid() + "," + s2.getSalesid());
        s1.determineDiscount();
        s2.determineDiscount();
        System.out.println("Please enter elements...");
        Scanner sc = new Scanner(System.in);
        int[] a = new int[6];
        passArrayToTheMethod(sc, a);
    }

    private static void passArrayToTheMethod(Scanner sc, int[] a) {
        for (int j = 0; j < 6; j++)
            a[j] = sc.nextInt();
        System.out.println("Array elements are : ");
        for (int i = 0; i < a.length; i++)
            System.out.println(a[i]);
    }

}

【讨论】:

    猜你喜欢
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    相关资源
    最近更新 更多