[Java] Java practice questions Beginner

Continuing from the basics, it is the beginner’s edition.

Beginner

Write a program that inputs two integer values, displays the result of dividing the first value by the second value, and then displays the result of multiplying the result by the second value. All calculations should be done with integer type (if it is not divisible, the decimals are automatically truncated).

public static void question21(int num1, int num2) {
System.out.println("Q21");
int num3 = num1 / num2;
//Display the result of dividing the first value by the second value
System.out.println(num3);
// display the result of multiplying the result by the second value
System.out.println(num3 * num2);
}

Create a program that lets you enter an integer value and displays OK if the value is greater than 5 and less than 20.

*
* @param num1
*/
public static void question22(int num1) {
System.out.println("Q22");
// If the value is greater than 5 and less than 20
if (num1> 5 && num1 <20) {
// if the value is greater than 5 and less than 20
System.out.println("The first value is OK");
}
}

Create a program that lets you enter an integer value and displays OK if the value is less than -10 or greater than 10.

*
* @param num1
*/
public static void question23(int num1) {
System.out.println("Q23");
if (num1 <= -10 || num1 >= 10) {
// If the value is -10 or less, or 10 or more
System.out.println("The first value is OK");
}
}

Create a program that allows you to enter a numerical value and display OK if the value is -10 or more and less than 0, or 10 or more, otherwise NG.

*
* @param num1
*/
public static void question24(int num1) {
System.out.println("Q24");
// If the value is -10 or more and less than 0
if ((num1 >= -10 && num1 <0) || (num1 >= 10)) {
System.out.println("The first value is OK");
} else {
System.out.println("NG");
}
}

Create a program that allows you to enter an integer value and display range 1 if it is less than -10, range 2 if it is -10 or more and less than 0, and range 3 if it is 0 or more.

public static void question25(int num1) {
System.out.println("Q25");
// If the value is less than -10
if (num1 <-10) {
System.out.println("range 1");
// -10 or more and less than 0
} else if (num1 <0) {
System.out.println("range 2");
// 0 or more
} else {
System.out.println("range 3");
}
}

Use swicth-case statement to create a program that allows you to enter an integer value and displays one if the value is 1, two if it is 2, three if it is 3, and others otherwise.

*
* @param num1
*/
public static void question26(int num1) {
System.out.println("Q26");
// Entry example num1(1,2,3,0)
switch (num1) {
// Processing when the value of the expression matches the value num1
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
// Processing when the value of the expression does not match the value of any case
default:
System.out.println("others");
}
}

Create a program that allows you to enter an integer value and calculate and display the sum from 1 to that value. However, if you enter a value less than 0, it is displayed as 0.

*
* @param num1
*/

public static void question27(int num1) {
System.out.println("Q27");
for (int i = 1; i <num1; i++) {
System.out.println(i);
}
if (num1 == 0) {
System.out.println(0);
}
}

Write a program that lets you enter an integer and display the factorial of that value. However, if you enter a value less than 0, it will be displayed as 1. (Factorial method)

*
* @param num1
*/
public static void question28(int num1) {
for (int i = 1; i <= 10; i++) {
num1 = num1 * i;
}
System.out.println(num1);
}

Create an iterative program that lets you enter integer values five times and then print the sum of those values.

*
* @param num1
* @param num2
* @param num3
* @param num4
* @param num5
*/

public static void question29(int num1, int num2, int num3, int num4, int num5) {
System.out.println("Q29");
// display the sum of the values
System.out.println(num1 + num2 + num3 + num4 + num5);
}

Write a program that inputs an integer value and displays * for that number. If the input value is 0 or less, you do not need to write anything.

*
* @param num1
*/
public static void question30(int num1) {
System.out.println("Q30");
for (int i = 0; i <num1; i++) {
System.out.print("*");
}
}

Create a program that allows you to enter an integer value and display * for each number with 5 spaces.

* If the input value is 0 or less, you don't have to write anything.
*
* @param num1
*/
public static void question31(int num1) {
System.out.println("Q31");
for (int i = 1; i <num1; i++) {
System.out.print("*");
// Make every 5 * blank
if (i %5 == 0) {
System.out.print("\t");
}
}
}

Display from ###1 to 20 in order, but if it is a multiple of 5, create a program that displays bar instead of numbers.

public static void question32() {
System.out.println("Q32");
for (int i = 1; i <20; i++) {
// for multiples of 5
if (i %5 == 0) {
System.out.println("bar");
} else {
// when not a multiple of 5
System.out.println(i);
}
}
}

Create a program that allows you to enter an integer value and display anything from 1 to 9 other than the input value.

*
* @param num1
*/
public static void question33(int num1) {
System.out.println("Q33");
for (int i = 1; i <10; i++) {
// if i is not the value enteredif (!(i == num1)) {
System.out.println(i);
}
}
}

Create a program that inputs an integer value and displays the input value from 1 to 9 other than the input value +1. If the input value is 9, only 9 is not displayed.

*
* @param num1
*/
public static void question34(int num1) {
System.out.println("Q34");
// if the input value and the input value are not +1
for (int i = 1; i <10; i++) {
if ((!(i == num1)) && (!(i == (num1 + 1)))) (
System.out.println(i);
}
}
}

###{3, 7, 0, 8, 4, 1, 9, 6, 5, 2} is declared as an integer type array of size 10 and the integer number is entered and the element number is entered Create a program that displays the value of the array element that is the value. Checking whether the input value is outside the range of array elements may be omitted.

public static void question35(int num1) {
System.out.println("Q35");
// declare an integer array of size 10 initialized with {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}
int[] array = {3, 7, 0, 8, 4, 1, 9, 6, 5, 2};
System.out.println(array[num1]);
}

###{3, 7, 0, 8, 4, 1, 9, 6, 5, 2} declares an integer array of size 10 and initializes it with two integer values. Write a program that calculates and displays the product of the values of the two array elements whose input values are. Checking whether the input value is outside the range of array elements may be omitted.

*
* @param num1
*/
public static void question36(int num1, int num2) {
System.out.println("Q36");
// declare an integer array of size 10 initialized with {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}
int[] array = {3, 7, 0, 8, 4, 1, 9, 6, 5, 2};
System.out.println(array[num1] * array[num2]);
}

###{3, 7, 0, 8, 4, 1, 9, 6, 5, 2} is declared as an integer type array of size 10 and the integer number is entered and the element number is entered Create a program that refers to the value of the array element of the value and further refers to the value of the array element with the referenced value as the element number. Checking whether the input value is outside the range of array elements may be omitted.

public static void question37(int num1) {
System.out.println("Q37");
// declare an integer array of size 10 initialized with {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}
int[] array = {3, 7, 0, 8, 4, 1, 9, 6, 5, 2};
int num2;
num2 = array[num1];
System.out.println(array[num2]);
}

Declare an integer type array of size 10 initialized by ###{3, 7, 0, 8, 4, 1, 9, 6, 5, 2}, and initially set the reference element number to 0, The value of the array element of this referenced element number is displayed, then the value of that array element is set as the next referenced element number, and the value of the array element of this next referenced element number is displayed. Create a program that uses the value of as the element number to refer to next and repeats 10 times.

public static void question38() {
System.out.println("Q38");
// declare an integer array of size 10 initialized with {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}
int[] array = new int[]{3, 7, 0, 8, 4, 1, 9, 6, 5, 2};
// display the value of the array element whose element number is 0
int index = 0;
// repeat 10 times
for (int i = 0; i <10; i++) {
// Substitute the value of index into the element number index
index = array[index];
// Output the array element value when the element number is a
System.out.println(index);
}
}

Declare an integer array of size 10 initialized by ###{3, 7, 0, 8, 4, 1, 9, 6, 5, 2}, and initially set the referenced element number to 0. .. Displays the value obtained by subtracting the value of the array element of the next element number from the value of the array element of the referenced element number, and increments the referenced element number by 1. Create a program that repeats this procedure 9 times. )

public static void question39() {
System.out.println("Q39");
// declare an integer array of size 10 initialized with {3, 7, 0, 8, 4, 1, 9, 6, 5, 2}
int[] array = new int[]{3, 7, 0, 8, 4, 1, 9, 6, 5, 2};
// repeat 9 times
// current element number index
for( int index = 0; index <9; index++){
/**
* When index=0, currentValue=3,nextIndex=1,nextValue=7
* When index=1 currentValue=7,nextIndex=2,nextValue=0
* When index=2 currentValue=0,nextIndex=3,nextValue=8
* */
// array element value of the referenced element number currentValue
int currentValue = array[index];
// nextIndex the element number next to the current element number (index)
int nextIndex = index + 1;
// nextValue the value of the array element with the next element number
int nextValue = array[nextIndex];
// Value obtained by subtracting the value of the array element with the next element number from the value of the array element with the element number
System.out.println(currentValue-nextValue);
}
}
}

Tags:

Updated: