三个JAVA问题!!一定要能调试通过!!!
导入Java . awt . color;
导入Java . awt . dimension;
导入Java . awt . event . action event;
导入Java . awt . event . action listener;
导入Java . util . date;
导入Java . util . random;
导入javax . swing . jbutton;
导入javax . swing . jframe;
导入javax . swing . jlabel;
导入javax . swing . jpanel;
导入javax . swing . jtextfield;
公共类Main扩展JFrame {
private JTextField text = null
private JButton button guess = null;
private JButton button start = null;
私有JLabel标签= null
private int number = 0;
public Main() {
超级(“猜数字游戏”);
text = new JTextField(" ");
text.setPreferredSize(新维度(180,26));
text . set editable(false);
label = new JLabel();
label.setPreferredSize(新维度(300,26));
button Guess = new JButton(" Guess ");
button guess . set enabled(false);
button guess . addactionlistener(new action listener(){
public void action performed(action event e){
guess number();
}
});
button Start = new JButton(" Start ");
button start . addactionlistener(new action listener(){
public void action performed(action event e){
gameStart();
}
});
JPanel pane = new JPanel();
getContentPane()。添加(窗格);
pane.add(new JLabel("我有一个介于1和1000之间的数字,你能猜出我的数字吗?"));
pane.add(new JLabel("请输入你的第一个猜测:")));
pane.add(文本);
pane . add(button guess);
pane . add(button start);
pane.add(标签);
setDefaultCloseOperation(JFrame。EXIT _ ON _ CLOSE);
setSize(400,200);
setLocationRelativeTo(null);
set visible(true);
}
私有void gameStart(){
number = getRand();
text . set editable(true);
text . settext(" ");
button guess . set enabled(true);
}
私有void guessNumber(){
int num = 0;
string numStr = text . gettext();
尝试{
num = integer . parse int(numStr);
if(num & gt;1000 ||编号& lt1){
label.setText("介于1和1000之间");
返回;
}
如果(数字& ltnum){
label.setText("太高");
text . set background(color . red);
} else if(number & gt;num){
label.setText("太低");
text . set background(color . blue);
}否则{
label.setText("正确!");
text . set background(color . white);
text . set editable(false);
button guess . set enabled(false);
}
} catch(NumberFormatException e){
label.setText("错误,请输入数字!");
}
}
private int getRand(){
Random rand = new Random(new Date()。getTime());
返回rand . nextint(1000)+1;
}
公共静态void main(String[] args) {
new Main();
}
}
素数的判断
导入Java . util . scanner;
公共类Main {
public Main() {
Scanner scanner =新扫描仪(system . in);
//system.out.print("请输入整数:");
//int x = scanner . nextint();
///system . out . printf(" % d % s(" % d % s一个质数\n ",x,isPrime(x)?"是”:“否”);
System.out.print("请输入第一个整数:");
int x = scanner . nextint();
System.out.print("请输入第二个整数:");
int y = scanner . nextint();
布尔b = isPrime(x,y);
系统。out.printf (%s prime in "%d ~% d \n ",x,y,b?“存在”:“不存在”);
}
/**
*判断是否是素数
* @param要判断的数字
* @return true是质数。
*/
public boolean isPrime(int a){
for(int I = 2;我& lta-1;i++){
if(a % i == 0){
返回false
}
}
返回true
}
/**
*判断两个值之间是否有素数,并显示这些素数。
* @param起始数字(含)
* @param b结束号码(不包括)
* @return true表示有素数。
*/
public boolean isPrime(int a,int b){
布尔bo = false
for(int I = a;我& ltb;i++){
if(isPrime(i)){
System.out.printf("%d是质数\n ",I);
bo =真;
}
}
回博;
}
公共静态void main(String[] args) {
new Main();
}
}
学生成绩管理:
注意:时间有限,程序没有处理任何异常。
导入Java . util . ArrayList;
导入Java . util . list;
导入Java . util . scanner;
公共类Main {
个人分发名单& lt学生& gtlist = null
public Main() {
list = new ArrayList & lt学生& gt();
Scanner s =新扫描仪(system . in);
System.out.println("学生信息输入格式:学号、姓名、应用数学、大学英语、Java编程、计算机应用基础");
for(int I = 0;我& lt3;i++) {
System.out.printf("请输入%d学生的信息:",I+1);
学生student = structure student(s . next());
list.add(学生);
}
//测试数据
//Student Student 1 = structure Student(" 1,asd,54,65,76,87 ");
//Student Student 2 = structure Student(" 2,qwe,23,34,45,56 ");
//Student Student 3 = structure Student(" 3,zxc,56,67,78,89 ");
//list . add(student 1);
//list . add(student 2);
//list . add(student 3);
//计算平均分
averageScoreList();
//科目平均分
averageScoreSubject();
//输入姓名查询学生成绩,输入退出退出。
字符串名称= null
System.out.println("请输入要查询的学生姓名,输入exit退出查询:");
而(!(name = s.next())。equalsIgnoreCase("exit")){
student student = get student(name);
System.out.println(学生);
}
}
/**
*计算各科平均分。
*/
private void averageScoreSubject(){
int sum 1 = 0;
int sum 2 = 0;
int sum 3 = 0;
int sum 4 = 0;
int max 1 = 0;
int min 1 = 100;
int max 2 = 0;
int min 2 = 100;
int max 3 = 0;
int min 3 = 100;
int max 4 = 0;
int min 4 = 100;
对于(学生学生:列表){
sum 1+= student . get score _ 1();
sum 2+= student . get score _ 2();
sum 3+= student . get score _ 3();
sum 4+= student . get score _ 4();
if(student . get score _ 1()>;max1){
max 1 = student . get score _ 1();
}
if(student . get score _ 1()& lt;min1){
min 1 = student . get score _ 1();
}
if(student . get score _ 2()& gt;max2){
max 2 = student . get score _ 2();
}
if(student . get score _ 2()& lt;min2){
min 2 = student . get score _ 2();
}
if(student . get score _ 3()& gt;max3){
max 3 = student . get score _ 3();
}
if(student . get score _ 3()& lt;min3){
min 3 = student . get score _ 3();
}
if(student . get score _ 4()& gt;max4){
max 4 = student . get score _ 4();
}
if(student . get score _ 4()& lt;min4){
min 4 = student . get score _ 4();
}
}
System.out.printf("科目一平均分:%5.2f\t最高分:%d\t最低分:% d \ n ",sum 1/(float) list.size(),max 1,min 1);
System.out.printf("科目二平均分:% 5.2f \最高分:% d \ t最低分:% d \ n ",sum 2/(float) list.size(),最大2,最小2);
System.out.printf("科目三平均分:% 5.2f \最高分:% d \ t最低分:% d \ n ",sum 3/(float) list.size(),最大3,最小3);
System.out.printf("科目四平均分:% 5.2f \最高分:% d \ t最低分:% d \ n ",sum 4/(float) list.size(),最大4,最小4);
}
/**
*平均分列表显示
*/
私有void averageScoreList() {
列表& lt学生& gtl =新数组列表& lt学生& gt();
l . add(list . get(0));
for(int I = 1;我& ltlist . size();i++) {
学生s = list . get(I);
布尔b =真;
for(int j = 0;j & ltl . size();j++){
if(s . get average()>;l.get(j)。getAverage()){
l.add(j,s);
b =假;
打破;
}
}
如果(b){
l .添加;
}
}
System.out.println("计算平均分,按降序排列\ n学生编号\ tName \一级\ t二级\ t三级\ t四级\ t平均分数");
对于(学生学生:l) {
System.out.println(学生);
}
}
私有学生getStudent(字符串名称){
对于(学生:列表){
if(s.getName()。等于(姓名)){
返回s;
}
}
返回null
}
/**
*将字符串信息转换成学生对象。
* @param info输入的学生信息字符串。
* @返回学生对象
*/
私有学生结构学生(字符串信息){
学生学生= null
String [] arr = info.split("[,,;;]");
学生=新生(arr[0],arr[1]);
student . set score _ 1(integer . parse int(arr[2]);
student . set score _ 2(integer . parse int(arr[3]));
student . set score _ 3(integer . parse int(arr[4]);
student . set score _ 4(integer . parse int(arr[5]));
返校生;
}
公共静态void main(String[] args) {
new Main();
}
}
/**
*学生信息类
*/
班级学生{
私有字符串studentNO = null
私有字符串名称= null
private int score _ 1 = 0;
private int score _ 2 = 0;
private int score _ 3 = 0;
private int score _ 4 = 0;
私有浮动平均值= 0;
公共学生(){
this(null,null);
}
公共学生(字符串学生号,字符串名称){
this . student no = student no;
this.name = name
}
@覆盖
公共字符串toString() {
return student no+" \ t "+name+" \ t "+score _ 1+" \ t "+score _ 2+" \ t "+score _ 3+" \ t "+score _ 4+" \ t "+average;
}
公共浮动getAverage() {
平均值=(score _ 1+score _ 2+score _ 3+score _ 4)/4f;
回报平均值;
}
公共字符串getStudentNO() {
返回学生编号;
}
public void setStudentNO(String studentNO){
this . student no = student no;
}
公共字符串getName() {
返回名称;
}
public void setName(字符串名){
this.name = name
}
public int getScore_1() {
返回分数_ 1;
}
public void setScore _ 1(int score _ 1){
this . score _ 1 = score _ 1;
}
public int getScore_2() {
返回分数_ 2;
}
public void setScore _ 2(int score _ 2){
this.score _ 2 = score _ 2
}
public int getScore_3() {
返回分数_ 3;
}
public void setScore _ 3(int score _ 3){
this.score _ 3 = score _ 3
}
public int getScore_4() {
返回分数_ 4;
}
public void setScore _ 4(int score _ 4){
this.score _ 4 = score _ 4
}
}