直到猜对为止
代码如下:
public class GuessNum{
public static void main(String args[]) throws java.io.IOException{
int i = 0 ;
char num = '6';
while(i!=num){
System.out.println("Please input a mumber between 1 and 10.");
i = System.in.read();
if(i==num) System.out.println("You are right!");
else{
if(i<num){
System.out.println("The number is low!");
}
else{
System.out.println("The number is high!");
}
}
}
}
}
为什么运行结果会是这样啊??为什么后面会多两段啊??
大于6时:
Please input a mumber between 1 and 10.
7
The number is high!
Please input a mumber between 1 and 10.
The number is low!
Please input a mumber between 1 and 10.
The number is low!
Please input a mumber between 1 and 10.小于6时:
Please input a mumber between 1 and 10.
4
The number is low!
Please input a mumber between 1 and 10.
The number is low!
Please input a mumber between 1 and 10.
The number is low!
Please input a mumber between 1 and 10.
代码如下:
public class GuessNum{
public static void main(String args[]) throws java.io.IOException{
int i = 0 ;
char num = '6';
while(i!=num){
System.out.println("Please input a mumber between 1 and 10.");
i = System.in.read();
if(i==num) System.out.println("You are right!");
else{
if(i<num){
System.out.println("The number is low!");
}
else{
System.out.println("The number is high!");
}
}
}
}
}
为什么运行结果会是这样啊??为什么后面会多两段啊??
大于6时:
Please input a mumber between 1 and 10.
7
The number is high!
Please input a mumber between 1 and 10.
The number is low!
Please input a mumber between 1 and 10.
The number is low!
Please input a mumber between 1 and 10.小于6时:
Please input a mumber between 1 and 10.
4
The number is low!
Please input a mumber between 1 and 10.
The number is low!
Please input a mumber between 1 and 10.
The number is low!
Please input a mumber between 1 and 10.
解决方案 »
- failed to lazily initialize a collection of role: com.db.tarena.crm.product.entity.Product.order, no session or session was clos
- 如何得到hibernate自动生成的数据库操作语言DML
- 菜包请教:Java代码中提示下面这行没有配对的包,是啥意思?
- 把应用服务器和数据库安装在同一台机上和装在不同的机上有何区别
- 那里下栽weblogic啊?
- struts+spring+hibernate调用数据库存储过程
- 200分请教xml解析器用在什么地方(有什么用途)?你们常用什么解析器?
- 我用tomcat连接池连接mysql,用jsp测试成功,可是改用class ConnectionProvider测试时出错,请大虾指点一下!在线等!
- 急,请大家帮忙看一下这段程序什么意思,我知道是创建会话
- 子集和
- socket连接tomcat问题
- 菜鸟提问~~Struts 1
你想实现的效果应该用Scanner来做,把你的
i = System.in.read();
换成下面这两句Scanner scanner = new Scanner(System.in);
i = scanner.nextInt();另外,你的char num = '6'应该换成 int num = 6
多看看API吧
char num = '6';
@@@@@Scanner sc = new Scanner(System.in);
while (i != num) {
System.out.println("Please input a mumber between 1 and 10.");
@@@@i = sc.nextInt();这里应该是缓存的问题 具体解决如上所示
import java.util.Scanner;public class GuessNum{
public static void main(String args[]) throws java.io.IOException{
int i = 0 ;
//char num = '6'; 要拿i和num比较,最好是让它们类型一样
int num = 6; Scanner scanner = new Scanner(System.in); //从Scanner而不是直接从System.in读
while(i!=num){
System.out.println("Please input a mumber between 1 and 10.");
//i = System.in.read();
i = scanner.nextInt(); //scanner支持直接读入一个整数
if(i==num) System.out.println("You are right!");
else{
if(i<num){
System.out.println("The number is low!");
}
else{
System.out.println("The number is high!");
} } }
}
}