高手们帮下忙,用JAVA编
发布网友
发布时间:2024-10-04 18:16
我来回答
共2个回答
热心网友
时间:2024-10-04 20:27
我来为楼主回答一下:
【说明】
根据楼主,程序完成了以下功能:
1、投票三轮,每轮输入a,b,c三个足球运动员
2、按照顺序不同分值不同
3、如果三名运动员分数相同并且出现除了这三个运动员的其他编号时,都视为作废
4、计算出成绩最好的运动员,打印“谁是足球先生”的字样
5、并且打印出所有运动员的分数;
完成了所有楼主期望的功能!
【程序如下】
import java.io.*;
public class CharTest
{
private int a = 0;
private int b = 0;
private int c = 0;
public void input()throws Exception
{
System.out.println("请开始第一轮投票:");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
String str1 = br1.readLine();
char[] ch1 = str1.toCharArray();
System.out.println("请开始第二轮投票:");
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
String str2 = br2.readLine();
char[] ch2 = str2.toCharArray();
System.out.println("请开始第三轮投票:");
BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
String str3 = br3.readLine();
char[] ch3 = str3.toCharArray();
if(((ch1[0]+ch1[1]+ch1[2])=='a'+'b'+'c')&&((ch2[0]+ch2[1]+ch2[2])=='a'+'b'+'c')&&((ch3[0]+ch3[1]+ch3[2])=='a'+'b'+'c'))
{
int i=0;
while(i<3)
{
if(i==0)
{
if(ch1[i] == 'a') a+=5;
if(ch1[i] == 'b') b+=5;
if(ch1[i] == 'c') c+=5;
}
if(i==1)
{
if(ch1[i] == 'a') a+=3;
if(ch1[i] == 'b') b+=3;
if(ch1[i] == 'c') c+=3;
}
if(i==2)
{
if(ch1[i] == 'a') a+=2;
if(ch1[i] == 'b') b+=2;
if(ch1[i] == 'c') c+=2;
}
i++;
}
i=0;
while(i<3)
{
if(i==0)
{
if(ch2[i] == 'a') a+=5;
if(ch2[i] == 'b') b+=5;
if(ch2[i] == 'c') c+=5;
}
if(i==1)
{
if(ch2[i] == 'a') a+=3;
if(ch2[i] == 'b') b+=3;
if(ch2[i] == 'c') c+=3;
}
if(i==2)
{
if(ch2[i] == 'a') a+=2;
if(ch2[i] == 'b') b+=2;
if(ch2[i] == 'c') c+=2;
}
i++;
}
i=0;
while(i<3)
{
if(i==0)
{
if(ch3[i] == 'a') a+=5;
if(ch3[i] == 'b') b+=5;
if(ch3[i] == 'c') c+=5;
}
if(i==1)
{
if(ch3[i] == 'a') a+=3;
if(ch3[i] == 'b') b+=3;
if(ch3[i] == 'c') c+=3;
}
if(i==2)
{
if(ch3[i] == 'a') a+=2;
if(ch3[i] == 'b') b+=2;
if(ch3[i] == 'c') c+=2;
}
i++;
}
String x = max(a,b,c);
if(a!=b || a!=c || c!=b)
{
System.out.println(x+"获得了足球先生");
System.out.println("a选手的总分是"+a);
System.out.println("b选手的总分是"+b);
System.out.println("c选手的总分是"+c);
}else
{
System.out.println(x);
}
}else
{
System.out.println("投票无效");
}
}
public String max(int a,int b,int c)
{
String x = null;
if(a>b && a>c) x="a";
if(b>a && b>c) x="b";
if(c>b && c>a) x="c";
if(a==b && a==c) x = "他们成绩相同,重新投票!";
return x;
}
public static void main(String[] args)throws Exception
{
CharTest ct = new CharTest();
ct.input();
}
}
【编译与运行】
编译:javac CharTest.java
执行:java CharTest
代码经过测试正确,希望我的回答能够给楼主以帮助!
热心网友
时间:2024-10-04 20:27
import java.util.*;
public class Test{
static int N=0;
static int[] footBallPlayer = new int[3];
static int[] a = new int[3];
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
System.out.println("输入人数:");
N=sc.nextInt();
a[0]=5;a[1]=3;a[2]=2;
String s = sc.nextLine();
try{
for(int i=0;i<N;++i){
s=sc.nextLine();
count(s);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
for(int i=0;i<3;++i){
System.out.println((char)(i+'a') + ": "+footBallPlayer[i]);
}
}
public static void count(String s){
String[] vote = s.split("\\s");
if(vote[0].equals(vote[1])||vote[1].equals(vote[2])||vote[0].equals(vote[2])){
System.out.println(s + " 此票无效");
return;
}
for(int i=0;i<vote.length;++i)
footBallPlayer[vote[i].toCharArray()[0]-'a']+=a[i];
}
}