gpt4 book ai didi

编写记录列表的Java应用程序

转载 作者:行者123 更新时间:2023-12-01 10:10:04 25 4
gpt4 key购买 nike

import java.util.Scanner;
import java.awt.Container;
importjavax.swing.*;
public class AM {
public static void main(String[] args)
{
String s0 = JOptionPane.showInputDialog( "no. of Students" );
int array [];
array = new int[s0];
Scanner s = new Scanner ( System.in);
String s1 = JOptionPane.showInputDialog( "Enter the Name of Student" );
String s2 = JOptionPane.showInputDialog("Enter the Exam Marks" );
String output = "Name of the Student\tExam Marks\n";
for ( int counter = 0; counter < array.length; counter++ )
output += counter + "\t" + array[ counter ] + "\n";
JTextArea outputArea = new JTextArea();
outputArea.setText( output);
JOptionPane.showMessageDialog( null, outputArea,
"Analysis of Exam Marks",
JOptionPane.INFORMATION_MESSAGE );
}
}

为什么array = new int[] <----不能把s0放进去以及如何使数组成为第一个。学生的?

当我使用array = new int [5];时结果:

no.__Marks

1_____0

2_____0

3_____0

4_____0

5_____0

如何让这个“0”成为考试分数?

最佳答案

Why the array = new int [ ] <----can not put s0 into here and how to make the array be the no. of the students??

如果您阅读过文档中的语法,您应该知道原因。您应该在方括号中放置一个整数值来分配数组的大小。那里不允许使用字符串。

array = new int[size];  //where size is int

您可以先将 String 解析为 int,并将其放在括号中:

int numStudents= Integer.parseInt(s0);
array = new int[numStudents];

HOW to make this "0" be the Exam Marks??

如果您想提示用户输入所有学生的分数,那么您需要将提示包含在循环中:

for(int x=0; x<numStudents; x++){
String name = JOptionPane.showInputDialog( "Enter the Name of Student" );
String str = JOptionPane.showInputDialog("Enter the Exam Marks" );
int marks = Integer.parseInt(str);
arrNames[x] = name;
arrMarks[x] = marks;
}
<小时/>

所以你的完整程序将如下所示:

public static void main(String[] args){
int numStudents = Integer.parseInt(JOptionPane.showInputDialog("Enter number of students."));

String[] names = new String[numStudents];
int[] marks= new int[numStudents];

//Populate student data
for(int x=0; x<numStudents; x++){
String name = JOptionPane.showInputDialog( "Enter the Name of Student" );
String str = JOptionPane.showInputDialog("Enter the Exam Marks" );
int marks = Integer.parseInt(str);
arrNames[x] = name;
arrMarks[x] = marks;
}

//Display student data
StringBuilder sb = new StringBuilder();
sb.append("Name of the Student\tExam Marks\n");
for(int x=0; x<numStudents; x++)
sb.append(names[x] + "\t" + marks[x] + "\n");

JTextArea outputArea = new JTextArea();
outputArea.setText(sb.toString());
JOptionPane.showMessageDialog( null, outputArea, "Analysis of Exam Marks", JOptionPane.INFORMATION_MESSAGE );
}

关于编写记录列表的Java应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36176213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com