java dicegolfgame-whileloop
我被这个while循环问题困住了,如果有人能指出我的错误,我会很高兴: 高尔夫游戏编写一个程序,使用while循环玩骰子高尔夫游戏。到洞的距离从100米开始。此人反复选择使用哪个俱乐部。每个不同的选择对应一个不同的虚拟“骰子”,即给出一个随机数。如果他们选择1号杆作为推杆,他们将掷0-5之间的数字,表示击球距离。如果他们选择2号球杆投球,他们的掷骰数为0-30。如果他们选择梅花3,他们会掷一个从0到150的数字。每次滚动后,从到孔的距离中减去该数字,下一轮负距离为正。它们一直滚动,直到距离精确到0。当他们这样做时,拍摄的照片数量会被打印出来java dicegolfgame-whileloop,java,while-loop,Java,While Loop,我被这个while循环问题困住了,如果有人能指出我的错误,我会很高兴: 高尔夫游戏编写一个程序,使用while循环玩骰子高尔夫游戏。到洞的距离从100米开始。此人反复选择使用哪个俱乐部。每个不同的选择对应一个不同的虚拟“骰子”,即给出一个随机数。如果他们选择1号杆作为推杆,他们将掷0-5之间的数字,表示击球距离。如果他们选择2号球杆投球,他们的掷骰数为0-30。如果他们选择梅花3,他们会掷一个从0到150的数字。每次滚动后,从到孔的距离中减去该数字,下一轮负距离为正。它们一直滚动,直到距离精确到
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 || 3 . 我不确定它是否做得正确,是否花了很多时间在这个问题上,如果有人能帮助解决这个问题,我会很高兴:非常感谢
是到球洞的距离决定了高尔夫球手是否需要再次投篮,所以应该在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.");
while difference>0&&difference<0-这怎么可能?我不确定,因为我说我是新手,但问题是while循环即使差异为负值也要继续,所以我认为应该保持差异>0,并感谢您的帮助!!:这使它很容易理解,现在它更有意义了@尼罗斯
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.");