Упражнение A
public class InputBoxes {
public static void main(String[] args) {
String width = JOptionPane.showInputDialog("Ширина прямоугольника");
String height = JOptionPane.showInputDialog("Высота прямоугольника");
int area = Integer.parseInt(width) * Integer.parseInt(height);
JOptionPane.showMessageDialog(null, "Площадь прямоугольника равна " + area, "", JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
Упражнение B
public class InputBoxes {
public static void main(String[] args) {
String width = JOptionPane.showInputDialog("Ширина прямоугольника");
String height = JOptionPane.showInputDialog("Высота прямоугольника");
float area = Float.parseFloat(width) * Float.parseFloat(height);
JOptionPane.showMessageDialog(null, "Площадь прямоугольника равна =" + area, "", JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
Упражнение С
package conditionalconstructions;
import java.util.Scanner;
public class OperatorIf {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in, "Cp1251");
System.out.print("Введите свой возраст: ");
int age = user_input.nextInt();
switch (age) {
case 0: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:
System.out.println(age + " это до 10 лет");
break;
case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20:
System.out.println(age + " это между 11 и 20 годами");
break;
case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: case 30:
System.out.println(age + " это между 21 и 30 годами");
break;
default:
System.out.println("Вам более 30 лет");
}
}
}
Упражнение D
package conditionalconstructions;
import java.util.Scanner;
public class OperatorIf {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in, "Cp1251");
System.out.print("Выберите цвет: Черный, Белый, Красный, Синий ");
String colour = user_input.next();
if(colour.equals("Черный")) {
System.out.println("Должно быть, вы брюнет(ка)!");
}
else if(colour.equals("Белый")) {
System.out.println("Вы светлый человек");
}
else if(colour.equals("Красный")) {
System.out.println("Вы веселый и общительный");
}
else if(colour.equals("Синий")) {
System.out.println("Вы творческий человек");
}
else {
System.out.println("Извините, цвет непонятен");
}
}
}
Упражнение E
package loops;
import java.util.Scanner;
public class ForLoops {
public static void main(String[] args) {
int loopVal;
int end_value = 11;
int addition = 0;
int field_table = 0;
Scanner user_input = new Scanner(System.in, "Cp1251");
System.out.println("Сколько полей будет в таблице?");
field_table = user_input.nextInt();
for(loopVal = 1; loopVal < end_value; loopVal++) {
addition = loopVal * field_table;
System.out.println(loopVal + " поле из " + field_table + " = " + addition);
}
}
}
Упражнение F
package loops;
public class ForLoops {
public static void main(String[] args) {
int loopVal;
int end_value = 11;
int oddNum = 0;
for(loopVal = 1; loopVal < end_value; loopVal++) {
oddNum = loopVal%2;
if(oddNum == 1) {
System.out.println("Нечетное число " + loopVal);
}
}
}
}
Упражнение G
package arrays;
public class ArraysTest {
public static void main(String[] args) {
int[] aryNums = {24, 6, 47, 35, 2, 14};
int i;
int average;
int arrayTotal = 0;
for (i = 0; i < aryNums.length; i++) {
arrayTotal = arrayTotal + aryNums[i];
}
average = arrayTotal/aryNums.length;
System.out.println("Среднее арифметическое " + average);
}
}
Упражнение H
package arrays;
import java.util.Arrays;
public class ArraysTest {
public static void main(String[] args) {
int[] aryNums = {24, 6, 47, 35, 2, 14};
Arrays.sort(aryNums);
int lastArrayNumber = aryNums.length - 1;
System.out.println("Наибольшее число " + aryNums[lastArrayNumber]);
}
}
Упражнение I
package arrays;
public class ArraysTest {
public static void main(String[] args) {
int[] aryNums = {24, 6, 47, 35, 2, 14};
int i;
int oddNum = 0;
for(i=0; i < aryNums.length; i++) {
oddNum = aryNums[i]%2;
if(oddNum == 1) {
System.out.println("Нечетное число " + aryNums[i]);
}
}
}
}