STEP: 12 文字列の分割
問題
解答
step12.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
System.out.println(sc.next());
}
}
}
結果
STEP: 13 整数の足し算
問題
解答
step13.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(addUp( sc.nextInt(), sc.nextInt()));
}
/**
* 標準入力で得た引数の足し算をする関数
*
* @param a 足し算したい数字
* @param b 足し算したい数字
*
* @return 合計
*/
public static int addUp(int a, int b){
return a + b;
}
}
結果
STEP: 14 文字列の長さ
問題
解答
step14.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
System.out.println(getCharacterLength(s));
}
/**
* 文字の長さを取得する関数
*
* @param s 文字の長さを取得したい文字列
*
* @return 文字の長さ
*/
public static int getCharacterLength(String s){
int lenght = s.length();
return lenght;
}
}
結果
STEP: 15 文字列の1文字目
問題
解答
step15.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sc.useDelimiter("");
System.out.println(sc.next());
sc.close();
}
}
結果
STEP: 16 あいだの整数
問題
解答
step16.java
puts 'The best way to log and share programmers knowledge.'
結果
FINAL問題 文字列を切り取る
問題
解答
final.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
// 1行改行
sc.nextLine();
String s = sc.nextLine();
System.out.println(cutOutLetters(a, b, s));
sc.close();
}
/**
* a 文字目から b 文字目の範囲で文字列を切り取る関数
*
* @param a スタートの文字数
* @param b エンドの文字数
*
* @return 切り取った文字
*/
public static String cutOutLetters(int a, int b, String s) {
String clippingCharacter = null;
// インデックスは0から始まる為、-1からスタート
clippingCharacter = s.substring(a-1, b);
return clippingCharacter;
}
}