java ;while()循环。好难啊!
答案:6 悬赏:0 手机版
解决时间 2021-04-01 23:09
- 提问者网友:焚苦与心
- 2021-04-01 09:42
java ;while()循环。好难啊!
最佳答案
- 五星知识达人网友:风格不统一
- 2021-04-01 10:56
难什么呢
很简单啊
你设置一个随机数 做机器
int n = 1+(int)(Math.random()*3); 随机参数1,2,3 三个数
跟用户输入的数字进行比较 如果大于 则机器人赢了
相等 则平手
是否根据用户输入 退出 System.exit(0);
很简单啊
你设置一个随机数 做机器
int n = 1+(int)(Math.random()*3); 随机参数1,2,3 三个数
跟用户输入的数字进行比较 如果大于 则机器人赢了
相等 则平手
是否根据用户输入 退出 System.exit(0);
全部回答
- 1楼网友:woshuo
- 2021-04-01 14:33
你北大青鸟的吧,好好想想,问问你老师、追问我想了好久了。总是思路不对啊 哥追答你现在是什么问题?哪点不对了追问就上面这题啊。 跟电脑比。我赢了也显示这句话啊 System.out.println("您输了。是否要继续?(y/n)");追答你余4试试,余3得不到3吧、
- 2楼网友:春色三分
- 2021-04-01 13:43
我刚刚测试过了,你说的功能实现了,基本上没改你的程序:
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;
}
}
}
}
}
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;
}
}
}
}
}
- 3楼网友:長槍戰八方
- 2021-04-01 12:25
把if改为while然后在while最后一句再sc=xia.next().charAt(0);
这种至少做一次的程序用do..while就正合适追问大神。我加了。可是跟机器人不管我赢也好还是输也好。直接跳
else {System.out.println("您输了。是否要继续");}这句了。还是出不来啊。
这种至少做一次的程序用do..while就正合适追问大神。我加了。可是跟机器人不管我赢也好还是输也好。直接跳
else {System.out.println("您输了。是否要继续");}这句了。还是出不来啊。
- 4楼网友:長槍戰八方
- 2021-04-01 11:53
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 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("--------------------输入正确指令-------------");
}
}
}
}
- 5楼网友:毛毛
- 2021-04-01 11:03
试试这个吧,我把你的代码修改了一下
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)");
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)");
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯