Java基础语法2

2023-03-09 16:43:55 浏览数 (1)

转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82800547 作者:Mercury_Lc SDUT Java基础语法练习2

I       C语言实验——打印菱形(SDUT 1174)

代码语言:javascript复制
import java.util.Scanner;   

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n;
		n = sc.nextInt();
		for(int i = 0; i < n; i   ) 
		{
			for(int j = 0; j < n - i - 1; j   )System.out.print(" ");
			for(int j = 0; j < i   1; j   )System.out.print('*');
			for(int j = 0; j < i; j   )System.out.print('*');
			System.out.println("");
		}
		for(int i = n - 2; i >= 0; i --)
		{
			for(int j = 0; j < n - i - 1; j   )System.out.print(" ");
			for(int j = i; j >= 0 ; j --)System.out.print('*');
			for(int j = i; j >= 1; j --)System.out.print('*');
			if(i != 0 )System.out.println("");
		}
	}
}

C语言实验——打印数字图形(SDUT 1179)

代码语言:javascript复制
import java.util.Scanner;   

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n;
		n = sc.nextInt();
		for(int i = 0; i < n; i   ) 
		{
			for(int j = 0; j < n - i - 1; j   )System.out.print(" ");
			for(int j = 0; j < i   1; j   )System.out.print(j   1);
			for(int j = 0; j < i; j   )System.out.print(i - j);
			System.out.println("");
		}
		for(int i = n - 2; i >= 0; i --)
		{
			for(int j = 0; j < n - i - 1; j   )System.out.print(" ");
			for(int j = i; j >= 0 ; j --)System.out.print(i - j   1);
			for(int j = i; j >= 1; j --)System.out.print(j);
			if(i != 0 )System.out.println("");
		}
	}
}

k     做乘法(SDUT 2249)

代码语言:javascript复制
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n;
		n = sc.nextInt();
		for (int i = 1; i <= n; i  ) {
			System.out.println(n   "*"   i   "="   n * i);
		}
	}
}

L   期末考试之分等级(SDUT 2251)

代码语言:javascript复制
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n, x;
		n = sc.nextInt();
		int a, b, c, d, e;
		a = b = c = d = e = 0;
		for (int i = 0; i < n; i  ) {
			x = sc.nextInt();
			if (x >= 90)a  ;
			else if (x < 90 && x >= 80)b  ;
			else if (x < 80 && x >= 70)c  ;
			else if (x < 70 && x >= 60)d  ;
			else if (x < 60) e  ;
		}
		System.out.print('A'   " "   a   "n"   'B'   " "   b   "n"   'C'   " "   c   "n"   'D'   " "   d   "n"   'E'
				  " "   e   "n");
	}
}

M     C语言实验——矩阵转置(SDUT 1164)

代码语言:javascript复制
import java.util.Scanner;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n;
		int a[][] = new int[1000][1000];
		n = sc.nextInt();
		for (int i = 0; i < n; i  ) {
			for (int j = 0; j < n; j  ) {
				a[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < n; i  ) {
			for (int j = 0; j < n; j  ) {
				if (j != n - 1)
					System.out.print(a[j][i]   " ");
				else
					System.out.print(a[j][i]);
			}
			System.out.println("");
		}
	}
}

N      C语言实验——矩阵下三角元素之和(SDUT 1172)

代码语言:javascript复制
package ll;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a[][] = new int[100][100];
		int n;
		n = sc.nextInt();
		int sum = 0;
		for (int i = 1; i <= n; i  ) {
			for (int j = 1; j <= n; j  ) {
				a[i][j] = sc.nextInt();
				if (i >= j)
					sum  = a[i][j];
			}
		}
		System.out.println(sum);
	}
}

0 人点赞