I started solving exercises to study Java. I'm sorry if I made a mistake.
package com.company; import java.util.Scanner;
public class HundredKnocksBasic { public static void main(String arg[]) { // question0(); // question1(); // question2(); // question3(); // question4(); // question5(); // question6(); // question7(); // question8(); // question9(); // question10(); // question11(); // question12(); // question13(); // question14(); // question15(); // question16(); // question17(); // question18(); // question19(); }
private static void question0() {
System.out.println("Question 0");
System.out.println("Hello World!");
}
private static void question1() {
System.out.println("Question 1");
int a = 12345;
int b = 23456;
System.out.println(a + b);
}
private static void question2() {
System.out.println("Question 2");
int a = 12345;
int b = 7;
System.out.println(a / b);
}
private static void question3() {
System.out.println("Question 3");
Scanner scanner = new Scanner(System.in); //Initialize the Scanner class Instantiate the Scanner class
//InputStream object, standard input(Input from the keyboard)
System.out.println("Please enter a number");
int num = scanner.nextInt(); //The part that receives input
scanner.close();
System.out.println(num);
}
private static void question4() {
System.out.println("Question 4");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number");
int num = scanner.nextInt(); //The part that receives input
scanner.close();
System.out.println(num * 3);
}
private static void question5() {
System.out.println("Question 5");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the first number");
int num1 = scanner.nextInt(); //The part that receives the first input
System.out.println("Please enter the second number");
int num2 = scanner.nextInt(); //The part that receives the second input
scanner.close();
System.out.println(num1 + num2);
System.out.println(num1 - num2);
System.out.println(num1 * num2);
System.out.println(num1 / num2);
}
private static void question6() {
System.out.println("Question 6");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number(0)");
int num = scanner.nextInt(); //The part that receives input
scanner.close();
if (num == 0) { //If the value entered is 0
System.out.println("zero");
}
}
private static void question7() {
System.out.println("Q7");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number(Other than 0)");
int num = scanner.nextInt();
scanner.close();
if (num == 0) { //If the value entered is 0
System.out.println("zero");
} else {
System.out.println("not zero");
}
}
private static void question8() {
System.out.println("Question 8");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number(Correct)");
int num = scanner.nextInt();
scanner.close();
if (num > 0) { //If the value is positive
System.out.println("positive");
}
}
private static void question9() {//Write with else if
System.out.println("Q9");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number");
int num = scanner.nextInt();
scanner.close();
if (num > 0) { //If the value is positive
System.out.println("positive");
} else if (num < 0) { //If the value is negative
System.out.println("negative");
} else { //If the value entered is 0
System.out.println("zero");
}
}
private static void question10() {
System.out.println("Q10");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number(Absolute value)");
int num = scanner.nextInt();
scanner.close();
###To get the absolute value, use the abs method of the Math class. Since the abs method is a static method, it can be called as it is.
num = Math.abs(num);
System.out.println(num);
}
private static void question11() {
System.out.println("Q11");
//for (Initialization formula;Conditional expression;Renewal formula) {
for (int i = 0; i < 10; i++) {
System.out.println("Hello World!");
}
}
private static void question12() {
System.out.println("Q12");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the number of repetitions");
int num = scanner.nextInt();
scanner.close();
//Repeat as many times as entered
for (int i = 0; i < num; i++) {
System.out.println("Hello World!");
}
}
private static void question13() {
System.out.println("Q13");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number");
int num = scanner.nextInt();
scanner.close();
//Execute up to the entered number
for (int i = 0; i < num; i++) {
System.out.println(i);
}
}
private static void question14() {
System.out.println("Q14");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number");
int num = scanner.nextInt();
scanner.close();
//Decrease by 1 from the entered number
for (int i = 0; i < num; num--) {
System.out.println(num);
}
}
private static void question15() {
System.out.println("Q15");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number");
int num = scanner.nextInt();
scanner.close();
//Increase by 2 up to the entered number
for (int i = 0; i < num; i += 2) {
System.out.println(i);
}
}
private static void question16() {//while statement
System.out.println("Q16");
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number");
int num = scanner.nextInt();
scanner.close();
while (num != 0) {
System.out.println("Please enter the number again");
num = scanner.nextInt();
//Stop if the input value is 0
}
scanner.close();
System.out.println("Finish");
}
private static void question17() {
System.out.println("Q17");
/*
Declaration of array and specification of number of elements
Type name Array variable name[] =new type name[Element count];
*/
int bar[] = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //Declare an array with 10 elements
/*
Type name Variable name=initial value;
The initial value of the i-th element is i
*/
for (int i = 0; i < bar.length; i++) {
//Use length to get the number of elements in an array bar[i]Index of i
System.out.println(bar[i]);
}
}
private static void question18() {
System.out.println("Q18");
Scanner scanner = new Scanner(System.in);
//Declare an array with 10 elements as a local variable
int[] a = new int[10];
System.out.println("Please enter a number");
//Have the local variable b enter a value
int b = scanner.nextInt();
//Describe the elements of the array to be assigned to the left side, and describe the value to be assigned to the right side.
//Array variable name[Sequence number] :=Value to substitute
//Substitute the value obtained in b for a
//Array length
//Repeat for the length of the array
for (int i = 0; i < a.length; i++) {
a[i] = b;
//Display array a
System.out.println(a[i]);
}
}
private static void question19() {
System.out.println("Q19");
Scanner sc = new Scanner(System.in);
//Declare an array with 10 elements
int bar[] = new int[10];
bar[0] = sc.nextInt();
bar[1] = sc.nextInt();
bar[2] = sc.nextInt();
bar[3] = sc.nextInt();
bar[4] = sc.nextInt();
bar[5] = sc.nextInt();
bar[6] = sc.nextInt();
bar[7] = sc.nextInt();
bar[8] = sc.nextInt();
bar[9] = sc.nextInt();
//Get the number of elements
for (int i = 0; i < bar.length; i++) {
System.out.println("Array" + i + "Second value" + bar[i]);
}
}
}
Recommended Posts