詳しくは公式マニュアルをお読みください。
Javaって、こんな風にも書ける、とか、こうとも言う、とか、色々有るので、
以下は雰囲気だけのざっくりとした説明です。
作成中です。ミス有ったら後免。
import java.util.Scanner; // For_Sample.java by Ryn 2016.10.15 public class For_Sample { public static void main(String[] args) { int total = 0; Scanner sc = new Scanner(System.in); for( int i = 0; i < 5; i++){ total += sc.nextInt(); } sc.close(); System.out.print("合計は " + total + "です。\n"); } }
import java.util.Scanner; // For_Sample.java by Ryn 2016.10.15 public class For_Sample { public static void main(String[] args) { int total = 0; Scanner sc = new Scanner(System.in); for( int i = 0; i < 5; i++) total += sc.nextInt(); sc.close(); System.out.print("合計は " + total + "です。\n"); } }
import java.util.Scanner; // While_Sample.java by Ryn 2016.10.15 public class While_Sample { public static void main(String[] args) { int total = 0, atai = 0; Scanner sc = new Scanner(System.in); while((atai = sc.nextInt()) != 0) { total += atai; } sc.close(); System.out.print("合計は " + total + "です。\n"); } }
import java.util.Scanner; // While_Sample.java by Ryn 2016.10.15 public class While_Sample { public static void main(String[] args) { int total = 0, atai = 0; Scanner sc = new Scanner(System.in); while((atai = sc.nextInt()) != 0) total += atai; sc.close(); System.out.print("合計は " + total + "です。\n"); } }
import java.util.Scanner; // Do_Sample.java by Ryn 2016.10.15 public class Do_Sample { public static void main(String[] args) { int total = 0, atai = 0; Scanner sc = new Scanner(System.in); do{ total += atai; }while(( atai = sc.nextInt()) != 0 ); sc.close(); System.out.print("合計は " + total + "です。\n"); } }
import java.util.Scanner; // Do_Sample.java by Ryn 2016.10.15 public class Do_Sample { public static void main(String[] args) { int total = 0, atai = 0; Scanner sc = new Scanner(System.in); do total += atai; while(( atai = sc.nextInt()) != 0 ); sc.close(); System.out.print("合計は " + total + "です。\n"); } }