JAVA课后题6.13+要求键盘输入确定矩阵行列,然后输入对应矩阵元素,并存储到?

JAVA课后题6.13+要求键盘输入确定矩阵行列,然后输入对应矩阵元素,并存储到?,第1张

以下是一个Java程序,实现了根据用户输入确定矩阵的行和列,并且可以从键盘输入对应的矩阵元素,并将其存储到一个二维数组中。

import java.util.Scanner

public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in)

int rows, cols

// 获消亏高取矩阵的行和列

System.out.print("请输入矩阵的行数:")

rows = input.nextInt()

System.out.print("请输入矩阵的列数:")

cols = input.nextInt()

// 创建二维数组,并从键盘输入矩阵元素

int[][] matrix = new int[rows][cols]

System.out.println("请输入矩阵元素:")

for (int i = 0i <rowsi++) {

for (int j = 0j <colsj++) {

matrix[i][j] = input.nextInt()

}

}

// 输出矩阵

System.out.println("您输入的矩阵为:")

for (int i = 0i <rowsi++) {

for (int j = 0j <colsj++) {

System.out.print(matrix[i][j] + " ")

}

System.out.println()

}

}

}

以上代码中,首先使用Scanner类从键盘输入矩阵的行和列,然后创建一个二维数组matrix,大小为rows行cols列,并从键盘输入矩阵元素。最后,使用嵌套的for循环输出矩阵。

注意:在使拿尺用Scanner类从键盘输入矩阵元素时,应该保证输入的数据类型与数组元素的数空举据类型相同,否则可能会出现异常。例如,如果矩阵元素的数据类型为double,则应该使用input.nextDouble()方法来输入元素。

package test

public abstract class Person {

protected String name

public Person(String name) {

super()

this.name 埋旦敬= name

}

public abstract void showinfo()

public static 弯慎void main(String[] args) 迟芦{

Student stu = new Student("张三", 18, "车辆工程")

stu.showinfo()

}

}

class Student extends Person {

private int grade

private String specialty

public Student(String name, int grade, String specialty) {

super(name)

this.grade = grade

this.specialty = specialty

}

@Override

public void showinfo() {

System.out.println("姓名:" + name + "年龄:" + grade + "专业:" + specialty)

}

}

/*******************1017*******************/

public class S1017 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in)

int count = Integer.parseInt(in.nextLine().trim())

for (int i = 1i <= counti++) {

for (int j = 1j <= ij++) {

System.out.print(j + "*" + i + "=" + (i * j) + " ")

}

System.out.println()

}

}

}

/*******************1018*******************/

public class S1018 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in)

int count = Integer.parseInt(in.nextLine().trim()) + 1

int[][] yanghui = new int[count][count]

for (int i = 0i <counti++) {

for (int j = 0j <ij++) {

try {

yanghui[i][j] = yanghui[i - 1][j - 1] + yanghui[i - 1][j]

} catch (Throwable e) {

yanghui[i][j] = 1

}

System.out.print(yanghui[i][j] + " ")

}

System.out.println()

}

}

}

/*******************1019*******************/

public class S1019 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in)

String input = in.nextLine().trim()

String[] numStrs = input.split(" ")

int n = Integer.parseInt(numStrs[0])

int m = Integer.parseInt(numStrs[1])

List<Object>list = new ArrayList<Object>()

for (int i = 0i <ni++) {

list.add(i)

}

int count = 0

while (list.size() != 1) {

count = (count + m) % list.size()

list.remove(count)

count --

}

System.out.println(list.get(0))

}

}

/*******************1020*******************/

public class S1020 {

public static void main(String[] args) {

Set<Character>end = new HashSet<Character>()

int count = 10000

String str

char ch

for (int i = 1i <= counti++) {

str = String.valueOf(i)

ch = str.charAt(str.length() - 1)

if (end.contains(ch)) {

System.out.println(i)

} else if (isTongGou(i)) {

end.add(ch)

System.out.println(i)

}

}

}

public static boolean isTongGou(int num) {

String str = String.valueOf(num)

char ch = str.charAt(str.length() - 1)

String str2 = String.valueOf(num * num)

char ch2 = str2.charAt(str2.length() - 1)

return ch == ch2

}

}

/*******************1021*******************/

public class S1021 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in)

int count = Integer.parseInt(in.nextLine().trim())

int[] counts = new int[count]

for (int i = 0i <counts.lengthi++) {

counts[i] = Integer.parseInt(in.nextLine().trim())

}

for (int i = 0i <counts.lengthi++) {

print(counts[i])

System.out.println()

}

}

public static void print(int count) {

for (int i = 1i <= counti += 2) {

for (int j = 1j <= (5 - i) / 2j++) {

System.out.print("-")

}

for (int j = 0j <ij++) {

System.out.print("*")

}

for (int j = 1j <= (5 - i) / 2j++) {

System.out.print("-")

}

System.out.println()

}

for (int i = count - 2i >0i -= 2) {

for (int j = 1j <= (5 - i) / 2j++) {

System.out.print("-")

}

for (int j = 0j <ij++) {

System.out.print("*")

}

for (int j = 1j <= (5 - i) / 2j++) {

System.out.print("-")

}

System.out.println()

}

}

}

/*******************1022*******************/

public class S1022 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in)

int count = Integer.parseInt(in.nextLine().trim())

int[] counts = new int[count]

for (int i = 0i <counts.lengthi++) {

counts[i] = Integer.parseInt(in.nextLine().trim())

}

for (int i = 0i <counts.lengthi++) {

System.out.println(fibonacci(counts[i]))

}

}

public static int fibonacci(int n) {

int total = 0

int[] f = new int[n]

for (int i = 0i <f.lengthi++) {

if (i == 0) {

f[i] = 0

} else {

try {

f[i] = f[i - 2] + f[i - 1]

} catch (Throwable e) {

f[i] = 1

}

}

total += f[i]

}

return total

}

}

/*******************1023*******************/

public class S1023 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in)

String[] input

int maxCount = -1

int maxDay = 0

for (int i = 1i <= 7i++) {

input = in.nextLine().trim().split(" ")

int temp = Integer.parseInt(input[0]) + Integer.parseInt(input[1]) - 8

if (temp >0) {

if (temp >maxCount) {

maxCount = temp

maxDay = i

}

}

}

System.out.println(maxDay)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12319429.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存