开发者社区> 问答> 正文

【我是菜鸟】Io流输出,对System.out.read()进行循环,输出结果会重复三遍?报错

问题基本描述:我在win7的命令行下调用java编译<<java完全手册>>上的一个程序,内容贴在下面。编译时候不会报错,但是会出现类似于重复输出三次的问题。

代码在这里

class contral{
public static void main(String args[])
throws java.io.IOException {
char choice;
int ind1;//pasage index1
System.out.println("your choice?");
System.out.println("Help infor:");
System.out.println("1.if");
System.out.println("2.do-while");
System.out.println("3.while");
System.out.println("4.switch");
System.out.println("5.for");
do{
choice=(char)System.in.read();//remark a
switch(choice){
case '1':System.out.print("If(bull value)\n"); 
System.out.print("  statement\n");
System.out.print("else\n");
System.out.print("  statement\n");
break;
case '2':System.out.print("do (statement)\n  while (bull value)");
break;
case '3':System.out.print("while (bull valie)\n  (statement)");
break;
case '4':System.out.print("switch (value)\n case a1\n  statement1 \n  break;...");
break;
case '5':System.out.print("for (variable = initial value; bull value formula; step length)\n  statement");
break;
default: System.out.println("Wrong message, input the right message again.");//Goto remark a 


break;
}
}while (choice<'1'||choice>'5');
//}while (ind1==1);


}
}




输出结果在这里

D:\ProgramData\Java\java Menu

Help on:

1. if

2. switch

3.while 

4.do-while

5.for

Choose one 

a //我输入的值,按a之后回车

Help on:

<p style="font-size:13.3333330154419px;">
	1. if
</p>
<p style="font-size:13.3333330154419px;">
	2. switch
</p>
<p style="font-size:13.3333330154419px;">
	3.while 
</p>
<p style="font-size:13.3333330154419px;">
	4.do-while
</p>
<p style="font-size:13.3333330154419px;">
	5.for
</p>
<p style="font-size:13.3333330154419px;">
	Choose one:
</p>
<p style="font-size:13.3333330154419px;">
	</p><p style="font-size:13.3333330154419px;">
		Help on:
	</p>
	<p style="font-size:13.3333330154419px;">
		1. if
	</p>
	<p style="font-size:13.3333330154419px;">
		2. switch
	</p>
	<p style="font-size:13.3333330154419px;">
		3.while 
	</p>
	<p style="font-size:13.3333330154419px;">
		4.do-while
	</p>
	<p style="font-size:13.3333330154419px;">
		5.for
	</p>
	<p style="font-size:13.3333330154419px;">
		Choose one 
	</p>
	<p style="font-size:13.3333330154419px;">
		Help on:
	</p>
	<p style="font-size:13.3333330154419px;">
		1. if
	</p>
	<p style="font-size:13.3333330154419px;">
		2. switch
	</p>
	<p style="font-size:13.3333330154419px;">
		3.while 
	</p>
	<p style="font-size:13.3333330154419px;">
		4.do-while
	</p>
	<p style="font-size:13.3333330154419px;">
		5.for
	</p>
	<p style="font-size:13.3333330154419px;">
		Choose one 
	</p>
	<p style="font-size:13.3333330154419px;">
		__(第二次提示输入)
	</p>
	<p style="font-size:13.3333330154419px;">
		也就是字符串出现三次,但是后两次是自动弹出的,完全是多余的。
	</p>
	<p style="font-size:13.3333330154419px;">
		第一次发表问题,不知道我表达清楚了没有。请各位大大指正!
	</p>
<p></p>

展开
收起
爱吃鱼的程序员 2020-06-14 14:54:43 534 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

        这是因为在你输入a之后还有一个回车符\n而这个回车符号正好也是两个字节,导致多出了两个 输出信息。

       我们可以使用Scanner读取用户输入的一行信息,然后只取第一个字节作为,判断标准,其他的字符全部忽略。e.g


     
      importjava.util.Scanner;classcontral{   publicstaticvoidmain(Stringargs[])           throwsjava.io.IOException{       bytechoice;       System.out.println("yourchoice?");       System.out.println("Helpinfor:");       System.out.println("1.if");       System.out.println("2.do-while");       System.out.println("3.while");       System.out.println("4.switch");       System.out.println("5.for");       Scannersc=newScanner(System.in);       do{           choice= sc.nextLine().getBytes()[0];//remarka           switch(choice){               case'1':                   System.out.print("If(bullvalue)\n");                   System.out.print(" statement\n");                   System.out.print("else\n");                   System.out.print(" statement\n");                   break;               case'2':                   System.out.print("do(statement)\n while(bullvalue)");                   break;               case'3':                   System.out.print("while(bullvalie)\n (statement)");                   break;               case'4':                   System.out.print("switch(value)\ncasea1\n statement1\n break;...");                   break;               case'5':                   System.out.print("for(variable=initialvalue;bullvalueformula;steplength)\n statement");                   break;               default:                   System.out.println("Wrongmessage,inputtherightmessageagain.");//Gotoremarka                   break;           }       }while(choice<'1'||choice>'5');   }}
     


    回复 @onlyamoment:同样的道理,在Scanner读取一行数据之后,也会读取一个换行符,所以byte[1]取出来的应该是换行符,程序依然不报错,如果解决了你的问题,请采纳。感谢老大!还有个问题,在调用getbyte()[0]的时候,这里的getByte()是一个数组吗?为什么我把【0】改成【1】编译无错但是执行时候有错呢?无论如何,我已经非常感谢您了!
    2020-06-14 14:55:01
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
多IO线程优化版 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载