В настоящее время я боюсь одной вещи об этом задании. Я просто хотел бы сделать весь код короче, даже немного (особенно заявление 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);
}
}
Имеет ли значение «ввести значение« целое »значение« целая », может быть более общим? – dbarnes
Подробнее о циклах. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html –
И узнайте о [массивах] (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) – CKing