Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Last active December 27, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/7358852 to your computer and use it in GitHub Desktop.
Save shigemk2/7358852 to your computer and use it in GitHub Desktop.
やさしいJava 7
class Sample1
{
public static void main(String[] args)
{
int[] test;
test = new int[5];
test[0] = 80;
test[1] = 60;
test[2] = 22;
test[3] = 50;
test[4] = 75;
for (int i=0; i<5; i++) {
System.out.println((i+1) + "番目の人の点数は" + test[i] + "です");
}
}
}
class Sample10
{
public static void main(String[] args)
{
int[][] test;
test = new int[2][5];
test[0][0] = 80;
test[0][1] = 60;
test[0][2] = 22;
test[0][3] = 50;
test[0][4] = 75;
test[1][0] = 90;
test[1][1] = 55;
test[1][2] = 68;
test[1][3] = 72;
test[1][4] = 58;
for (int i=0; i<5; i++) {
System.out.println((i+1)+"番目の人の国語の点数は"+test[0][i]+"です");
System.out.println((i+1)+"番目の人の算数の点数は"+test[1][i]+"です");
}
}
}
class Sample11
{
public static void main(String[] args)
{
int[][] test = {
{80,60,22,50},{90,55,68,72},{33,75,63}
};
for (int i=0; i<test.length; i++) {
System.out.println((i+1)+"番目の配列要素の長さは"+test[i].length+"です");
}
}
}
import java.io.*;
class Sample2
{
public static void main(String[] args) throws IOException
{
System.out.println("テストの受験者数を入力してください");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int num = Integer.parseInt(str);
int[] test;
test = new int[num];
System.out.println("人数分の点数を入力してください");
for (int i=0; i<num; i++) {
str = br.readLine();
int tmp = Integer.parseInt(str);
test[i] = tmp;
}
for (int i=0; i<num; i++) {
System.out.println((i+1) + "番目の人の点数は" + test[i] + "です");
}
}
}
class Sample3
{
public static void main(String[] args)
{
// 型名[] 配列変数名 = new 型名[要素数];
int[] test = new int[5];
test[0] = 60;
test[1] = 60;
test[2] = 22;
test[3] = 50;
test[4] = 75;
for (int i=0; i<5; i++) {
System.out.println((i+1)+"番目の人の点数は"+test[i]+"です");
}
}
}
// 配列初期化
class Sample4
{
public static void main(String[] args)
{
// 型名[] 配列変数名 = {値1,値2,値3,....}
int[] test = {80,60,22,50,75};
for (int i=0; i<5; i++) {
System.out.println((i+1)+"番目の人の点数は"+test[i]+"です");
}
}
}
class Sample5
{
public static void main(String[] args)
{
int[] test1;
test1 = new int[3];
System.out.println("test1を宣言しました。");
System.out.println("配列要素を確保しました。");
test1[0] = 80;
test1[1] = 60;
test1[2] = 22;
int[] test2;
System.out.println("test2を宣言しました");
test2 = test1;
System.out.println("test2にtest1を代入しました");
for (int i=0; i<3; i++) {
System.out.println("test1がさす"+(i+1)+"番目の人の点数は"+test1[i]+"です");
}
for (int i=0; i<3; i++) {
System.out.println("test2がさす"+(i+1)+"番目の人の点数は"+test2[i]+"です");
}
}
}
class Sample6
{
public static void main(String[] args)
{
int[] test1;
test1 = new int[3];
System.out.println("test1を宣言しました。");
System.out.println("配列要素を確保しました。");
test1[0] = 80;
test1[1] = 60;
test1[2] = 22;
int[] test2;
System.out.println("test2を宣言しました");
test2 = test1;
System.out.println("test2にtest1を代入しました");
for (int i=0; i<3; i++) {
System.out.println("test1がさす"+(i+1)+"番目の人の点数は"+test1[i]+"です");
}
test1[2] = 100;
System.out.println("test1がさす3番目の人の点数を変更します");
for (int i=0; i<3; i++) {
System.out.println("test1がさす"+(i+1)+"番目の人の点数は"+test1[i]+"です");
}
for (int i=0; i<3; i++) {
System.out.println("test2がさす"+(i+1)+"番目の人の点数は"+test2[i]+"です");
}
}
}
class Sample7
{
public static void main(String[] args)
{
int[] test = {80,60,22,50,75};
for (int i=0; i<5; i++) {
System.out.println((i+1)+"番目の人の点数は"+test[i]+"です");
}
System.out.println("テストの受験者は" + test.length + "人です");
}
}
class Sample8
{
public static void main(String[] args)
{
int[] test = {80,60,22,50,75};
for (int i=0; i<test.length; i++) {
System.out.println((i+1)+"番目の人の点数は"+test[i]+"です");
}
System.out.println("テストの受験者は" + test.length + "人です");
}
}
// たぶんバブルソート
import java.io.*;
class Sample9
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] test = new int[5];
System.out.println(test.length+"人の点数を入力してください");
for (int i=0; i<test.length; i++) {
String str = br.readLine();
test[i] = Integer.parseInt(str);
}
for (int s=0; s<test.length-1; s++) {
for (int t=s+1; t<test.length; t++) {
if(test[t] > test[s]) {
int tmp = test[t];
test[t] = test[s];
test[s] = tmp;
}
}
}
for (int j=0; j<test.length; j++) {
System.out.println((j+1)+"番目の人の点数は" + test[j] + "です");
}
}
}
class SampleP2
{
public static void main(String[] args)
{
int[] test;
test = new int[5];
test[0] = 80;
test[1] = 60;
test[2] = 22;
test[3] = 50;
test[4] = 75;
// 配列を後から追加できない
// test[5] = 100;
for (int i=0; i<5; i++) {
System.out.println((i+1)+"番目の人の点数は"+test[i]+"です");
}
}
}
class SampleP3
{
public static void main(String[] args)
{
int[] test = {80,60,22,50,75,100};
for (int i=0; i<6; i++) {
System.out.println((i+1)+"番目の人の点数は"+test[i]+"です");
}
}
}
// たぶんバブルソート
import java.io.*;
class SampleP4
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] test = new int[5];
System.out.println(test.length+"人の点数を入力してください");
for (int i=0; i<test.length; i++) {
String str = br.readLine();
test[i] = Integer.parseInt(str);
}
for (int s=0; s<test.length-1; s++) {
for (int t=s+1; t<test.length; t++) {
if(test[t] > test[s]) {
int tmp = test[t];
test[t] = test[s];
test[s] = tmp;
}
}
}
for (int j=0; j<test.length; j++) {
System.out.println((j+1)+"番目の人の点数は" + test[j] + "です");
}
System.out.println("最高点は" + test[0] + "点です");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment