2015-05-03 2 views
0

В настоящее время я боюсь одной вещи об этом задании. Я просто хотел бы сделать весь код короче, даже немного (особенно заявление if).Есть ли способ сделать весь этот код короче?

package Integers; 

import java.util.Scanner; 

public class SumOfInt { 
    public static void main(String[] args) { 
     int positive = 0; 

     System.out.println ("-Input ten non-zero integers to calculate their sum. " + "\n" + "-Input the integers at the console and press <Enter>" + "\n"); 

     System.out.println ("Input the 1st integer:"); 
     Scanner input = new Scanner (System.in); 
     int num1 = input.nextInt(); 

     System.out.println ("Input the 2nd integer:"); 
     int num2 = input.nextInt(); 

     System.out.println ("Input the 3rd integer:"); 
     int num3 = input.nextInt(); 

     System.out.println ("Input the 4th integer:"); 
     int num4 = input.nextInt(); 

     System.out.println ("Input the 5th integer:"); 
     int num5 = input.nextInt(); 

     { if (num1 > 0) 
       positive++; 
     } 
     { if (num2 > 0) 
       positive++; 
     } 
     { if (num3 > 0) 
       positive++; 
     } 
     { if (num4 > 0) 
       positive++; 
     } 
     { if (num5 > 0) 
       positive++; 
     } 

     System.out.println ("The number of positive integers are: " + positive); 
    } 
} 
+0

Имеет ли значение «ввести значение« целое »значение« целая », может быть более общим? – dbarnes

+0

Подробнее о циклах. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html –

+0

И узнайте о [массивах] (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) – CKing

ответ

3

Если вы нашли себя писать очень похожий код снова и снова (или даже скопировать склеивание материал), всегда есть способ обобщить код и вставить в цикл for или дополнительный метод. И это путь. В вашем случае вы можете просто использовать цикл for.

public static void main(String[] args) { 
    int positive = 0; 
    System.out.println("Input the integers at the console and press <Enter>"); 

    Scanner input = new Scanner (System.in); 
    for (int i = 1; i <= 5; i++) { 
    System.out.println("Input the " + i + "st integer:"); 
    int x = input.nextInt(); 
    if (x > 0) positive++; 
    } 
    input.close(); 
    System.out.println ("The number of positive integers are: " + positive); 
} 
1

Попробуйте

Scanner input = new Scanner(System.in); 

for (int i = 1; i <= 5; i++) { 
    String place = i + "th"; 
     if (i == 1) 
      place = "1st"; 
     if (i == 2) 
      place = "2nd"; 
     if (i == 3) 
      place = "3rd"; 
     System.out.println("Input the " + place + " integer:"); 
    if (input.nextInt() > 0) 
     positive++; 
} 

input.close(); 

System.out.println("The number of positive integers are: " + positive); 
+1

"Введите 2-е целое число"? – JClassic

+0

hrm, возможно 'int n = (i% 10);' и '(n == 1)? "st": (n == 2)? "nd": (n == 3)? «rd»: «th» ' – trooper

0

Если вы хотите сохранить переменную для последующего использования, вы можете использовать массив, чтобы сделать его проще.

Scanner input = new Scanner(System.in); 
int[] array = new int[5]; 
int positive = 0; 
for (int i = 0; i < 5; i++) { 
    array[i]=input.nextInt(); 
    if (array[i] > 0) 
     positive++; 
} 
System.out.println("Number of positive elements are"+ positive); 
Смежные вопросы