java - 为什么我的方法 pickANumberPro() 不起作用
问题描述
我创建了这个程序:
import java.util.Random;
public class pickANumberPro {
public static void main(String args[]) {
//created the odds of each number being rolled
// 3 and seven have skewed odds to come up more often than the others
String names[] = { "1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1", "2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2", "3","3","3","3","3", "4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4", "5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5", "6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","7", "8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10", };
//creates random object
Random Dice = new Random();
//determines random
int n = Dice.nextInt(166);
prints out which number it lands on
System.out.println(names[n]);
}
上面的程序运行,我怎样才能把上面的代码转换成一个方法,让 main 调用它,并显示每个随机数选择了多少个数据?下面,是我开始的。
public static Double pickANumberPro(){
String randomdie[] = { "1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1", "2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2", "3","3","3","3","3", "4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4", "5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5", "6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","7", "8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10"};
Random dice = new Random();
int n = dice.nextInt(166);
int rand = (randomdie[n]);
return rand;
// System.out.println(rand[n]);
}
public static void main(String[] args)
{
final int ATTEMPTS = 500;
int i, target, guess;
int wins; // counter for number of wins (correct guesses)
Random rand = new Random(); // Create a Random object.
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
for (i=0, wins=0; i < ATTEMPTS; i++)
{
target = pickANumberPro(rand);
guess = pickANumberPro(rand);
if (target == guess) wins++;
}
System.out.printf( "Pro vs. Pro: %d wins out of %d attempts, %.2f%% \n", wins, ATTEMPTS, (wins*100.0/ (double)ATTEMPTS));
编辑:
Random dice = new Random();
int n = dice.nextInt(166);
String rand = (randomdie[n]);
return rand;
// System.out.println(rand[n]);
}
解决方案
这应该可以修复您的代码。
public static void main(String[] args) {
final int ATTEMPTS = 500;
int i, target, guess;
int wins; // counter for number of wins (correct guesses)
Random rand = new Random(); // Create a Random object.
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
for (i=0, wins=0; i < ATTEMPTS; i++)
{
guess = keyboard.nextInt();
target = pickANumberPro(rand);
if (target == guess) wins++;
}
System.out.printf( "Pro vs. Pro: %d wins out of %d attempts, %.2f%% \n", wins, ATTEMPTS, (wins*100.0/ (double)ATTEMPTS));
}
public static int pickANumberPro(Random random){
int diceEntries[] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3, 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10};
int n = random.nextInt(166);
int selectedNumber = (diceEntries[n]);
System.out.println(selectedNumber);
return selectedNumber;
}
在代码中修复了一些问题:
- 在 pickANumberPro 方法中接受随机对象
- 正确打印 selectedNumber
- 使用扫描仪获取键盘输入并使用它
推荐阅读
- c# - VS2017 C# 与 VB 创建事件
- android - 如何安装我的 Android APK 以便可以从我的 Android 桌面启动该应用程序?
- docker - 使用 vpn 时的 docker 容器网络访问
- python - 如何通过另一个函数更改 on_press 函数?(蟒蛇,kivy)
- angularjs - ng-show 不适用于嵌套的 ng-repeat
- amazon-web-services - 如何连接我的 redshift 数据库?
- java - ECB 模式(实体、控制、边界)实现
- java - 如何发出错误然后使用 RxJava 发出缓存的数据?
- php - FB CanvasLoginHelper 对象缺少导致 NULL 会话的某些用户的信息
- c++ - 双摆返回 nan 结果。为什么?