import java.util.* ; public class xuanjv{ static boolean judge2(String a){ 
boolean b=false ; 
for(int c=0;c<3;c++) 
if(a.charAt(c)!='a'&&a.charAt(c)!='b'&&a.charAt(c)!='c') 
b= true ; 
return b ; } 
static boolean judge1(String a){ 
boolean b=false ; 
for(int c=0;c<3;c++) 
{for(int d=c+1;d<3;d++) 
if(a.charAt(c)==a.charAt(d)) 
b=true ; 

return b ; 

public static void main(String args[]){ 
System.out.println("现在开始选举.") ; Scanner a ; 
int b ; 
boolean c=false ; do{ 
System.out.print("请输入投票人数:") ; 
b=0 ; 
try{a = new Scanner(System.in) ; 
b = Integer.parseInt(a.nextLine()) ; 
c = true ;} 
catch(Exception d){ 
System.out.println("非法的输入!") ; 

}while (c!=true) ; int f ; 
String g[] = new String[b] ; 
char h[][] = new char[b][3] ; 
int i; do{ 
c = true ; 
for(f=0;f<b;f++) 
{System.out.print("请输入第"+(f+1)+"张投票内容:") ; 
a = new Scanner(System.in) ; 
g[f] = a.nextLine() ; 
if(g[f].replaceAll(" ","").length()!=3) 
{c = false ; 
System.out.println("刚才的输入有误,请重新输入!") ; 
break ;} 
else 
g[f] = g[f].replaceAll(" ","") ; 
if(judge1(g[f])) 
{System.out.println("此张选票无效!原因是有相同元素.") ;continue ;} 
if(judge2(g[f])) 
{System.out.println("此张选票无效!原因是输入了\"a b c\"以外的元素.") ;continue ;} 
for(i=0;i<3;i++) 
h[f][i] = g[f].charAt(i) ; } 
} while (c!=true) ; for(f=0;f<b;f++) 
for(i=0;i<3;i++) 
System.out.println(h[f][i]) ; 


我的本意是做一个投票程序,然后把所有的票都打印出来,那票只限于a b c 三个字符..程序如上.但是在输出时却出错... 现在开始选举. 
请输入投票人数:5 
请输入第1张投票内容:a b c 
请输入第2张投票内容:a a a 
此张选票无效!原因是有相同元素. 
请输入第3张投票内容:b b b 
此张选票无效!原因是有相同元素. 
请输入第4张投票内容:a b c 
请输入第5张投票内容:a b c 


c 处理已完成。 本来有三张票是正确的,但只输出了一张..
请问为什么会出现这个问题?

解决方案 »

  1.   

    现在开始选举.
    请输入投票人数:
    非法的输入!
    请输入投票人数:a b c
    非法的输入!
    请输入投票人数:5
    请输入第1张投票内容:a b c
    请输入第2张投票内容:1 2 3
    此张选票无效!原因是输入了"a b c"以外的元素.
    请输入第3张投票内容:a c b
    请输入第4张投票内容:a a b
    此张选票无效!原因是有相同元素.
    请输入第5张投票内容:c b a
    a
    b
    c
    `
    `
    `
    a
    c
    b
    `
    `
    `
    c
    b
    a
    你这个不只是把正确的票输出来了,也把无效的票也输出来了,你存贮正确的票时也把错误的票也存储了,这样打印的时候所有的票都出来了
      

  2.   

    我的程序只输出了现在开始选举. 
    请输入投票人数:5 
    请输入第1张投票内容:a b c 
    请输入第2张投票内容:a a a 
    此张选票无效!原因是有相同元素. 
    请输入第3张投票内容:b b b 
    此张选票无效!原因是有相同元素. 
    请输入第4张投票内容:a b c 
    请输入第5张投票内容:a b c 


    c 处理已完成。 
    ...晕