两种实现方式:1)sql,2)java
1)sql语句-mysql
SELECT NAME,num,rankingFROM (SELECT NAME,num,
IF (num =@num ,@ranking ,@ranking :=@ranking + 1)AS ranking,
@num := num
FROM score,
(SELECT @ranking := 0,@num:=-1) tmp
ORDER 答羡BY num DESC
)tmp
2) java实现
public static void main(String[] args) {//初始化数据
List<Rank> ranks = Arrays.asList(new Rank("薛之谦", 100l),
悄卜 new Rank("陈奕迅", 102l),
new Rank("启举穗田馥甄", 100l),
new Rank("周杰伦", 99l))
//按分数降序排序
ranks.sort((r1, r2) -> r2.getNum().compareTo(r1.getNum()))
//计算排名
int ranking = 0
long num = -1
for (Rank rank : ranks) {
rank.setRanking(rank.getNum() == num ? ranking : ++ranking)
num = rank.getNum()
}
//输出
ranks.forEach(o -> System.out.println(o.getName() + "" + o.getNum() + ":" + o.getRanking()))
}
import java.lang.reflect.Arrayimport java.util.Arrays
import java.util.Scanner
public class Test {
public static void Ballots() {
Scanner sc = new Scanner(System.in)
int[] count = new int[7]
String instr = ""
int spoiled = 0, sum = 0
for (int i = 0 i < count.length i++) 察团野{
count[i] = 0
}
System.out.println("输入方框数字代码投票给歌手,输入.结束 ")
do {
System.out.print("张学败喊友[0], 刘德华[1], 小刚[2], 孙燕姿[3], 梁静茹[4], 王杰[5], 刘若英[6])? ")
instr = sc.nextLine()
if (instr.equals("0")) {
count[0]++
} else if (instr.equals("1")) {
count[1]++
} else if (instr.equals("2")) {
count[2]++
} else if (instr.equals("3")) {
count[3]++
} else if (instr.equals("4")) {
count[4]++
} else if (instr.equals("5")) {
count[5]++
} else if (instr.equals("6")) {
count[6]++
} else {
if (!instr.equals(".")) {
spoiled++
}
}
} while (!instr.equals("."))
System.out.println("投票结束,结果是 : \n")
System.out.println("歌手\t\t得票数")
System.out.println("======\t\t=========")
for (int i = 0 i < count.length i++) {
if (i == 0) {
System.out.println(" 张学友 \t\t" + count[i])
} else if (i == 1) {
System.out.println(" 刘德华或腔 \t\t" + count[i])
} else if (i == 2) {
System.out.println(" 小刚 \t\t" + count[i])
} else if (i == 3) {
System.out.println(" 孙燕姿 \t\t" + count[i])
} else if (i == 4) {
System.out.println(" 梁静茹 \t\t" + count[i])
} else if (i == 5) {
System.out.println(" 王杰 \t\t" + count[i])
}else if (i == 6) {
System.out.println(" 刘若英 \t\t" + count[i])
}
}
for (int t : count) {
sum += t
}
System.out.println("弃权票数: " + spoiled)
System.out.println("总票数: " + sum)
}
public static void main(String[] args) {
System.out.println("投票开始:")
Scanner sc = new Scanner(System.in)
String is = ""
do {
Ballots()
System.out.println("继续第二轮投票? (y/n):")
is = sc.nextLine()
} while (is.equals("y"))
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)