我一直坚持这个循环问题,如果有人能够发现我出错的地方会很高兴:)
高尔夫游戏编写一个使用while循环播放骰子高尔夫游戏的程序。到洞的距离从100米开始。该人反复选择使用哪个俱乐部。不同的选择每个对应于不同的侧面虚拟'骰子'滚动 - 即给出一个随机数。如果他们选择球杆1(推杆),他们会从0-5开始滚动一个数字,表示距离命中。如果他们选择2号球杆(投球),他们会从0到30输出一个数字。如果他们选择球杆3(铁杆),则他们会从0到150滚动一个数字。在每次掷骰后,从距离球洞的距离减去该数量(下一轮的负距离为正)。它们一直在滚动,直到它们到达正好为0的距离。当它们完成时,会打印出拍摄的数量。
The following shows an example run of the program:
Distance to hole 100m. Which club (1-putting, 2-pitching or 3-iron) 3
You hit it...120m
Distance to hole 20m. Which club (1-putting, 2-pitching or 3-iron) 2
You hit it...16m
Distance to hole 4m. Which club (1-putting, 2-pitching or 3-iron) 1
You hit it...1m
Distance to hole 3m. Which club (1-putting, 2-pitching or 3-iron) 1
You hit it...3m
Congratulations. You took 4 shots.
我是java新手,下面是我设法完成的工作,但显示错误:while(input.equals!(" 1" ||" 2&#34) ; ||" 3"))。我不确定它是否正确完成并花费了多少时间,如果有人可以帮助解决问题,我会很高兴:)非常感谢。
public static void dicegolfgame(String[] args)
{
String input = "";
int difference = 0;
int newdistance = 0;
int distance = 0;
int i = 0;
while (input.equals ( "1" || "2" || "3") )
{
Random roll2 = new Random();
distance = roll2.nextInt(100);
input = JOptionPane.showInputDialog ( "distance to hole " + distance + " Which club (1-putting), 2-(pitching) or 3-iron");
while (difference > 0 && difference < 0)
if (input.equals ("1"))
{
Random roll = new Random();
newdistance = roll.nextInt(5);
i = i+1;
JOptionPane.showMessageDialog(null, "you hit ..." + newdistance + "m");
difference = distance - newdistance;
}
else if (input.equals ("2"))
{
Random roll = new Random();
newdistance = roll.nextInt(30);
i = i+1;
JOptionPane.showMessageDialog(null, "you hit ..." + newdistance + "m");
difference = distance - newdistance;
}
else if (input.equals ("3"))
{
Random roll = new Random();
newdistance = roll.nextInt(150);
i = i+1;
JOptionPane.showMessageDialog(null, "you hit ..." + newdistance + "m");
difference = distance - newdistance;
}
if (difference == 0)
{
break;
JOptionPane.showMessageDialog(null, "Congratulations, you took" + i + "shots.");
}
答案 0 :(得分:0)
距离洞的距离决定了高尔夫球手是否必须再次投篮 - 所以这应该是在while循环中。我在代码中添加了一些注释:
String input = "";
int distance = 100;
int numberOfHits = 0;
int hit = 0;
while (distance > 0) {
numberOfHits++;//increasing the nuber of hits
input = JOptionPane.showInputDialog("distance to hole " + distance + " Which club (1-putting), 2-(pitching) or 3-iron");
switch (input) {//if you don't know switch you coukd use if-else
case "1":
Random roll = new Random();
hit = roll.nextInt(6);//a random int netween 0-5
JOptionPane.showMessageDialog(null, "you hit ..." + hit + "m");
distance -= hit;//sets the new distance to the old distance subtracted the hit
distance = Math.abs(distance);//if distance is negative make it positive
break;
case "2":
roll = new Random();
hit = roll.nextInt(31);
JOptionPane.showMessageDialog(null, "you hit ..." + hit + "m");
distance -= hit;
distance = Math.abs(distance);
break;
case "3":
roll = new Random();
hit = roll.nextInt(151);
JOptionPane.showMessageDialog(null, "you hit ..." + hit + "m");
distance -= hit;
distance = Math.abs(distance);
break;
}
}
//when we are out of the loop the distance is zero
JOptionPane.showMessageDialog(null, "Congratulations, you took " + numberOfHits + " shots.");