黑马程序员技术交流社区
标题:
关于循环中再次返回循环的操作;如何实现
[打印本页]
作者:
贾成龙
时间:
2012-8-2 22:29
标题:
关于循环中再次返回循环的操作;如何实现
import java.util.Random;
import java.util.Scanner;
class GussMath
{
public static void main(String[]agrs)
{
Scanner sc=new Scanner(System.in);
Random r=new Random();
boolean flag=true;
int b=(int)(r.nextInt(100));
while(flag)
{
int number=sc.nextInt();
if(number<1 || number>100)
{
System.out.println("请输入1~100之间的数");
// flag=false;
break;
}
if(number==b)
{
System.out.println("恭喜你,猜对了");
System.out.println("请问你还想继续玩吗?是请按Y/否请按N");
String s=sc.nextLine();
String c1="Y";
String c2="N";
if(sc.equals(c1))
{
Random ra=new Random();
//int b=(int)(ra.nextInt(100));//请各位大侠帮忙解决一下这里如何实现继续向下执行?(对循环中嵌套在返回去执行老是迷糊)
flag=true;
continue;
}
if(sc.equals(c2))
{
break;
}
//flag=false;
}
if(number>b)
{
System.out.println("大了,请继续");
continue;
}
if(number<b)
{
System.out.println("小了,请继续");
continue;
}
}
}
}
求大侠们给解决一下注释的地方!!!
作者:
刘健
时间:
2012-8-3 00:36
改写了一下,你的 String s=sc.nextLine();获取不到值
import java.util.Random;
import java.util.Scanner;
class GussMath {
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
Random r = new Random();
String number;
boolean flag = true;
int b = (int)(r.nextInt(100));
while (flag) {
System.out.println(b);
number = sc.next();
if (Integer.valueOf(number) < 1 || Integer.valueOf(number) > 100) {
System.out.println("请输入1~100之间的数");
break;
}
if (Integer.valueOf(number) == b) {
System.out.println("恭喜你,猜对了");
System.out.println("请问你还想继续玩吗?是请按Y/否请按N");
number = sc.next();
String c1 = "y";
String c2 = "N";
if (number.equals(c1)) {
b = (int)(r.nextInt(100));
continue;
}
if (sc.equals(c2)) {
break;
}
}
if (Integer.valueOf(number) > b) {
System.out.println("大了,请继续");
continue;
}
if (Integer.valueOf(number) < b) {
System.out.println("小了,请继续");
continue;
}
}
}
}
复制代码
作者:
贾成龙
时间:
2012-8-3 22:41
哥们,我的意思是用基础的知识写出来!!!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2