[Java] Scanner, ternary operator, array operation (AOJ ① large and small alignment)


I will study using the teaching materials of AIZU ONLINE JUDGE

What is AIZU ONLINE JUDGE?

An online judge is a service that allows you to challenge many exercises and score your code online. Test data is prepared for each question, and it will immediately judge the correctness of the submitted code and its efficiency.

How to receive input (Scanner)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        // String str = scan.nextLine();
        // System.out.println(str); //neko inu
        String str1 = scan.next(); 
        String str2 = scan.next(); 
        System.out.println(str1); //neko
        System.out.println(str2); //inu
        
        scan.close();
    }
}

Rectangle area and circumference

Create a program to find the area and circumference of a rectangle a cm long and b cm wide.

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        System.out.println( (a*b) + " " + (a*2+b*2) );
	//System.out.printf("%d %d\n",a*b,2*(a+b));
    }
}

clock

Since the time S in seconds is given, convert it to the format of h: m: s and output it. Where h is hours, m is minutes less than 60, and s is seconds less than 60. Output H, m, and s on one line separated by: (colon).

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
        int sec, min, hour;
        sec = sc.nextInt();
        hour = sec / 3600;
        min = (sec%3600) / 60;
        sec = sec % 60;
        System.out.println(hour+":"+min+":"+sec);
    }
}

Conditional operator (ternary operator)

public class Main {
    public static void main(String[] args) throws Exception{
    var age = 20;
    System.out.println(age >= 20 ? "grown up" : "children"); //grown up
    }
}

Large and small relationship

Create a program that reads two integers a and b and outputs the magnitude relationship between a and b.

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        String result = (a == b) ? "a == b" : (a > b) ? "a > b" : "a < b";
        System.out.println(result);
    }
}

Array manipulation

//Shallow copy
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    var  array1 = new String[] { "dog", "cat", "mouse", "fox", "lion" };
    //Sort array
    Arrays.sort(array1);
    //Stringize an array
    System.out.println(Arrays.toString(array1)); //[cat, dog, fox, lion, mouse]
    //Find values in a sorted array
    System.out.println(Arrays.binarySearch(array1, "mouse")); //4

    var array2 = new String[] { "Ah", "I", "U", "e", "O" };   
    //Array copy, length as argument, 0 shortage/Fill with null
    var array3 = Arrays.copyOf(array2, 2);
    System.out.println(Arrays.toString(array3)); //[Ah,I]

    //Copy the array by specifying the range with the argument
    var array4 = Arrays.copyOfRange(array2, 1, 7);
    System.out.println(Arrays.toString(array4)); //[I,U,e,O, null, null]

    //Set values in the array
    Arrays.fill(array4, 4, 6, "―");
    System.out.println(Arrays.toString(array4)); //[I,U,e,O, ―, ―]
  }
}
//Deep copy
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    var list1 = new StringBuilder[] {
        new StringBuilder("Doremifa Donuts"),
        new StringBuilder("ARAMA"),
        new StringBuilder("Hanihoheto")
      };

    var list2 = new StringBuilder[list1.length];
    for (var i = 0; i < list1.length; i++) {
      list2[i] = new StringBuilder(list1[i].toString());
    }

    list1[2].append("Hello");
    System.out.println(Arrays.toString(list1)); //[Doremifa Donuts, ARAMA,Hanihoheto Hello]
    System.out.println(Arrays.toString(list2)); //[Doremifa Donuts, ARAMA,Hanihoheto]
  }
}

Alignment of three numbers

Create a program that reads three integers and outputs them in ascending order.

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int num1 = scanner.nextInt();
		int num2 = scanner.nextInt();
		int num3 = scanner.nextInt();
		int[] nums = {num1,num2,num3};
		Arrays.sort(nums);
		
		System.out.println(String.format("%s %s %s", nums[0],nums[1],nums[2])); //46 50 80
		
	}
}

Recommended Posts

[Java] Scanner, ternary operator, array operation (AOJ ① large and small alignment)
Study Java # 2 (\ mark and operator)
Java basic learning content 3 (operator / ternary operator)
[Java] Output multidimensional array / spreadsheet (AOJ⑥ spreadsheet)
[Java beginner] == operator and equals method
[Java] Declare and initialize an array
[Java] Difference between array and ArrayList