用JAVA语言编写选班长程序

用JAVA语言编写选班长程序,第1张

package com.nanky.choice

import java.util.Scanner

public class Test { private static int num = 10// 班里一共多少人,除候选人外 private static int men = 4// 候选人人数 private static int count = 0// 已经输入候选人的数目 private static boolean flag = false// 判断是否成功输入和成功投票

public static void main(String[] args) { Leader[] leader = new Leader[men] for (int i = 1i <= men) { System.out.println("请输入第" + i + "个候选人信息!") System.out.print("请输入姓名:") Scanner sca = new Scanner(System.in) String name = sca.next() System.out.println() System.out.print("请输入候选人编号:") Scanner sc = new Scanner(System.in) int id try { id = sc.nextInt() for (int j = 0j <countj++) {// 判断候选人编号是否重复 if (id == leader[count].getId()) { System.out.println("\n该编号已经输入了,请重新输入信息!") flag = true break } } if (flag == false) { Leader le = new Leader(id, name) leader[i - 1] = le System.out.println("\n输入完毕!") i++ } else { flag = false } } catch (Exception e) { System.out.println("输入格式不正确!") } } for (int i = 1i <= meni++) { System.out.println("第" + i + "候选人,编号:" + leader[i - 1].getId() + " 姓名:" + leader[i - 1].getName()) } for (int i = 1i <= numi++) { System.out.println("请输入候选人的编号:") Scanner sca = new Scanner(System.in) int id try { id = sca.nextInt() for (int j = 0j <menj++) { if (id == leader[j].getId()) { leader[j].setCount(leader[j].getCount() + 1) flag = true } } if (flag) { System.out.println("投票成功!") flag = false } else { System.out.println("投票失败!") } } catch (Exception e) { System.out.println("输入格式不正确") } } for (int i = 1i <leader.lengthi++) {// 排下序 for (int j = 0j <leader.length - ij++) { if (leader[j].getCount() >leader[j + 1].getCount()) { Leader temp = leader[j] leader[j] = leader[j + 1] leader[j + 1] = temp } } } for (int i = 0i <leader.lengthi++) { System.out.println(leader[i].getId() + " " + leader[i].getName() + " " + leader[i].getCount()) } System.out.println("班长是:" + leader[leader.length - 1].getName()) }

}

import java.io.BufferedReader

import java.io.InputStreamReader

public class Vote {

public static void main(String args[]) {

try {

String[] persons = new String[]{"1","2","3"}

Integer[] votes = new Integer[]{0,0,0}

BufferedReader sin = new BufferedReader(new InputStreamReader(

System.in))

String readline

System.out.println("================投票开始===============")

System.out.println("请按1,2,3投票,Enter确认")

System.out.println("按0-->Enter结束投票,公布结果\n")

readline = sin.readLine()// 从系统标准输入读入一字符串

while (!readline.equals("0")) {

String voteStr = readline

if(persons[0].equals(voteStr)){

votes[0] +=1

}else if(persons[1].equals(voteStr)){

votes[1] +=1

}else if(persons[2].equals(voteStr)){

votes[2] +=1

}else{

System.out.println("无效的投票!")

}

readline = sin.readLine()// 从系统标准输入读入一字符串

} // 继续循环

System.out.println("================投票结束===============")

System.out.println("================结果公布===============")

System.out.println(persons[0]+"================"+persons[1]+"==============="+persons[2])

System.out.println(votes[0]+"================"+votes[1]+"==============="+votes[2])

} catch (Exception e) {

System.out.println("Error" + e)// 出错,则打印出错信息

}

}

}


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

原文地址: http://outofmemory.cn/yw/12169542.html

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

发表评论

登录后才能评论

评论列表(0条)

保存