永发信息网

java ;while()循环。好难啊!

答案:6  悬赏:0  手机版
解决时间 2021-04-01 23:09
java ;while()循环。好难啊!
最佳答案
难什么呢
很简单啊
你设置一个随机数 做机器
int n = 1+(int)(Math.random()*3); 随机参数1,2,3 三个数
跟用户输入的数字进行比较 如果大于 则机器人赢了
相等 则平手
是否根据用户输入 退出 System.exit(0);
全部回答
你北大青鸟的吧,好好想想,问问你老师、追问我想了好久了。总是思路不对啊 哥追答你现在是什么问题?哪点不对了追问就上面这题啊。 跟电脑比。我赢了也显示这句话啊 System.out.println("您输了。是否要继续?(y/n)");追答你余4试试,余3得不到3吧、
我刚刚测试过了,你说的功能实现了,基本上没改你的程序:
import java.util.Scanner;
public class caiquan {

public static void main(String[] args) {
// 猜拳
int xiao = 0;
System.out.println("---------欢迎来到游戏世界-----");
System.out.println("");
System.out.println("\t***********");
System.out.println("\t**猜拳,开始**");
System.out.println("\t***********");
System.out.println("");
System.out.println("出拳规则:1.剪刀2.石头3.布");
System.out.println("");
System.out.println("要开始出拳吗?(y/n)");
for (int i = 0; i < 99999; i++) {
int c;
Scanner xia = new Scanner(System.in);
char sc = xia.next().charAt(0);
while (sc == 'y') {
System.out.println("请出拳1.剪刀2.石头3.布");

xiao = xia.nextInt();
switch (xiao) {
case 1:
System.out.println("剪刀");
break;
case 2:
System.out.println("石头");
break;
case 3:
System.out.println("布");
break;
default:
System.out.println("您输入的有误");
break;
}
double a = Math.random() * 10;
c = (int) a % 3;
c++;
System.out.println("电脑出拳:" + c);
// int xiao=xia.nextInt();
if ((xiao == 1 && c == 3) || (xiao == 2 && c == 1)
|| (xiao == 3 && c == 2)) {
System.out.println("哈哈,您赢了");
} else if ((xiao == 1 && c == 1) || (xiao == 2 && c == 2)
|| (xiao == 3 && c == 3)) {
System.out.println("平局");
} else {
System.out.println("您输了。是否要继续(y/n)");
break;
}
}
}
}
}
把if改为while然后在while最后一句再sc=xia.next().charAt(0);
这种至少做一次的程序用do..while就正合适追问大神。我加了。可是跟机器人不管我赢也好还是输也好。直接跳
else {System.out.println("您输了。是否要继续");}这句了。还是出不来啊。
package test;
import java.util.Scanner;
public class Guessing {
// 初始化构造方法
public Guessing() {
System.out.println("---------欢迎来到游戏世界-----");
System.out.println("\t***********");
System.out.println("\t**猜拳,开始**");
System.out.println("\t***********");
System.out.println("要开始出拳吗?(y/n)");
}
// 判断方法
public static void result(int people) {
System.out.println("-=-=-=-=" + people);
String pe = getcount(people);
if (!pe.equals("") && null != pe) {
int n = 1 + (int) (Math.random() * 3);// 随机参数1,2,3 三个数
String cuntString = getcount(n);
if (people == n) {
System.out.println("====和局");
} else {
if (people == n) {
System.out.println("你出:" + pe + "=====电脑出:" + cuntString
+ "-----------和局");
} else {
if (people == 1 && n == 3) {
System.out.println("你出:" + pe + "======电脑出:"
+ cuntString + "-----------你赢了");
return;
}
if (people == 3 && n == 1) {
System.out.println("你出:" + pe + "=====电脑出:"
+ cuntString + "------------电脑赢了");
return;
}
if (people > n) {
System.out.println("你出:" + pe + "=====电脑出:"
+ cuntString + "--------------你赢了");
return;
} else {
System.out.println("你出:" + pe + "====电脑出:" + cuntString
+ "-----------电脑赢了");
return;
}
}
}
}
}
public static String getcount(int n) {
String pString = "";
switch (n) {
case 1:
pString = "剪刀";
break;
case 2:
pString = "石头";
break;
case 3:
pString = "布";
break;
default:
System.out.println("-------您输入的有误请重新输入-------");
break;
}
return pString;
}
public static void main(String[] args) {
Guessing g = new Guessing();
Scanner xia = new Scanner(System.in);
String sc = xia.next();
while (true) {
if (sc.equals("n") || sc.equals("y")) {
if (sc.equals("y")) {
System.out.println("出拳规则:1.剪刀2.石头3.布0.退出游戏");
Scanner scanner = new Scanner(System.in);
int people = scanner.nextInt();
if (people == 0) {
System.out
.println("--------------------谢谢使用猜拳游戏EXIT--------------");
System.exit(1);
}
g.result(people);
} else {
System.out
.println("--------------------谢谢使用猜拳游戏EXIT--------------");
System.exit(1);
}
} else {
System.out.println("--------------------输入正确指令-------------");
}
}
}
}
试试这个吧,我把你的代码修改了一下
import java.util.Scanner;
public class Caiquan {

public static void main(String[] args) {
// 猜拳
boolean isContinue = true;
int xiao = 0;
System.out.println("---------欢迎来到游戏世界-----");
System.out.println("");
System.out.println("\t***********");
System.out.println("\t**猜拳,开始**");
System.out.println("\t***********");
System.out.println("");
System.out.println("出拳规则:1.剪刀2.石头3.布");
System.out.println("");
while(isContinue){
System.out.println("要开始出拳吗?(y/n)");
Scanner xia = new Scanner(System.in);
char sc = xia.next().charAt(0);
if (sc == 'y') {
System.out.println("请出拳1.剪刀2.石头3.布");

xiao = xia.nextInt();
switch (xiao) {
case 1:
System.out.println("剪刀");
break;
case 2:
System.out.println("石头");
break;
case 3:
System.out.println("布");
break;
default:
System.out.println("您输入的有误");
break;
}
}
double a = Math.random() * 10;
int c = (int) a % 3;
++c;
System.out.println(+c);
// int xiao=xia.nextInt();
if ((xiao == 1 && 'c' == 3) || (xiao == 2 && 'c' == 1) || (xiao == 3 && 'c' == 2)) {
System.out.println("哈哈,您赢了");
} else if ((xiao == 1 && 'c' == 1) || (xiao == 2 && 'c' == 2)|| (xiao == 3 && 'c' == 3)) {
System.out.println("平局");
} else {
System.out.println("您输了。是否要继续?(y/n)");
System.out.println(xia.next());
if("y".equalsIgnoreCase(xia.next())){
continue;
}else if ("n".equalsIgnoreCase(xia.next())){
isContinue = false;
return;
}else{
System.out.println("输入错误,请重新输入");
}
}
}
}
}追问还是不对啊。总是我输啊。 不管怎么都直跳这句System.out.println("您输了。是否要继续?(y/n)");
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
前田氏的加贺藩史
乔丹鞋有两种标志是什么意思
t231次列车3车厢座位分布表
扶沟县顺丰快递具体位置和电话
单选题森林中的一棵死亡倒下的树及在其上生活
带雪和星的成语
孢子中怎么把别人弄的生物和房子直接搞成自己
竹节棉 ,拉架棉,棉混纺,混合棉纺那个最好?
歌词中包含:单纯的像小孩,。。。你的表情。
成语三檐四簇马出彩怎么解
求大神教我写星期六不回校学习的申请书
为什么一定要在3,4月走一趟川藏线
5(x+2)的二次方-180=0
三国群英传3怎么加速啊?
谁有恐怖殡仪馆小说~
推荐资讯
我用卓大师一键root获取权限,结果失败重启后
美立方面膜价格
谁发明了冰棍?
辣椒吃多了好吗?
为什么我什么都要听父亲的,我都20多岁了,我
斗地主残局破解88 1010 333 6 7张如
铁岭市银州区福寿堂殡葬用品服务部地址好找么
求助关于rpm 同时安装2个包的命令
大家说说这两串那一串好?
试论保险业诚信缺失的危害有哪些
满液式蒸发器的优缺点??
1.29+3.7+0.7+6.3的简便运算
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?