English 中文(简体)
Java方案要求获得10个数字,并将其放入一个阵列。 我如何要求投入? 我的法典是否正确?
原标题:Java program asking for 10 numbers and puts them in an array. How do I ask for input? Is my code correct so far?
  • 时间:2014-04-02 04:24:15
  •  标签:
  • java
  • arrays

该方案要求用户输入10个数字,并转换成int阵列。 如果阵列是 l(包含第7、13或18号)的话,则该阵列将列出阵列中所有数字的总和。 如果它不包含这些内容,那是假的,它只是打印所有数字的总和。

我如何正确要求这种投入? 我怎样获得阵列中哪怕数的总和? 是否有任何其他法典不正确?

import java.util.Scanner;

public class FunArrays {

public static void main(String[] args) {
    // until you do user input, you should test your methods using "test" as the input.
    int[] test = {1,2,3,4,5,6,7}; 
    luckyNumber1 = {7};
    luckyNumber2 = {13};
    luckyNumber3 = {18};


    int[] a=new int[9];
    Scanner sc=new Scanner(System.in);
        System.out.println("Please enter numbers...");
        for(int j=0;j==9;j++)
        a[j]=sc.nextInt();

}

public static int sum(int [ ] value) {
      int i, total = 0;
      for(i=0; i<10; i++)
      {
          total = total + value[ i ];
      }

      return (total);
 }
public static int sumOfEvens (
public static boolean isLucky (int[] array) {

    if ( (int == luckyNumber1) || (int == luckyNumber2) || (int == luckyNumber3 )
        return true; 

    else
        return false
}

// write the static methods isLucky, sum, and sumOfEvens

}
问题回答

页: 1 相反,它应当是尺寸的<代码>10<>。 因此, 1. 改变初始阶段

int[] a=new int[10];

使用模块操作器<代码>%,或在 Java其余操作者发现偶然或奇数。

    int evenSum = 0;
    for(int j=0;j<a.length;j++){
    if(a[j]%2 == 0){
       //even numbers
       evenSum = evenSum + a[j];
    }else{
       //odd numbers
      }
    }
    return evenSum;
  int[] a=new int[9];
    Scanner sc=new Scanner(System.in);
        System.out.println("Please enter numbers...");
        for(int j=0;j==9;j++)
        a[j]=sc.nextInt();

}

你们的 lo有点错,这应该是错的。

for (int j = 0; j < 9; j++)

通过这一固定办法,用户应当准确达到9人。 将阵列改为10个阵.,需要10个。

www.un.org/Depts/DGACM/index_spanish.htm 晚宴摘要

static int sumOfEvens(int array[]) {
    int sum = 0;
    for(int i = 0; i < array.length; i++>) {
        if(array[i] % 2 == 0)
            sum += array[i];
    }
    return sum;
}

<><>Input

for(int j = 0; j < a.length; j++)
    a[j] = sc.nextInt();
import java.util.Scanner;

public class MyMain1 {

    public static void main(String[] args) {
        // until you do user input, you should test your methods using "test" as
        // the input.
        int[] luckyNumbers = { 7, 13, 18 };

        int[] a = new int[10];
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter numbers...");
        for (int j = 0; j <= 9; j++) {
            a[j] = sc.nextInt();
        }
        sc.close();

        boolean sumAll = false;

        for (int i : a) {
            for (int j : luckyNumbers) {
                if (i == j) {
                    sumAll = true;
                    break;
                }
            }
            if(sumAll) {
                break;
            }
        }

        if (sumAll) {
            System.out.println("Summing all : " + sumAll(a));
        } else {
            System.out.println("Summing Only Even : " + sumEven(a));
        }

    }

    public static int sumAll(int[] value) {
        int total = 0;
        for (int j : value) {
            total = total + j;
        }
        return total;
    }

    public static int sumEven(int[] value) {

        int total = 0;
        for (int j : value) {
            if (j % 2 == 0) {
                total = total + j;
            }
        }
        return total;
    }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签